|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers; |
|
5
|
|
|
|
|
6
|
|
|
use GraphQL\Type\Definition\ObjectType; |
|
7
|
|
|
use GraphQL\Type\Definition\Type; |
|
8
|
|
|
use GraphQL\Type\SchemaConfig; |
|
9
|
|
|
use TheCodingMachine\GraphQL\Controllers\Containers\BasicAutoWiringContainer; |
|
10
|
|
|
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface; |
|
11
|
|
|
use TheCodingMachine\GraphQL\Controllers\Types\CustomTypesRegistry; |
|
12
|
|
|
use TheCodingMachine\GraphQL\Controllers\Types\TypeResolver; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A GraphQL schema that takes into constructor argument a QueryProvider. |
|
16
|
|
|
* |
|
17
|
|
|
* TODO: turn this into a SchemaFactory (cleaner than extending a class) |
|
18
|
|
|
*/ |
|
19
|
|
|
class Schema extends \GraphQL\Type\Schema |
|
20
|
|
|
{ |
|
21
|
|
|
public function __construct(QueryProviderInterface $queryProvider, RecursiveTypeMapperInterface $recursiveTypeMapper, TypeResolver $typeResolver, SchemaConfig $config = null) |
|
22
|
|
|
{ |
|
23
|
|
|
if ($config === null) { |
|
24
|
|
|
$config = SchemaConfig::create(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$query = new ObjectType([ |
|
28
|
|
|
'name' => 'Query', |
|
29
|
|
|
'fields' => function() use ($queryProvider) { |
|
30
|
|
|
$queries = $queryProvider->getQueries(); |
|
31
|
|
|
if (empty($queries)) { |
|
32
|
|
|
return [ |
|
33
|
|
|
'dummyQuery' => [ |
|
34
|
|
|
'type' => Type::string(), |
|
35
|
|
|
'description' => 'A placeholder query used by thecodingmachine/graphql-controllers when there are no declared queries.', |
|
36
|
|
|
'resolve' => function () { |
|
37
|
|
|
return 'This is a placeholder query. Please create a query using the @Query annotation.'; |
|
38
|
|
|
} |
|
39
|
|
|
] |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
return $queries; |
|
43
|
|
|
} |
|
44
|
|
|
]); |
|
45
|
|
|
$mutation = new ObjectType([ |
|
46
|
|
|
'name' => 'Mutation', |
|
47
|
|
|
'fields' => function() use ($queryProvider) { |
|
48
|
|
|
$mutations = $queryProvider->getMutations(); |
|
49
|
|
|
if (empty($mutations)) { |
|
50
|
|
|
return [ |
|
51
|
|
|
'dummyMutation' => [ |
|
52
|
|
|
'type' => Type::string(), |
|
53
|
|
|
'description' => 'A placeholder query used by thecodingmachine/graphql-controllers when there are no declared mutations.', |
|
54
|
|
|
'resolve' => function () { |
|
55
|
|
|
return 'This is a placeholder mutation. Please create a mutation using the @Mutation annotation.'; |
|
56
|
|
|
} |
|
57
|
|
|
] |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
return $mutations; |
|
61
|
|
|
} |
|
62
|
|
|
]); |
|
63
|
|
|
|
|
64
|
|
|
$config->setQuery($query); |
|
65
|
|
|
$config->setMutation($mutation); |
|
66
|
|
|
|
|
67
|
|
|
$config->setTypes(function() use ($recursiveTypeMapper) { |
|
68
|
|
|
return $recursiveTypeMapper->getOutputTypes(); |
|
69
|
|
|
}); |
|
70
|
|
|
|
|
71
|
|
|
$config->setTypeLoader(function(string $name) use ($recursiveTypeMapper, $query, $mutation) { |
|
72
|
|
|
// We need to find a type FROM a GraphQL type name |
|
73
|
|
|
if ($name === 'Query') { |
|
74
|
|
|
return $query; |
|
75
|
|
|
} |
|
76
|
|
|
if ($name === 'Mutation') { |
|
77
|
|
|
return $mutation; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$type = CustomTypesRegistry::mapNameToType($name); |
|
81
|
|
|
if ($type !== null) { |
|
82
|
|
|
return $type; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $recursiveTypeMapper->mapNameToType($name); |
|
86
|
|
|
}); |
|
87
|
|
|
|
|
88
|
|
|
$typeResolver->registerSchema($this); |
|
89
|
|
|
|
|
90
|
|
|
parent::__construct($config); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|