@@ -54,12 +54,13 @@ final class SchemaBuilder implements SchemaBuilderInterface
54
54
private $ itemResolver ;
55
55
private $ itemMutationResolverFactory ;
56
56
private $ defaultFieldResolver ;
57
+ private $ queryResolverLocator ;
57
58
private $ filterLocator ;
58
59
private $ typesFactory ;
59
60
private $ paginationEnabled ;
60
61
private $ graphqlTypes = [];
61
62
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 )
63
64
{
64
65
$ this ->propertyNameCollectionFactory = $ propertyNameCollectionFactory ;
65
66
$ this ->propertyMetadataFactory = $ propertyMetadataFactory ;
@@ -69,6 +70,7 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
69
70
$ this ->itemResolver = $ itemResolver ;
70
71
$ this ->itemMutationResolverFactory = $ itemMutationResolverFactory ;
71
72
$ this ->defaultFieldResolver = $ defaultFieldResolver ;
73
+ $ this ->queryResolverLocator = $ queryResolverLocator ;
72
74
$ this ->typesFactory = $ typesFactory ;
73
75
$ this ->filterLocator = $ filterLocator ;
74
76
$ this ->paginationEnabled = $ paginationEnabled ;
@@ -86,12 +88,28 @@ public function getSchema(): Schema
86
88
$ graphqlConfiguration = $ resourceMetadata ->getGraphql () ?? [];
87
89
foreach ($ graphqlConfiguration as $ operationName => $ value ) {
88
90
if ('query ' === $ operationName ) {
89
- $ queryFields += $ this ->getQueryFields ($ resourceClass , $ resourceMetadata );
91
+ $ queryFields += $ this ->getQueryFields ($ resourceClass , $ resourceMetadata, $ operationName , [ ' args ' => [ ' id ' => [ ' type ' => GraphQLType:: id ()]]], [] );
90
92
91
93
continue ;
92
94
}
93
95
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 );
95
113
}
96
114
}
97
115
@@ -155,20 +173,24 @@ private function getNodeQueryField(): array
155
173
156
174
/**
157
175
* 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
158
179
*/
159
- private function getQueryFields (string $ resourceClass , ResourceMetadata $ resourceMetadata ): array
180
+ private function getQueryFields (string $ resourceClass , ResourceMetadata $ resourceMetadata, string $ operationName , $ itemConfiguration , $ collectionConfiguration ): array
160
181
{
161
182
$ queryFields = [];
162
183
$ 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 );
164
187
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 );
168
190
}
169
191
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 ) ;
172
194
}
173
195
174
196
return $ queryFields ;
@@ -177,7 +199,7 @@ private function getQueryFields(string $resourceClass, ResourceMetadata $resourc
177
199
/**
178
200
* Gets the mutation field for the given operation name.
179
201
*/
180
- private function getMutationFields (string $ resourceClass , ResourceMetadata $ resourceMetadata , string $ mutationName ): array
202
+ private function getMutationField (string $ resourceClass , ResourceMetadata $ resourceMetadata , string $ mutationName ): array
181
203
{
182
204
$ shortName = $ resourceMetadata ->getShortName ();
183
205
$ resourceType = new Type (Type::BUILTIN_TYPE_OBJECT , true , $ resourceClass );
0 commit comments