-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTypesInterface.php
141 lines (128 loc) · 5.07 KB
/
TypesInterface.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
namespace GraphQL\Doctrine;
use Doctrine\ORM\QueryBuilder;
use GraphQL\Doctrine\Definition\EntityIDType;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\ListOfType;
use GraphQL\Type\Definition\NamedType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
/**
* Registry of types to manage all GraphQL types.
*
* This interface purpose is to be able to mock implementations in tests. It is not meant
* to create alternative implementations for production use.
*/
interface TypesInterface
{
/**
* Returns whether a type exists for the given key.
*/
public function has(string $key): bool;
/**
* Always return the same instance of `Type` for the given key.
*
* It will first look for the type in the custom types container, and then
* use automatically generated types. This allow for custom types to override
* automatic ones.
*
* @param string $key the key the type was registered with (eg: "Post", "PostInput", "PostPartialInput" or "PostStatus")
*/
public function get(string $key): Type&NamedType;
/**
* Returns an output type for the given entity.
*
* All entity getter methods will be exposed, unless specified otherwise
* with attributes.
*
* @param class-string $className the class name of an entity (`Post::class`)
*/
public function getOutput(string $className): ObjectType;
/**
* Returns an input type for the given entity.
*
* This would typically be used in mutations to create new entities.
*
* All entity setter methods will be exposed, unless specified otherwise
* with attributes.
*
* @param class-string $className the class name of an entity (`Post::class`)
*/
public function getInput(string $className): InputObjectType;
/**
* Returns a partial input type for the given entity.
*
* This would typically be used in mutations to update existing entities.
*
* All entity setter methods will be exposed, unless specified otherwise
* with attributes. But they will all be marked as optional and without
* default values. So this allow the API client to specify only some fields
* to be updated, and not necessarily all of them at once.
*
* @param class-string $className the class name of an entity (`Post::class`)
*/
public function getPartialInput(string $className): InputObjectType;
/**
* Returns a filter input type for the given entity.
*
* This would typically be used to filter queries.
*
* @param class-string $className the class name of an entity (`Post::class`)
*/
public function getFilter(string $className): InputObjectType;
/**
* Returns a sorting input type for the given entity.
*
* This would typically be used to sort queries.
*
* @param class-string $className the class name of an entity (`Post::class`)
*/
public function getSorting(string $className): ListOfType;
/**
* Returns an special ID type for the given entity.
*
* This is mostly useful for internal usage when a getter has an entity
* as parameter. This type will automatically load the entity from DB, so
* the resolve functions can use a real instance of entity instead of an ID.
* But this can also be used to build your own schema and thus avoid
* manually fetching objects from database for simple cases.
*
* @param class-string $className the class name of an entity (`Post::class`)
*/
public function getId(string $className): EntityIDType;
/**
* Create and return a query builder that is filtered and sorted for the given entity.
*
* Typical usage would be to call this method in your query resolver with the filter and sorting arguments directly
* coming from GraphQL.
*
* You may apply further pagination according to your needs before executing the query.
*
* Filter and sorting arguments are assumed to be valid and complete as the validation should have happened when
* parsing the GraphQL query.
*
* @param class-string $className
*/
public function createFilteredQueryBuilder(string $className, array $filter, array $sorting): QueryBuilder;
/**
* Load a type from its name.
*
* This should be used to declare typeLoader in `GraphQL\Type\Schema` with something similar to:
*
* ```php
* $types = new Types(...);
* $schema = new GraphQL\Type\Schema([
* 'typeLoader' => fn (string $name) => $types->loadType($name, 'Application\Model') ?? $types->loadType($name, 'OtherApplication\Model')
* // ...
* ]);
* ```
*
* While this method could technically replace of uses of dedicated `get*()` methods, we suggest to only use
* `loadType` with the `typeLoader`. Because dedicated `get*()` methods are easier to use, and provide
* stronger typing.
*
* @return null|(NamedType&Type)
*/
public function loadType(string $typeName, string $namespace): ?Type;
}