@@ -54,12 +54,13 @@ final class SchemaBuilder implements SchemaBuilderInterface
5454 private $ itemResolver ;
5555 private $ itemMutationResolverFactory ;
5656 private $ defaultFieldResolver ;
57+ private $ queryResolverLocator ;
5758 private $ filterLocator ;
5859 private $ typesFactory ;
5960 private $ paginationEnabled ;
6061 private $ graphqlTypes = [];
6162
62- public function __construct (PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , ResourceNameCollectionFactoryInterface $ resourceNameCollectionFactory , ResourceMetadataFactoryInterface $ resourceMetadataFactory , ResolverFactoryInterface $ collectionResolverFactory , ResolverFactoryInterface $ itemMutationResolverFactory , callable $ itemResolver , callable $ defaultFieldResolver , TypesFactoryInterface $ typesFactory , ContainerInterface $ filterLocator = null , bool $ paginationEnabled = true )
63+ public function __construct (PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , ResourceNameCollectionFactoryInterface $ resourceNameCollectionFactory , ResourceMetadataFactoryInterface $ resourceMetadataFactory , ResolverFactoryInterface $ collectionResolverFactory , ResolverFactoryInterface $ itemMutationResolverFactory , callable $ itemResolver , callable $ defaultFieldResolver , ContainerInterface $ queryResolverLocator , TypesFactoryInterface $ typesFactory , ContainerInterface $ filterLocator = null , bool $ paginationEnabled = true )
6364 {
6465 $ this ->propertyNameCollectionFactory = $ propertyNameCollectionFactory ;
6566 $ this ->propertyMetadataFactory = $ propertyMetadataFactory ;
@@ -69,6 +70,7 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
6970 $ this ->itemResolver = $ itemResolver ;
7071 $ this ->itemMutationResolverFactory = $ itemMutationResolverFactory ;
7172 $ this ->defaultFieldResolver = $ defaultFieldResolver ;
73+ $ this ->queryResolverLocator = $ queryResolverLocator ;
7274 $ this ->typesFactory = $ typesFactory ;
7375 $ this ->filterLocator = $ filterLocator ;
7476 $ this ->paginationEnabled = $ paginationEnabled ;
@@ -86,12 +88,28 @@ public function getSchema(): Schema
8688 $ graphqlConfiguration = $ resourceMetadata ->getGraphql () ?? [];
8789 foreach ($ graphqlConfiguration as $ operationName => $ value ) {
8890 if ('query ' === $ operationName ) {
89- $ queryFields += $ this ->getQueryFields ($ resourceClass , $ resourceMetadata );
91+ $ queryFields += $ this ->getQueryFields ($ resourceClass , $ resourceMetadata, $ operationName , [ ' args ' => [ ' id ' => [ ' type ' => GraphQLType:: id ()]]], [] );
9092
9193 continue ;
9294 }
9395
94- $ mutationFields [$ operationName .$ resourceMetadata ->getShortName ()] = $ this ->getMutationFields ($ resourceClass , $ resourceMetadata , $ operationName );
96+ if ($ itemQuery = $ resourceMetadata ->getGraphqlAttribute ($ operationName , 'item_query ' )) {
97+ $ value ['resolve ' ] = $ this ->queryResolverLocator ->get ($ itemQuery );
98+
99+ $ queryFields += $ this ->getQueryFields ($ resourceClass , $ resourceMetadata , $ operationName , $ value , false );
100+
101+ continue ;
102+ }
103+
104+ if ($ collectionQuery = $ resourceMetadata ->getGraphqlAttribute ($ operationName , 'collection_query ' )) {
105+ $ value ['resolve ' ] = $ this ->queryResolverLocator ->get ($ collectionQuery );
106+
107+ $ queryFields += $ this ->getQueryFields ($ resourceClass , $ resourceMetadata , $ operationName , false , $ value );
108+
109+ continue ;
110+ }
111+
112+ $ mutationFields [$ operationName .$ resourceMetadata ->getShortName ()] = $ this ->getMutationField ($ resourceClass , $ resourceMetadata , $ operationName );
95113 }
96114 }
97115
@@ -155,20 +173,24 @@ private function getNodeQueryField(): array
155173
156174 /**
157175 * Gets the query fields of the schema.
176+ *
177+ * @param array|false $itemConfiguration false if not configured
178+ * @param array|false $collectionConfiguration false if not configured
158179 */
159- private function getQueryFields (string $ resourceClass , ResourceMetadata $ resourceMetadata ): array
180+ private function getQueryFields (string $ resourceClass , ResourceMetadata $ resourceMetadata, string $ operationName , $ itemConfiguration , $ collectionConfiguration ): array
160181 {
161182 $ queryFields = [];
162183 $ shortName = $ resourceMetadata ->getShortName ();
163- $ deprecationReason = $ resourceMetadata ->getGraphqlAttribute ('query ' , 'deprecation_reason ' , '' , true );
184+ $ fieldName = lcfirst ('query ' === $ operationName ? $ shortName : $ operationName .$ shortName );
185+
186+ $ deprecationReason = $ resourceMetadata ->getGraphqlAttribute ($ operationName , 'deprecation_reason ' , '' , true );
164187
165- if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration ($ resourceClass , $ resourceMetadata , null , $ deprecationReason , new Type (Type::BUILTIN_TYPE_OBJECT , true , $ resourceClass ), $ resourceClass )) {
166- $ fieldConfiguration ['args ' ] += ['id ' => ['type ' => GraphQLType::id ()]];
167- $ queryFields [lcfirst ($ shortName )] = $ fieldConfiguration ;
188+ if (false !== $ itemConfiguration && $ fieldConfiguration = $ this ->getResourceFieldConfiguration ($ resourceClass , $ resourceMetadata , null , $ deprecationReason , new Type (Type::BUILTIN_TYPE_OBJECT , true , $ resourceClass ), $ resourceClass )) {
189+ $ queryFields [$ fieldName ] = array_merge ($ fieldConfiguration , $ itemConfiguration );
168190 }
169191
170- 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 )) {
171- $ queryFields [lcfirst ( Inflector::pluralize ($ shortName )) ] = $ fieldConfiguration ;
192+ if (false !== $ collectionConfiguration && $ 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 )) {
193+ $ queryFields [Inflector::pluralize ($ fieldName ) ] = array_merge ( $ fieldConfiguration, $ collectionConfiguration ) ;
172194 }
173195
174196 return $ queryFields ;
@@ -177,7 +199,7 @@ private function getQueryFields(string $resourceClass, ResourceMetadata $resourc
177199 /**
178200 * Gets the mutation field for the given operation name.
179201 */
180- private function getMutationFields (string $ resourceClass , ResourceMetadata $ resourceMetadata , string $ mutationName ): array
202+ private function getMutationField (string $ resourceClass , ResourceMetadata $ resourceMetadata , string $ mutationName ): array
181203 {
182204 $ shortName = $ resourceMetadata ->getShortName ();
183205 $ resourceType = new Type (Type::BUILTIN_TYPE_OBJECT , true , $ resourceClass );
0 commit comments