@@ -168,12 +168,12 @@ private function getQueryFields(string $resourceClass, ResourceMetadata $resourc
168
168
$ shortName = $ resourceMetadata ->getShortName ();
169
169
$ deprecationReason = $ resourceMetadata ->getGraphqlAttribute ('query ' , 'deprecation_reason ' , '' , true );
170
170
171
- if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration ($ resourceClass , $ resourceMetadata , null , $ deprecationReason , new Type (Type::BUILTIN_TYPE_OBJECT , true , $ resourceClass ), $ resourceClass )) {
171
+ if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration (null , $ deprecationReason , new Type (Type::BUILTIN_TYPE_OBJECT , true , $ resourceClass ), $ resourceClass )) {
172
172
$ fieldConfiguration ['args ' ] += ['id ' => ['type ' => GraphQLType::id ()]];
173
173
$ queryFields [lcfirst ($ shortName )] = $ fieldConfiguration ;
174
174
}
175
175
176
- if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration ($ resourceClass , $ resourceMetadata , null , $ deprecationReason , new Type (Type::BUILTIN_TYPE_OBJECT , false , null , true , null , new Type (Type::BUILTIN_TYPE_OBJECT , false , $ resourceClass )), $ resourceClass )) {
176
+ if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration (null , $ deprecationReason , new Type (Type::BUILTIN_TYPE_OBJECT , false , null , true , null , new Type (Type::BUILTIN_TYPE_OBJECT , false , $ resourceClass )), $ resourceClass )) {
177
177
$ queryFields [lcfirst (Inflector::pluralize ($ shortName ))] = $ fieldConfiguration ;
178
178
}
179
179
@@ -189,8 +189,8 @@ private function getMutationFields(string $resourceClass, ResourceMetadata $reso
189
189
$ resourceType = new Type (Type::BUILTIN_TYPE_OBJECT , true , $ resourceClass );
190
190
$ deprecationReason = $ resourceMetadata ->getGraphqlAttribute ($ mutationName , 'deprecation_reason ' , '' , true );
191
191
192
- if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration ($ resourceClass , $ resourceMetadata , ucfirst ("{$ mutationName }s a $ shortName. " ), $ deprecationReason , $ resourceType , $ resourceClass , false , $ mutationName )) {
193
- $ fieldConfiguration ['args ' ] += ['input ' => $ this ->getResourceFieldConfiguration ($ resourceClass , $ resourceMetadata , null , $ deprecationReason , $ resourceType , $ resourceClass , true , $ mutationName )];
192
+ if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration (ucfirst ("{$ mutationName }s a $ shortName. " ), $ deprecationReason , $ resourceType , $ resourceClass , false , $ mutationName )) {
193
+ $ fieldConfiguration ['args ' ] += ['input ' => $ this ->getResourceFieldConfiguration (null , $ deprecationReason , $ resourceType , $ resourceClass , true , $ mutationName )];
194
194
195
195
if (!$ this ->isCollection ($ resourceType )) {
196
196
$ itemMutationResolverFactory = $ this ->itemMutationResolverFactory ;
@@ -206,19 +206,27 @@ private function getMutationFields(string $resourceClass, ResourceMetadata $reso
206
206
*
207
207
* @see http://webonyx.github.io/graphql-php/type-system/object-types/
208
208
*/
209
- private function getResourceFieldConfiguration (string $ resourceClass , ResourceMetadata $ resourceMetadata , ?string $ fieldDescription , string $ deprecationReason , Type $ type , string $ rootResource , bool $ input = false , string $ mutationName = null , int $ depth = 0 ): ?array
209
+ private function getResourceFieldConfiguration (?string $ fieldDescription , string $ deprecationReason , Type $ type , string $ rootResource , bool $ input = false , string $ mutationName = null , int $ depth = 0 ): ?array
210
210
{
211
211
try {
212
+ $ resourceClass = $ this ->isCollection ($ type ) && ($ collectionValueType = $ type ->getCollectionValueType ()) ? $ collectionValueType ->getClassName () : $ type ->getClassName ();
213
+
212
214
if (null === $ graphqlType = $ this ->convertType ($ type , $ input , $ mutationName , $ depth )) {
213
215
return null ;
214
216
}
215
217
216
218
$ graphqlWrappedType = $ graphqlType instanceof WrappingType ? $ graphqlType ->getWrappedType () : $ graphqlType ;
217
219
$ isStandardGraphqlType = \in_array ($ graphqlWrappedType , GraphQLType::getStandardTypes (), true );
218
220
if ($ isStandardGraphqlType ) {
219
- $ className = '' ;
220
- } else {
221
- $ className = $ this ->isCollection ($ type ) && ($ collectionValueType = $ type ->getCollectionValueType ()) ? $ collectionValueType ->getClassName () : $ type ->getClassName ();
221
+ $ resourceClass = '' ;
222
+ }
223
+
224
+ $ resourceMetadata = null ;
225
+ if (!empty ($ resourceClass )) {
226
+ try {
227
+ $ resourceMetadata = $ this ->resourceMetadataFactory ->create ($ resourceClass );
228
+ } catch (ResourceClassNotFoundException $ e ) {
229
+ }
222
230
}
223
231
224
232
$ args = [];
@@ -236,39 +244,14 @@ private function getResourceFieldConfiguration(string $resourceClass, ResourceMe
236
244
];
237
245
}
238
246
239
- foreach ($ resourceMetadata ->getGraphqlAttribute ('query ' , 'filters ' , [], true ) as $ filterId ) {
240
- if (null === $ this ->filterLocator || !$ this ->filterLocator ->has ($ filterId )) {
241
- continue ;
242
- }
243
-
244
- foreach ($ this ->filterLocator ->get ($ filterId )->getDescription ($ resourceClass ) as $ key => $ value ) {
245
- $ nullable = isset ($ value ['required ' ]) ? !$ value ['required ' ] : true ;
246
- $ filterType = \in_array ($ value ['type ' ], Type::$ builtinTypes , true ) ? new Type ($ value ['type ' ], $ nullable ) : new Type ('object ' , $ nullable , $ value ['type ' ]);
247
- $ graphqlFilterType = $ this ->convertType ($ filterType , false , null , $ depth );
248
-
249
- if ('[] ' === substr ($ key , -2 )) {
250
- $ graphqlFilterType = GraphQLType::listOf ($ graphqlFilterType );
251
- $ key = substr ($ key , 0 , -2 ).'_list ' ;
252
- }
253
-
254
- parse_str ($ key , $ parsed );
255
- if (\array_key_exists ($ key , $ parsed ) && \is_array ($ parsed [$ key ])) {
256
- $ parsed = [$ key => '' ];
257
- }
258
- array_walk_recursive ($ parsed , function (&$ value ) use ($ graphqlFilterType ) {
259
- $ value = $ graphqlFilterType ;
260
- });
261
- $ args = $ this ->mergeFilterArgs ($ args , $ parsed , $ resourceMetadata , $ key );
262
- }
263
- }
264
- $ args = $ this ->convertFilterArgsToTypes ($ args );
247
+ $ args = $ this ->getFilterArgs ($ args , $ resourceClass , $ resourceMetadata , $ depth );
265
248
}
266
249
267
250
if ($ isStandardGraphqlType || $ input ) {
268
251
$ resolve = null ;
269
252
} elseif ($ this ->isCollection ($ type )) {
270
253
$ resolverFactory = $ this ->collectionResolverFactory ;
271
- $ resolve = $ resolverFactory ($ className , $ rootResource , $ mutationName );
254
+ $ resolve = $ resolverFactory ($ resourceClass , $ rootResource , $ mutationName );
272
255
} else {
273
256
$ resolve = $ this ->itemResolver ;
274
257
}
@@ -287,6 +270,41 @@ private function getResourceFieldConfiguration(string $resourceClass, ResourceMe
287
270
return null ;
288
271
}
289
272
273
+ private function getFilterArgs (array $ args , ?string $ resourceClass , ?ResourceMetadata $ resourceMetadata , int $ depth ): array
274
+ {
275
+ if (null === $ resourceMetadata || null === $ resourceClass ) {
276
+ return $ args ;
277
+ }
278
+
279
+ foreach ($ resourceMetadata ->getGraphqlAttribute ('query ' , 'filters ' , [], true ) as $ filterId ) {
280
+ if (null === $ this ->filterLocator || !$ this ->filterLocator ->has ($ filterId )) {
281
+ continue ;
282
+ }
283
+
284
+ foreach ($ this ->filterLocator ->get ($ filterId )->getDescription ($ resourceClass ) as $ key => $ value ) {
285
+ $ nullable = isset ($ value ['required ' ]) ? !$ value ['required ' ] : true ;
286
+ $ filterType = \in_array ($ value ['type ' ], Type::$ builtinTypes , true ) ? new Type ($ value ['type ' ], $ nullable ) : new Type ('object ' , $ nullable , $ value ['type ' ]);
287
+ $ graphqlFilterType = $ this ->convertType ($ filterType , false , null , $ depth );
288
+
289
+ if ('[] ' === substr ($ key , -2 )) {
290
+ $ graphqlFilterType = GraphQLType::listOf ($ graphqlFilterType );
291
+ $ key = substr ($ key , 0 , -2 ).'_list ' ;
292
+ }
293
+
294
+ parse_str ($ key , $ parsed );
295
+ if (\array_key_exists ($ key , $ parsed ) && \is_array ($ parsed [$ key ])) {
296
+ $ parsed = [$ key => '' ];
297
+ }
298
+ array_walk_recursive ($ parsed , function (&$ value ) use ($ graphqlFilterType ) {
299
+ $ value = $ graphqlFilterType ;
300
+ });
301
+ $ args = $ this ->mergeFilterArgs ($ args , $ parsed , $ resourceMetadata , $ key );
302
+ }
303
+ }
304
+
305
+ return $ this ->convertFilterArgsToTypes ($ args );
306
+ }
307
+
290
308
private function mergeFilterArgs (array $ args , array $ parsed , ResourceMetadata $ resourceMetadata = null , $ original = '' ): array
291
309
{
292
310
foreach ($ parsed as $ key => $ value ) {
@@ -508,15 +526,9 @@ private function getResourceObjectTypeFields(?string $resourceClass, ResourceMet
508
526
continue ;
509
527
}
510
528
511
- $ rootResource = $ resourceClass ;
512
- if (null !== $ propertyMetadata ->getSubresource ()) {
513
- $ resourceClass = $ propertyMetadata ->getSubresource ()->getResourceClass ();
514
- $ resourceMetadata = $ this ->resourceMetadataFactory ->create ($ resourceClass );
515
- }
516
- if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration ($ resourceClass , $ resourceMetadata , $ propertyMetadata ->getDescription (), $ propertyMetadata ->getAttribute ('deprecation_reason ' , '' ), $ propertyType , $ rootResource , $ input , $ mutationName , $ depth )) {
529
+ if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration ($ propertyMetadata ->getDescription (), $ propertyMetadata ->getAttribute ('deprecation_reason ' , '' ), $ propertyType , $ resourceClass , $ input , $ mutationName , $ depth )) {
517
530
$ fields ['id ' === $ property ? '_id ' : $ property ] = $ fieldConfiguration ;
518
531
}
519
- $ resourceClass = $ rootResource ;
520
532
}
521
533
}
522
534
0 commit comments