14
14
namespace ApiPlatform \GraphQl \Type ;
15
15
16
16
use ApiPlatform \Api \ResourceClassResolverInterface ;
17
- use ApiPlatform \Exception \OperationNotFoundException ;
18
17
use ApiPlatform \GraphQl \Resolver \Factory \ResolverFactoryInterface ;
19
18
use ApiPlatform \GraphQl \Type \Definition \TypeInterface ;
20
19
use ApiPlatform \Metadata \GraphQl \Mutation ;
45
44
*/
46
45
final class FieldsBuilder implements FieldsBuilderInterface
47
46
{
48
- public function __construct (private readonly PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , private readonly PropertyMetadataFactoryInterface $ propertyMetadataFactory , private readonly ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory , private readonly ResourceClassResolverInterface $ resourceClassResolver , private readonly TypesContainerInterface $ typesContainer , private readonly TypeBuilderInterface $ typeBuilder , private readonly TypeConverterInterface $ typeConverter , private readonly ResolverFactoryInterface $ itemResolverFactory , private readonly ResolverFactoryInterface $ collectionResolverFactory , private readonly ResolverFactoryInterface $ itemMutationResolverFactory , private readonly ResolverFactoryInterface $ itemSubscriptionResolverFactory , private readonly ContainerInterface $ filterLocator , private readonly Pagination $ pagination , private readonly ?NameConverterInterface $ nameConverter , private readonly string $ nestingSeparator , private readonly ?ResourceMetadataCollectionFactoryInterface $ graphQlNestedOperationResourceMetadataFactory = null )
47
+ public function __construct (private readonly PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , private readonly PropertyMetadataFactoryInterface $ propertyMetadataFactory , private readonly ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory , private readonly ResourceClassResolverInterface $ resourceClassResolver , private readonly TypesContainerInterface $ typesContainer , private readonly TypeBuilderInterface $ typeBuilder , private readonly TypeConverterInterface $ typeConverter , private readonly ResolverFactoryInterface $ itemResolverFactory , private readonly ResolverFactoryInterface $ collectionResolverFactory , private readonly ResolverFactoryInterface $ itemMutationResolverFactory , private readonly ResolverFactoryInterface $ itemSubscriptionResolverFactory , private readonly ContainerInterface $ filterLocator , private readonly Pagination $ pagination , private readonly ?NameConverterInterface $ nameConverter , private readonly string $ nestingSeparator )
49
48
{
50
49
}
51
50
@@ -68,6 +67,10 @@ public function getNodeQueryFields(): array
68
67
*/
69
68
public function getItemQueryFields (string $ resourceClass , Operation $ operation , array $ configuration ): array
70
69
{
70
+ if ($ operation instanceof Query && $ operation ->getNested ()) {
71
+ return [];
72
+ }
73
+
71
74
$ fieldName = lcfirst ('item_query ' === $ operation ->getName () ? $ operation ->getShortName () : $ operation ->getName ().$ operation ->getShortName ());
72
75
73
76
if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration (null , $ operation ->getDescription (), $ operation ->getDeprecationReason (), new Type (Type::BUILTIN_TYPE_OBJECT , true , $ resourceClass ), $ resourceClass , false , $ operation )) {
@@ -85,6 +88,10 @@ public function getItemQueryFields(string $resourceClass, Operation $operation,
85
88
*/
86
89
public function getCollectionQueryFields (string $ resourceClass , Operation $ operation , array $ configuration ): array
87
90
{
91
+ if ($ operation instanceof Query && $ operation ->getNested ()) {
92
+ return [];
93
+ }
94
+
88
95
$ fieldName = lcfirst ('collection_query ' === $ operation ->getName () ? $ operation ->getShortName () : $ operation ->getName ().$ operation ->getShortName ());
89
96
90
97
if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration (null , $ operation ->getDescription (), $ operation ->getDeprecationReason (), new Type (Type::BUILTIN_TYPE_OBJECT , false , null , true , null , new Type (Type::BUILTIN_TYPE_OBJECT , false , $ resourceClass )), $ resourceClass , false , $ operation )) {
@@ -257,17 +264,7 @@ private function getResourceFieldConfiguration(?string $property, ?string $field
257
264
$ resourceOperation = $ rootOperation ;
258
265
if ($ resourceClass && $ rootOperation ->getClass () && $ this ->resourceClassResolver ->isResourceClass ($ resourceClass ) && $ rootOperation ->getClass () !== $ resourceClass ) {
259
266
$ resourceMetadataCollection = $ this ->resourceMetadataCollectionFactory ->create ($ resourceClass );
260
- try {
261
- $ resourceOperation = $ resourceMetadataCollection ->getOperation ($ isCollectionType ? 'collection_query ' : 'item_query ' );
262
- } catch (OperationNotFoundException ) {
263
- // If there is no query operation for a nested resource we force one to exist
264
- $ nestedResourceMetadataCollection = $ this ->graphQlNestedOperationResourceMetadataFactory ->create ($ resourceClass );
265
- $ resourceOperation = $ nestedResourceMetadataCollection ->getOperation ($ isCollectionType ? 'collection_query ' : 'item_query ' );
266
- // Add filters from the metadata defined on the resource itself.
267
- if ($ filters = $ resourceMetadataCollection [0 ]?->getFilters()) {
268
- $ resourceOperation = $ resourceOperation ->withFilters ($ filters );
269
- }
270
- }
267
+ $ resourceOperation = $ resourceMetadataCollection ->getOperation ($ isCollectionType ? 'collection_query ' : 'item_query ' );
271
268
}
272
269
273
270
if (!$ resourceOperation instanceof Operation) {
0 commit comments