Skip to content

Commit abeb355

Browse files
authored
Move plugin system to graphql base module. (#364)
1 parent ade2981 commit abeb355

235 files changed

Lines changed: 807 additions & 816 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

graphql.module

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@
66
*/
77
use Drupal\graphql\Utility\StringHelper;
88

9+
define('GRAPHQL_SCALAR_PLUGIN', 'scalar');
10+
define('GRAPHQL_FIELD_PLUGIN', 'field');
11+
define('GRAPHQL_MUTATION_PLUGIN', 'mutation');
12+
define('GRAPHQL_INTERFACE_PLUGIN', 'interface');
13+
define('GRAPHQL_UNION_TYPE_PLUGIN', 'union');
14+
define('GRAPHQL_INPUT_TYPE_PLUGIN', 'input');
15+
define('GRAPHQL_TYPE_PLUGIN', 'type');
16+
define('GRAPHQL_ENUM_PLUGIN', 'enum');
17+
918
/**
1019
* Implements hook_help().
1120
*/
@@ -48,4 +57,74 @@ function graphql_camelcase($components) {
4857
*/
4958
function graphql_propcase($components) {
5059
return StringHelper::propCase($components);
51-
}
60+
}
61+
62+
/**
63+
* Implements hook_graphql_interfaces_alter().
64+
*
65+
* Flatten the interface inheritance tree.
66+
*/
67+
function graphql_graphql_interfaces_alter(&$definitions) {
68+
$interfaces = array_map(function($definition) use ($definitions) {
69+
return graphql_list_interfaces($definitions, $definition);
70+
}, $definitions);
71+
72+
foreach ($interfaces as $index => $list) {
73+
$definition['interfaces'] = $list;
74+
}
75+
}
76+
77+
/**
78+
* Implements hook_graphql_types_alter().
79+
*
80+
* Flatten the interface inheritance tree.
81+
*/
82+
function graphql_graphql_types_alter(&$definitions) {
83+
$interfaceDefinitions = \Drupal::service('plugin.manager.graphql.interface')->getDefinitions();
84+
85+
$interfaces = array_map(function($definition) use ($interfaceDefinitions) {
86+
return graphql_list_interfaces($interfaceDefinitions, $definition);
87+
}, $definitions);
88+
89+
foreach ($interfaces as $index => $list) {
90+
$definitions[$index]['interfaces'] = $list;
91+
}
92+
}
93+
94+
/**
95+
* Get a flattened list of a plugins interface inheritance tree.
96+
*
97+
* @param array $definitions
98+
* The list of interface definitions.
99+
* @param mixed $definition
100+
* A plugin definition.
101+
*
102+
* @return string[]
103+
* A list of interface names.
104+
*/
105+
function graphql_list_interfaces(array &$definitions, $definition) {
106+
$parents = array_filter($definitions, function($parent) use ($definition) {
107+
return in_array($parent['name'], $definition['interfaces']);
108+
});
109+
110+
$interfaces = array_reduce(array_map(function($parent) use ($definitions) {
111+
return graphql_list_interfaces($definitions, $parent);
112+
}, $parents), 'array_merge', $definition['interfaces']);
113+
114+
return $interfaces;
115+
}
116+
117+
/**
118+
* Alter the subrequest payload and add contextual data.
119+
*/
120+
function graphql_graphql_subrequest_alter(&$data, $requirements) {
121+
/** @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface $repository */
122+
$repository = \Drupal::service('graphql.context_repository');
123+
$requiredContexts = array_intersect(array_keys($repository->getAvailableContexts()), $requirements);
124+
$runtimeContexts = $repository->getRuntimeContexts($requiredContexts);
125+
foreach ($requiredContexts as $contextId) {
126+
if (array_key_exists($contextId, $runtimeContexts)) {
127+
$data[$contextId] = $runtimeContexts[$contextId]->getContextValue();
128+
}
129+
}
130+
}

graphql.services.yml

Lines changed: 188 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,23 @@ parameters:
2525
#
2626
# By default, this uses the GraphQL schema provided by the module.
2727
#
28-
# @default \Drupal\graphql\Schema
29-
schema_class: Drupal\graphql\GraphQL\Schema
28+
# @default \Drupal\graphql\GraphQL\Schema\Schema
29+
schema_class: Drupal\graphql\GraphQL\Schema\Schema
3030

3131
services:
32+
# Logger channel for graphql related log entries.
33+
logger.channel.graphql:
34+
parent: logger.channel_base
35+
arguments: ['graphql']
36+
37+
# Check access for executing graphql queries.
3238
access_check.graphql.query:
3339
class: Drupal\graphql\Access\QueryAccessCheck
3440
arguments: ['@request_stack']
3541
tags:
3642
- { name: access_check, applies_to: _graphql_query_access }
43+
44+
# Schema and schema metadata cache bin.
3745
cache.graphql_schema:
3846
class: Drupal\Core\Cache\CacheBackendInterface
3947
tags:
@@ -46,6 +54,8 @@ services:
4654
- { name: cache.bin }
4755
factory: cache_factory:get
4856
arguments: [graphql_schema_metadata]
57+
58+
# Response and response metadata cache bin.
4959
cache.graphql_response:
5060
class: Drupal\Core\Cache\CacheBackendInterface
5161
tags:
@@ -58,18 +68,30 @@ services:
5868
- { name: cache.bin }
5969
factory: cache_factory:get
6070
arguments: [graphql_response_metadata]
71+
72+
# Cache context for graphql request parameters.
6173
cache_context.gql:
6274
class: Drupal\graphql\Cache\Context\QueryCacheContext
6375
arguments: ['@request_stack']
6476
tags:
6577
- { name: cache.context }
78+
79+
# Schema and processor factory.
80+
graphql.query_processor:
81+
class: Drupal\graphql\GraphQL\Execution\QueryProcessor
82+
arguments:
83+
- '@service_container'
84+
- '@graphql.schema'
85+
- '@graphql.reducer_manager'
86+
- '@current_user'
87+
- '%graphql.config%'
6688
graphql.schema:
67-
class: Drupal\graphql\GraphQL\Schema
89+
class: Drupal\graphql\GraphQL\Schema\Schema
6890
factory: ['@graphql.schema_factory', getSchema]
6991
tags:
7092
- { name: graphql_extra_cache_metadata }
7193
graphql.schema_factory:
72-
class: Drupal\graphql\SchemaFactory
94+
class: Drupal\graphql\GraphQL\Schema\SchemaFactory
7395
arguments:
7496
- '@cache_contexts_manager'
7597
- '@request_stack'
@@ -78,22 +100,19 @@ services:
78100
- '@cache.graphql_schema_metadata'
79101
- '%graphql.config%'
80102
tags:
81-
- { name: service_collector, tag: graphql_extra_schema_cache_metadata, call: addExtraCacheMetadata }
82-
graphql.query_processor:
83-
class: Drupal\graphql\QueryProcessor
84-
arguments:
85-
- '@service_container'
86-
- '@graphql.schema'
87-
- '@graphql.reducer_manager'
88-
- '@current_user'
89-
- '%graphql.config%'
103+
- { name: service_collector, tag: graphql_extra_schema_cache_metadata, call: addExtraCacheMetadata }
104+
105+
# Global cache metadata to add to every graphql schema.
106+
# TODO: This needs to be refactored.
90107
graphql.extra_schema_cache_metadata:
91108
class: Drupal\Core\Cache\CacheableMetadata
92109
calls:
93110
- { method: addCacheContexts, arguments: [['languages:language_interface']] }
94111
- { method: addCacheTags, arguments: [['graphql_schema']] }
95112
tags:
96113
- { name: graphql_extra_schema_cache_metadata }
114+
115+
# Composite service for gathering schemas.
97116
graphql.schema_provider:
98117
class: Drupal\graphql\SchemaProvider\SchemaProvider
99118
arguments: ['%graphql.config%']
@@ -103,22 +122,31 @@ services:
103122
class: Drupal\graphql\QueryMapProvider\QueryMapProvider
104123
tags:
105124
- { name: service_collector, tag: graphql_query_map_provider, call: addQueryMapProvider }
125+
126+
# Manager for schema reducers. Uses a compiler pass to gather reducers.
106127
graphql.reducer_manager:
107-
class: Drupal\graphql\Reducers\ReducerManager
128+
class: Drupal\graphql\GraphQL\Reducers\ReducerManager
108129
arguments: ['@service_container', '%graphql.reducers%']
130+
131+
# Service for graphql request caching.
109132
graphql.cache_subscriber:
110133
class: Drupal\graphql\EventSubscriber\CacheSubscriber
111134
arguments: ['@graphql.request_policy', '@graphql.response_policy', '@current_route_match', '@request_stack', '@cache.graphql_response', '@cache.graphql_response_metadata', '@cache_contexts_manager', '%graphql.config%']
112135
tags:
113136
- { name: service_collector, tag: graphql_extra_cache_metadata, call: addExtraCacheMetadata }
114137
- { name: event_subscriber }
138+
139+
# Global cache metadata to add to every graphql request.
140+
# TODO: This needs to be refactored.
115141
graphql.extra_cache_metadata:
116142
class: Drupal\Core\Cache\CacheableMetadata
117143
calls:
118144
- { method: addCacheContexts, arguments: [['gql', 'user']] }
119145
- { method: addCacheTags, arguments: [['graphql_response']] }
120146
tags:
121147
- { name: graphql_extra_cache_metadata }
148+
149+
# Cache request and response policies for graphql requests.
122150
graphql.request_policy:
123151
class: Drupal\Core\PageCache\ChainRequestPolicy
124152
tags:
@@ -142,12 +170,157 @@ services:
142170
arguments: ['@path_processor_manager']
143171
tags:
144172
- { name: graphql_request_policy }
173+
174+
# Upcasting for graphql query request parameters.
145175
graphql.route_enhancer.query:
146176
class: Drupal\graphql\Routing\QueryRouteEnhancer
147177
arguments: ['@graphql.query_map_provider']
148178
tags:
149179
- { name: route_enhancer }
180+
181+
# Schema introspection service.
150182
graphql.introspection:
151-
class: Drupal\graphql\Introspection
183+
class: Drupal\graphql\GraphQL\Utility\Introspection
152184
arguments: ['@service_container', '@graphql.reducer_manager', '@graphql.schema']
153185

186+
# Support for subrequests.
187+
graphql.subrequest_subscriber:
188+
class: Drupal\graphql\EventSubscriber\SubrequestSubscriber
189+
tags:
190+
- { name: event_subscriber }
191+
graphql.context_repository:
192+
class: Drupal\graphql\GraphQL\Context\ContextRepository
193+
tags:
194+
- { name: service_collector, tag: context_provider, call: addContextProvider }
195+
196+
# Support for deferred / batched field resolvers.
197+
graphql.batched_resolver:
198+
class: Drupal\graphql\GraphQL\Batching\BatchedFieldResolver
199+
200+
# Schema provider based on plugin system.
201+
graphql.schema_provider.pluggable:
202+
class: Drupal\graphql\SchemaProvider\PluggableSchemaProvider
203+
arguments:
204+
- '@graphql.pluggable_schema_manager'
205+
tags:
206+
- { name: graphql_schema_provider }
207+
208+
# Plugin system based schema manager.
209+
graphql.pluggable_schema_manager:
210+
class: Drupal\graphql\Plugin\GraphQL\PluggableSchemaManager
211+
tags:
212+
- { name: service_collector, tag: graphql_plugin_manager, call: addPluginManager }
213+
214+
# Plugin manager implementations for schema types.
215+
plugin.manager.graphql.union_type:
216+
class: Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManager
217+
tags:
218+
- { name: graphql_plugin_manager }
219+
calls:
220+
- [setSchemaManager, ['@graphql.pluggable_schema_manager']]
221+
arguments:
222+
- 'Plugin/GraphQL/UnionTypes'
223+
- '@container.namespaces'
224+
- '@module_handler'
225+
- '\Drupal\graphql\Plugin\GraphQL\Unions\UnionTypePluginBase'
226+
- '\Drupal\graphql\Annotation\GraphQLUnionType'
227+
- 'graphql_union_types'
228+
- '@logger.channel.graphql'
229+
plugin.manager.graphql.interface:
230+
class: Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManager
231+
tags:
232+
- { name: graphql_plugin_manager }
233+
calls:
234+
- [setSchemaManager, ['@graphql.pluggable_schema_manager']]
235+
arguments:
236+
- 'Plugin/GraphQL/Interfaces'
237+
- '@container.namespaces'
238+
- '@module_handler'
239+
- '\Drupal\graphql\Plugin\GraphQL\Interfaces\InterfacePluginBase'
240+
- '\Drupal\graphql\Annotation\GraphQLInterface'
241+
- 'graphql_interfaces'
242+
- '@logger.channel.graphql'
243+
plugin.manager.graphql.type:
244+
class: Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManager
245+
tags:
246+
- { name: graphql_plugin_manager }
247+
calls:
248+
- [setSchemaManager, ['@graphql.pluggable_schema_manager']]
249+
arguments:
250+
- 'Plugin/GraphQL/Types'
251+
- '@container.namespaces'
252+
- '@module_handler'
253+
- '\Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase'
254+
- '\Drupal\graphql\Annotation\GraphQLType'
255+
- 'graphql_types'
256+
- '@logger.channel.graphql'
257+
plugin.manager.graphql.input_type:
258+
class: Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManager
259+
tags:
260+
- { name: graphql_plugin_manager }
261+
calls:
262+
- [setSchemaManager, ['@graphql.pluggable_schema_manager']]
263+
arguments:
264+
- 'Plugin/GraphQL/InputTypes'
265+
- '@container.namespaces'
266+
- '@module_handler'
267+
- '\Drupal\graphql\Plugin\GraphQL\InputTypes\InputTypePluginBase'
268+
- '\Drupal\graphql\Annotation\GraphQLInputType'
269+
- 'graphql_input_types'
270+
- '@logger.channel.graphql'
271+
plugin.manager.graphql.field:
272+
class: Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManager
273+
tags:
274+
- { name: graphql_plugin_manager }
275+
calls:
276+
- [setSchemaManager, ['@graphql.pluggable_schema_manager']]
277+
arguments:
278+
- 'Plugin/GraphQL/Fields'
279+
- '@container.namespaces'
280+
- '@module_handler'
281+
- '\Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase'
282+
- '\Drupal\graphql\Annotation\GraphQLField'
283+
- 'graphql_fields'
284+
- '@logger.channel.graphql'
285+
plugin.manager.graphql.mutation:
286+
class: Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManager
287+
tags:
288+
- { name: graphql_plugin_manager }
289+
calls:
290+
- [setSchemaManager, ['@graphql.pluggable_schema_manager']]
291+
arguments:
292+
- 'Plugin/GraphQL/Mutations'
293+
- '@container.namespaces'
294+
- '@module_handler'
295+
- '\Drupal\graphql\Plugin\GraphQL\Mutations\MutationPluginBase'
296+
- '\Drupal\graphql\Annotation\GraphQLMutation'
297+
- 'graphql_mutations'
298+
- '@logger.channel.graphql'
299+
plugin.manager.graphql.scalar:
300+
class: Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManager
301+
tags:
302+
- { name: graphql_plugin_manager }
303+
calls:
304+
- [setSchemaManager, ['@graphql.pluggable_schema_manager']]
305+
arguments:
306+
- 'Plugin/GraphQL/Scalars'
307+
- '@container.namespaces'
308+
- '@module_handler'
309+
- '\Youshido\GraphQL\Type\TypeInterface'
310+
- '\Drupal\graphql\Annotation\GraphQLScalar'
311+
- 'graphql_scalars'
312+
- '@logger.channel.graphql'
313+
plugin.manager.graphql.enum:
314+
class: Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManager
315+
tags:
316+
- { name: graphql_plugin_manager }
317+
calls:
318+
- [setSchemaManager, ['@graphql.pluggable_schema_manager']]
319+
arguments:
320+
- 'Plugin/GraphQL/Enums'
321+
- '@container.namespaces'
322+
- '@module_handler'
323+
- '\Drupal\graphql\Plugin\GraphQL\Enums\EnumPluginBase'
324+
- '\Drupal\graphql\Annotation\GraphQLEnum'
325+
- 'graphql_enums'
326+
- '@logger.channel.graphql'

0 commit comments

Comments
 (0)