29
29
use GraphQL \Type \Definition \WrappingType ;
30
30
use GraphQL \Type \Schema ;
31
31
use Symfony \Component \Config \Definition \Exception \InvalidTypeException ;
32
- use Symfony \Component \HttpFoundation \Request ;
33
32
use Symfony \Component \PropertyInfo \Type ;
34
33
35
34
/**
@@ -67,9 +66,13 @@ public function __construct(PropertyNameCollectionFactoryInterface $propertyName
67
66
public function getSchema (): Schema
68
67
{
69
68
$ queryFields = [];
69
+ foreach ($ this ->resourceNameCollectionFactory ->create () as $ resourceClass ) {
70
+ $ resourceMetadata = $ this ->resourceMetadataFactory ->create ($ resourceClass );
71
+ if (!isset ($ resourceMetadata ->getGraphql ()['query ' ])) {
72
+ continue ;
73
+ }
70
74
71
- foreach ($ this ->resourceNameCollectionFactory ->create () as $ resource ) {
72
- $ queryFields += $ this ->getQueryFields ($ resource );
75
+ $ queryFields += $ this ->getQueryFields ($ resourceClass , $ resourceMetadata );
73
76
}
74
77
75
78
return new Schema ([
@@ -83,44 +86,41 @@ public function getSchema(): Schema
83
86
/**
84
87
* Gets the query fields of the schema.
85
88
*/
86
- private function getQueryFields (string $ resource ): array
89
+ private function getQueryFields (string $ resourceClass , ResourceMetadata $ resourceMetadata ): array
87
90
{
88
91
$ queryFields = [];
89
- $ resourceMetadata = $ this ->resourceMetadataFactory ->create ($ resource );
90
92
$ shortName = $ resourceMetadata ->getShortName ();
91
93
92
- foreach ($ this ->getOperations ($ resourceMetadata , true , true ) as $ operationName => $ queryItemOperation ) {
93
- $ fieldNamePrefix = 'get ' === $ operationName ? '' : $ operationName ;
94
- if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration (null , new Type (Type::BUILTIN_TYPE_OBJECT , true , $ resource ), $ resource , $ operationName )) {
95
- $ fieldConfiguration ['args ' ] += $ this ->getResourceIdentifiersArgumentsConfiguration ($ resource , $ operationName );
96
- $ queryFields [lcfirst ($ fieldNamePrefix .$ shortName )] = $ fieldConfiguration ;
97
- }
94
+ if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration (null , new Type (Type::BUILTIN_TYPE_OBJECT , true , $ resourceClass ), $ resourceClass )) {
95
+ $ fieldConfiguration ['args ' ] += $ this ->getResourceIdentifiersArgumentsConfiguration ($ resourceClass );
96
+ $ queryFields [lcfirst ($ shortName )] = $ fieldConfiguration ;
98
97
}
99
98
100
- foreach ($ this ->getOperations ($ resourceMetadata , true , false ) as $ operationName => $ queryCollectionOperation ) {
101
- $ fieldNamePrefix = 'get ' === $ operationName ? '' : $ operationName ;
102
- if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration (null , new Type (Type::BUILTIN_TYPE_OBJECT , false , null , true , null , new Type (Type::BUILTIN_TYPE_OBJECT , false , $ resource )), $ resource , $ operationName )) {
103
- $ queryFields [lcfirst ($ fieldNamePrefix .Inflector::pluralize ($ shortName ))] = $ fieldConfiguration ;
104
- }
99
+ if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration (null , new Type (Type::BUILTIN_TYPE_OBJECT , false , null , true , null , new Type (Type::BUILTIN_TYPE_OBJECT , false , $ resourceClass )), $ resourceClass )) {
100
+ $ queryFields [lcfirst (Inflector::pluralize ($ shortName ))] = $ fieldConfiguration ;
105
101
}
106
102
107
103
return $ queryFields ;
108
104
}
109
105
110
106
/**
111
- * Get the field configuration of a resource.
107
+ * Gets the field configuration of a resource.
112
108
*
113
109
* @see http://webonyx.github.io/graphql-php/type-system/object-types/
114
110
*
115
111
* @return array|null
116
112
*/
117
- private function getResourceFieldConfiguration (string $ fieldDescription = null , Type $ type , string $ rootResource , string $ operationName , bool $ isInput = false )
113
+ private function getResourceFieldConfiguration (string $ fieldDescription = null , Type $ type , string $ rootResource , bool $ isInput = false )
118
114
{
119
115
try {
120
- $ graphqlType = $ this ->convertType ($ type , $ operationName , $ isInput );
116
+ $ graphqlType = $ this ->convertType ($ type , $ isInput );
121
117
$ graphqlWrappedType = $ graphqlType instanceof WrappingType ? $ graphqlType ->getWrappedType () : $ graphqlType ;
122
118
$ isInternalGraphqlType = in_array ($ graphqlWrappedType , GraphQLType::getInternalTypes (), true );
123
- $ className = $ isInternalGraphqlType ? '' : ($ type ->isCollection () ? $ type ->getCollectionValueType ()->getClassName () : $ type ->getClassName ());
119
+ if ($ isInternalGraphqlType ) {
120
+ $ className = '' ;
121
+ } else {
122
+ $ className = $ type ->isCollection () ? $ type ->getCollectionValueType ()->getClassName () : $ type ->getClassName ();
123
+ }
124
124
125
125
$ args = [];
126
126
if ($ this ->paginationEnabled && !$ isInternalGraphqlType && $ type ->isCollection () && !$ isInput ) {
@@ -139,7 +139,7 @@ private function getResourceFieldConfiguration(string $fieldDescription = null,
139
139
if ($ isInternalGraphqlType || $ isInput ) {
140
140
$ resolve = null ;
141
141
} else {
142
- $ resolve = $ type ->isCollection () ? $ this ->collectionResolverFactory ->createCollectionResolver ($ className , $ rootResource, $ operationName ) : $ this ->itemResolverFactory ->createItemResolver ($ className , $ rootResource, $ operationName );
142
+ $ resolve = $ type ->isCollection () ? $ this ->collectionResolverFactory ->createCollectionResolver ($ className , $ rootResource ) : $ this ->itemResolverFactory ->createItemResolver ($ className , $ rootResource );
143
143
}
144
144
145
145
return [
@@ -158,21 +158,21 @@ private function getResourceFieldConfiguration(string $fieldDescription = null,
158
158
*
159
159
* @throws \LogicException
160
160
*/
161
- private function getResourceIdentifiersArgumentsConfiguration (string $ resource , string $ operationName ): array
161
+ private function getResourceIdentifiersArgumentsConfiguration (string $ resourceClass ): array
162
162
{
163
163
$ arguments = [];
164
- $ identifiers = $ this ->identifiersExtractor ->getIdentifiersFromResourceClass ($ resource );
164
+ $ identifiers = $ this ->identifiersExtractor ->getIdentifiersFromResourceClass ($ resourceClass );
165
165
foreach ($ identifiers as $ identifier ) {
166
- $ propertyMetadata = $ this ->propertyMetadataFactory ->create ($ resource , $ identifier );
166
+ $ propertyMetadata = $ this ->propertyMetadataFactory ->create ($ resourceClass , $ identifier );
167
167
$ propertyType = $ propertyMetadata ->getType ();
168
168
if (null === $ propertyType ) {
169
169
continue ;
170
170
}
171
171
172
- $ arguments [$ identifier ] = $ this ->getResourceFieldConfiguration ($ propertyMetadata ->getDescription (), $ propertyType , $ resource , $ operationName , true );
172
+ $ arguments [$ identifier ] = $ this ->getResourceFieldConfiguration ($ propertyMetadata ->getDescription (), $ propertyType , $ resourceClass , true );
173
173
}
174
174
if (!$ arguments ) {
175
- throw new \LogicException ("Missing identifier field for resource \"$ resource \". " );
175
+ throw new \LogicException ("Missing identifier field for resource \"$ resourceClass \". " );
176
176
}
177
177
178
178
return $ arguments ;
@@ -183,7 +183,7 @@ private function getResourceIdentifiersArgumentsConfiguration(string $resource,
183
183
*
184
184
* @throws InvalidTypeException
185
185
*/
186
- private function convertType (Type $ type , string $ operationName , bool $ isInput = false ): GraphQLType
186
+ private function convertType (Type $ type , bool $ isInput = false ): GraphQLType
187
187
{
188
188
switch ($ type ->getBuiltinType ()) {
189
189
case Type::BUILTIN_TYPE_BOOL :
@@ -211,7 +211,7 @@ private function convertType(Type $type, string $operationName, bool $isInput =
211
211
throw new InvalidTypeException ();
212
212
}
213
213
214
- $ graphqlType = $ this ->getResourceObjectType ($ className , $ resourceMetadata , $ operationName , $ isInput );
214
+ $ graphqlType = $ this ->getResourceObjectType ($ className , $ resourceMetadata , $ isInput );
215
215
break ;
216
216
default :
217
217
throw new InvalidTypeException ();
@@ -229,19 +229,18 @@ private function convertType(Type $type, string $operationName, bool $isInput =
229
229
*
230
230
* @return ObjectType|InputObjectType
231
231
*/
232
- private function getResourceObjectType (string $ resource , ResourceMetadata $ resourceMetadata , string $ operationName , bool $ isInput = false )
232
+ private function getResourceObjectType (string $ resource , ResourceMetadata $ resourceMetadata , bool $ isInput = false )
233
233
{
234
234
$ shortName = $ resourceMetadata ->getShortName ().($ isInput ? 'Input ' : '' );
235
-
236
235
if (isset ($ this ->resourceTypesCache [$ shortName ])) {
237
236
return $ this ->resourceTypesCache [$ shortName ];
238
237
}
239
238
240
239
$ configuration = [
241
240
'name ' => $ shortName ,
242
241
'description ' => $ resourceMetadata ->getDescription (),
243
- 'fields ' => function () use ($ resource , $ operationName , $ isInput ) {
244
- return $ this ->getResourceObjectTypeFields ($ resource , $ operationName , $ isInput );
242
+ 'fields ' => function () use ($ resource , $ isInput ) {
243
+ return $ this ->getResourceObjectTypeFields ($ resource , $ isInput );
245
244
},
246
245
];
247
246
@@ -251,18 +250,16 @@ private function getResourceObjectType(string $resource, ResourceMetadata $resou
251
250
/**
252
251
* Gets the fields of the type of the given resource.
253
252
*/
254
- private function getResourceObjectTypeFields (string $ resource , string $ operationName , bool $ isInput = false ): array
253
+ private function getResourceObjectTypeFields (string $ resource , bool $ isInput = false ): array
255
254
{
256
255
$ fields = [];
257
-
258
256
foreach ($ this ->propertyNameCollectionFactory ->create ($ resource ) as $ property ) {
259
257
$ propertyMetadata = $ this ->propertyMetadataFactory ->create ($ resource , $ property );
260
- if (null === ($ propertyType = $ propertyMetadata ->getType ())
261
- || !$ propertyMetadata ->isReadable ()) {
258
+ if (null === ($ propertyType = $ propertyMetadata ->getType ()) || !$ propertyMetadata ->isReadable ()) {
262
259
continue ;
263
260
}
264
261
265
- if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration ($ propertyMetadata ->getDescription (), $ propertyType , $ resource , $ operationName , $ isInput )) {
262
+ if ($ fieldConfiguration = $ this ->getResourceFieldConfiguration ($ propertyMetadata ->getDescription (), $ propertyType , $ resource , $ isInput )) {
266
263
$ fields [$ property ] = $ fieldConfiguration ;
267
264
}
268
265
}
@@ -286,7 +283,7 @@ private function getResourcePaginatedCollectionType($resourceType, bool $isInput
286
283
}
287
284
288
285
$ edgeObjectTypeConfiguration = [
289
- 'name ' => $ shortName. ' Edge ' ,
286
+ 'name ' => "{ $ shortName} Edge " ,
290
287
'description ' => "Edge of $ shortName. " ,
291
288
'fields ' => [
292
289
'node ' => $ resourceType ,
@@ -295,7 +292,7 @@ private function getResourcePaginatedCollectionType($resourceType, bool $isInput
295
292
];
296
293
$ edgeObjectType = $ isInput ? new InputObjectType ($ edgeObjectTypeConfiguration ) : new ObjectType ($ edgeObjectTypeConfiguration );
297
294
$ pageInfoObjectTypeConfiguration = [
298
- 'name ' => $ shortName. ' PageInfo ' ,
295
+ 'name ' => "{ $ shortName} PageInfo " ,
299
296
'description ' => 'Information about the current page. ' ,
300
297
'fields ' => [
301
298
'endCursor ' => GraphQLType::string (),
@@ -305,37 +302,14 @@ private function getResourcePaginatedCollectionType($resourceType, bool $isInput
305
302
$ pageInfoObjectType = $ isInput ? new InputObjectType ($ pageInfoObjectTypeConfiguration ) : new ObjectType ($ pageInfoObjectTypeConfiguration );
306
303
307
304
$ configuration = [
308
- 'name ' => $ shortName. ' Connection ' ,
305
+ 'name ' => "{ $ shortName} Connection " ,
309
306
'description ' => "Connection for $ shortName. " ,
310
307
'fields ' => [
311
308
'edges ' => GraphQLType::listOf ($ edgeObjectType ),
312
309
'pageInfo ' => GraphQLType::nonNull ($ pageInfoObjectType ),
313
310
],
314
311
];
315
312
316
- return $ this ->resourceTypesCache [$ shortName .'Connection ' ] = $ isInput ? new InputObjectType ($ configuration ) : new ObjectType ($ configuration );
317
- }
318
-
319
- /**
320
- * Get the available operations for a resource.
321
- */
322
- private function getOperations (ResourceMetadata $ resourceMetadata , bool $ isQuery , bool $ isItem ): \Traversable
323
- {
324
- $ operations = $ isItem ? $ resourceMetadata ->getItemOperations () : $ resourceMetadata ->getCollectionOperations ();
325
- if (null === $ operations ) {
326
- return yield from [];
327
- }
328
-
329
- foreach ($ operations as $ operationName => $ operation ) {
330
- if (isset ($ operation ['controller ' ]) || !isset ($ operation ['method ' ])) {
331
- continue ;
332
- }
333
-
334
- if ($ isQuery && Request::METHOD_GET !== $ operation ['method ' ] || !$ isQuery && Request::METHOD_GET === $ operation ['method ' ]) {
335
- continue ;
336
- }
337
-
338
- yield $ operationName => $ operation ;
339
- }
313
+ return $ this ->resourceTypesCache ["{$ shortName }Connection " ] = $ isInput ? new InputObjectType ($ configuration ) : new ObjectType ($ configuration );
340
314
}
341
315
}
0 commit comments