@@ -48,7 +48,7 @@ public function __construct(private readonly TypesContainerInterface $typesConta
48
48
/**
49
49
* {@inheritdoc}
50
50
*/
51
- public function getResourceObjectType (?string $ resourceClass , ResourceMetadataCollection $ resourceMetadataCollection , Operation $ operation , bool $ input , bool $ wrapped = false , int $ depth = 0 ): GraphQLType
51
+ public function getResourceObjectType (?string $ resourceClass , ResourceMetadataCollection $ resourceMetadataCollection , Operation $ operation , bool $ input , bool $ wrapped = false , int $ depth = 0 , bool $ required = true ): GraphQLType
52
52
{
53
53
$ shortName = $ operation ->getShortName ();
54
54
$ operationName = $ operation ->getName ();
@@ -84,80 +84,82 @@ public function getResourceObjectType(?string $resourceClass, ResourceMetadataCo
84
84
$ shortName .= 'Data ' ;
85
85
}
86
86
87
- if ($ this ->typesContainer ->has ($ shortName )) {
88
- $ resourceObjectType = $ this -> typesContainer -> get ( $ shortName );
89
- if (!( $ resourceObjectType instanceof ObjectType || $ resourceObjectType instanceof NonNull) ) {
90
- throw new \ LogicException ( sprintf ( ' Expected GraphQL type "%s" to be %s. ' , $ shortName , implode ( ' | ' , [ObjectType:: class, NonNull::class]))) ;
87
+ if (! $ this ->typesContainer ->has ($ shortName )) {
88
+ $ ioMetadata = $ input ? $ operation -> getInput () : $ operation -> getOutput ( );
89
+ if (null !== $ ioMetadata && \array_key_exists ( ' class ' , $ ioMetadata ) && null !== $ ioMetadata [ ' class ' ] ) {
90
+ $ resourceClass = $ ioMetadata [ ' class ' ] ;
91
91
}
92
92
93
- return $ resourceObjectType ;
94
- }
93
+ $ wrapData = !$ wrapped && ($ operation instanceof Mutation || $ operation instanceof Subscription) && !$ input && $ depth < 1 ;
95
94
96
- $ ioMetadata = $ input ? $ operation ->getInput () : $ operation ->getOutput ();
97
- if (null !== $ ioMetadata && \array_key_exists ('class ' , $ ioMetadata ) && null !== $ ioMetadata ['class ' ]) {
98
- $ resourceClass = $ ioMetadata ['class ' ];
99
- }
95
+ $ configuration = [
96
+ 'name ' => $ shortName ,
97
+ 'description ' => $ operation ->getDescription (),
98
+ 'resolveField ' => $ this ->defaultFieldResolver ,
99
+ 'fields ' => function () use ($ resourceClass , $ operation , $ operationName , $ resourceMetadataCollection , $ input , $ wrapData , $ depth , $ ioMetadata ) {
100
+ if ($ wrapData ) {
101
+ $ queryNormalizationContext = $ this ->getQueryOperation ($ resourceMetadataCollection )?->getNormalizationContext() ?? [];
100
102
101
- $ wrapData = !$ wrapped && ($ operation instanceof Mutation || $ operation instanceof Subscription) && !$ input && $ depth < 1 ;
103
+ try {
104
+ $ mutationNormalizationContext = $ operation instanceof Mutation || $ operation instanceof Subscription ? ($ resourceMetadataCollection ->getOperation ($ operationName )->getNormalizationContext () ?? []) : [];
105
+ } catch (OperationNotFoundException ) {
106
+ $ mutationNormalizationContext = [];
107
+ }
108
+ // Use a new type for the wrapped object only if there is a specific normalization context for the mutation or the subscription.
109
+ // If not, use the query type in order to ensure the client cache could be used.
110
+ $ useWrappedType = $ queryNormalizationContext !== $ mutationNormalizationContext ;
102
111
103
- $ configuration = [
104
- 'name ' => $ shortName ,
105
- 'description ' => $ operation ->getDescription (),
106
- 'resolveField ' => $ this ->defaultFieldResolver ,
107
- 'fields ' => function () use ($ resourceClass , $ operation , $ operationName , $ resourceMetadataCollection , $ input , $ wrapData , $ depth , $ ioMetadata ) {
108
- if ($ wrapData ) {
109
- $ queryNormalizationContext = $ this ->getQueryOperation ($ resourceMetadataCollection )?->getNormalizationContext() ?? [];
110
-
111
- try {
112
- $ mutationNormalizationContext = $ operation instanceof Mutation || $ operation instanceof Subscription ? ($ resourceMetadataCollection ->getOperation ($ operationName )->getNormalizationContext () ?? []) : [];
113
- } catch (OperationNotFoundException ) {
114
- $ mutationNormalizationContext = [];
115
- }
116
- // Use a new type for the wrapped object only if there is a specific normalization context for the mutation or the subscription.
117
- // If not, use the query type in order to ensure the client cache could be used.
118
- $ useWrappedType = $ queryNormalizationContext !== $ mutationNormalizationContext ;
112
+ $ wrappedOperationName = $ operationName ;
119
113
120
- $ wrappedOperationName = $ operationName ;
114
+ if (!$ useWrappedType ) {
115
+ $ wrappedOperationName = $ operation instanceof Query ? $ operationName : 'item_query ' ;
116
+ }
121
117
122
- if (!$ useWrappedType ) {
123
- $ wrappedOperationName = $ operation instanceof Query ? $ operationName : 'item_query ' ;
124
- }
118
+ $ wrappedOperation = $ resourceMetadataCollection ->getOperation ($ wrappedOperationName );
125
119
126
- $ wrappedOperation = $ resourceMetadataCollection ->getOperation ($ wrappedOperationName );
120
+ $ fields = [
121
+ lcfirst ($ wrappedOperation ->getShortName ()) => $ this ->getResourceObjectType ($ resourceClass , $ resourceMetadataCollection , $ wrappedOperation instanceof Operation ? $ wrappedOperation : null , $ input , true , $ depth ),
122
+ ];
127
123
128
- $ fields = [
129
- lcfirst ($ wrappedOperation ->getShortName ()) => $ this ->getResourceObjectType ($ resourceClass , $ resourceMetadataCollection , $ wrappedOperation instanceof Operation ? $ wrappedOperation : null , $ input , true , $ depth ),
130
- ];
124
+ if ($ operation instanceof Subscription) {
125
+ $ fields ['clientSubscriptionId ' ] = GraphQLType::string ();
126
+ if ($ operation ->getMercure ()) {
127
+ $ fields ['mercureUrl ' ] = GraphQLType::string ();
128
+ }
131
129
132
- if ($ operation instanceof Subscription) {
133
- $ fields ['clientSubscriptionId ' ] = GraphQLType::string ();
134
- if ($ operation ->getMercure ()) {
135
- $ fields ['mercureUrl ' ] = GraphQLType::string ();
130
+ return $ fields ;
136
131
}
137
132
138
- return $ fields ;
133
+ return $ fields + [ ' clientMutationId ' => GraphQLType:: string ()] ;
139
134
}
140
135
141
- return $ fields + ['clientMutationId ' => GraphQLType::string ()];
142
- }
136
+ $ fieldsBuilder = $ this ->fieldsBuilderLocator ->get ('api_platform.graphql.fields_builder ' );
137
+ $ fields = $ fieldsBuilder ->getResourceObjectTypeFields ($ resourceClass , $ operation , $ input , $ depth , $ ioMetadata );
138
+
139
+ if ($ input && $ operation instanceof Mutation && null !== $ mutationArgs = $ operation ->getArgs ()) {
140
+ return $ fieldsBuilder ->resolveResourceArgs ($ mutationArgs , $ operation ) + ['clientMutationId ' => $ fields ['clientMutationId ' ]];
141
+ }
142
+ if ($ input && $ operation instanceof Mutation && null !== $ extraMutationArgs = $ operation ->getExtraArgs ()) {
143
+ return $ fields + $ fieldsBuilder ->resolveResourceArgs ($ extraMutationArgs , $ operation );
144
+ }
143
145
144
- $ fieldsBuilder = $ this ->fieldsBuilderLocator ->get ('api_platform.graphql.fields_builder ' );
145
- $ fields = $ fieldsBuilder ->getResourceObjectTypeFields ($ resourceClass , $ operation , $ input , $ depth , $ ioMetadata );
146
+ return $ fields ;
147
+ },
148
+ 'interfaces ' => $ wrapData ? [] : [$ this ->getNodeInterface ()],
149
+ ];
146
150
147
- if ($ input && $ operation instanceof Mutation && null !== $ mutationArgs = $ operation ->getArgs ()) {
148
- return $ fieldsBuilder ->resolveResourceArgs ($ mutationArgs , $ operation ) + ['clientMutationId ' => $ fields ['clientMutationId ' ]];
149
- }
150
- if ($ input && $ operation instanceof Mutation && null !== $ extraMutationArgs = $ operation ->getExtraArgs ()) {
151
- return $ fields + $ fieldsBuilder ->resolveResourceArgs ($ extraMutationArgs , $ operation );
152
- }
151
+ $ resourceObjectType = $ input ? new InputObjectType ($ configuration ) : new ObjectType ($ configuration );
152
+ $ this ->typesContainer ->set ($ shortName , $ resourceObjectType );
153
+ }
153
154
154
- return $ fields ;
155
- },
156
- ' interfaces ' => $ wrapData ? [] : [ $ this -> getNodeInterface ()],
157
- ];
155
+ $ resourceObjectType = $ this -> typesContainer -> get ( $ shortName ) ;
156
+ if (!( $ resourceObjectType instanceof ObjectType || $ resourceObjectType instanceof NonNull || $ resourceObjectType instanceof InputObjectType)) {
157
+ throw new \ LogicException ( sprintf ( ' Expected GraphQL type "%s" to be %s. ' , $ shortName , implode ( ' | ' , [ObjectType::class, NonNull::class, InputObjectType::class])));
158
+ }
158
159
159
- $ resourceObjectType = $ input ? GraphQLType::nonNull (new InputObjectType ($ configuration )) : new ObjectType ($ configuration );
160
- $ this ->typesContainer ->set ($ shortName , $ resourceObjectType );
160
+ if ($ required && $ input ) {
161
+ $ resourceObjectType = GraphQLType::nonNull ($ resourceObjectType );
162
+ }
161
163
162
164
return $ resourceObjectType ;
163
165
}
0 commit comments