Skip to content

Commit 2e9ca9b

Browse files
authored
Converting schemas to plugins. (#365)
1 parent ded7454 commit 2e9ca9b

83 files changed

Lines changed: 1505 additions & 665 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.links.menu.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
graphql.config_page:
22
title: 'GraphQL'
33
description: 'GraphQL settings and extensions.'
4-
parent: 'system.admin_config_services'
4+
parent: system.admin_config_services
55
route_name: graphql.config_page
6+
7+
graphq.schema_overview:
8+
title: 'Schemas'
9+
description: 'Birds-eye overview of your available schemas.'
10+
parent: graphql.config_page
11+
route_name: graphql.schema_overview

graphql.routing.yml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
graphql.request:
2-
path: '/graphql'
3-
defaults:
4-
_graphql:
5-
single: '\Drupal\graphql\Controller\RequestController::handleRequest'
6-
multiple: '\Drupal\graphql\Controller\RequestController::handleBatchRequest'
7-
requirements:
8-
_graphql_query_access: 'TRUE'
9-
101
graphql.config_page:
112
path: '/admin/config/graphql'
123
defaults:
134
_controller: '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage'
145
_title: 'GraphQL'
156
requirements:
167
_permission: 'administer site configuration'
8+
9+
graphql.schema_overview:
10+
path: '/admin/config/graphql/schemas'
11+
defaults:
12+
_controller: '\Drupal\graphql\Controller\SchemaOverviewController::listSchemas'
13+
_title: 'Schemas'
14+
requirements:
15+
_permission: 'administer site configuration'

graphql.services.yml

Lines changed: 40 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ parameters:
2121
# @default false
2222
development: false
2323

24-
# GraphQL schema class to be used:
25-
#
26-
# By default, this uses the GraphQL schema provided by the module.
27-
#
28-
# @default \Drupal\graphql\GraphQL\Schema\Schema
29-
schema_class: Drupal\graphql\GraphQL\Schema\Schema
30-
3124
services:
3225
# Logger channel for graphql related log entries.
3326
logger.channel.graphql:
@@ -80,44 +73,22 @@ services:
8073
graphql.query_processor:
8174
class: Drupal\graphql\GraphQL\Execution\QueryProcessor
8275
arguments:
83-
- '@service_container'
84-
- '@graphql.schema'
85-
- '@graphql.reducer_manager'
86-
- '@current_user'
87-
- '%graphql.config%'
88-
graphql.schema:
89-
class: Drupal\graphql\GraphQL\Schema\Schema
90-
factory: ['@graphql.schema_factory', getSchema]
91-
tags:
92-
- { name: graphql_extra_cache_metadata }
93-
graphql.schema_factory:
94-
class: Drupal\graphql\GraphQL\Schema\SchemaFactory
76+
- '@service_container'
77+
- '@graphql.reducer_manager'
78+
- '@graphql.schema_loader'
79+
- '@current_user'
80+
- '%graphql.config%'
81+
graphql.schema_loader:
82+
class: Drupal\graphql\GraphQL\Schema\SchemaLoader
9583
arguments:
96-
- '@cache_contexts_manager'
97-
- '@request_stack'
98-
- '@graphql.schema_provider'
99-
- '@cache.graphql_schema'
100-
- '@cache.graphql_schema_metadata'
101-
- '%graphql.config%'
102-
tags:
103-
- { name: service_collector, tag: graphql_extra_schema_cache_metadata, call: addExtraCacheMetadata }
84+
- '@cache_contexts_manager'
85+
- '@request_stack'
86+
- '@plugin.manager.graphql.schema'
87+
- '@cache.graphql_schema'
88+
- '@cache.graphql_schema_metadata'
89+
- '%graphql.config%'
10490

105-
# Global cache metadata to add to every graphql schema.
106-
# TODO: This needs to be refactored.
107-
graphql.extra_schema_cache_metadata:
108-
class: Drupal\Core\Cache\CacheableMetadata
109-
calls:
110-
- { method: addCacheContexts, arguments: [['languages:language_interface']] }
111-
- { method: addCacheTags, arguments: [['graphql_schema']] }
112-
tags:
113-
- { name: graphql_extra_schema_cache_metadata }
114-
115-
# Composite service for gathering schemas.
116-
graphql.schema_provider:
117-
class: Drupal\graphql\SchemaProvider\SchemaProvider
118-
arguments: ['%graphql.config%']
119-
tags:
120-
- { name: service_collector, tag: graphql_schema_provider, call: addSchemaProvider }
91+
# Query map provider using the composite pattern.
12192
graphql.query_map_provider:
12293
class: Drupal\graphql\QueryMapProvider\QueryMapProvider
12394
tags:
@@ -131,20 +102,24 @@ services:
131102
# Service for graphql request caching.
132103
graphql.cache_subscriber:
133104
class: Drupal\graphql\EventSubscriber\CacheSubscriber
134-
arguments: ['@graphql.request_policy', '@graphql.response_policy', '@current_route_match', '@request_stack', '@cache.graphql_response', '@cache.graphql_response_metadata', '@cache_contexts_manager', '%graphql.config%']
105+
arguments:
106+
- '@graphql.request_policy'
107+
- '@graphql.response_policy'
108+
- '@current_route_match'
109+
- '@request_stack'
110+
- '@cache.graphql_response'
111+
- '@cache.graphql_response_metadata'
112+
- '@cache_contexts_manager'
113+
- '@graphql.schema_loader'
114+
- '%graphql.config%'
135115
tags:
136-
- { name: service_collector, tag: graphql_extra_cache_metadata, call: addExtraCacheMetadata }
137116
- { name: event_subscriber }
138117

139-
# Global cache metadata to add to every graphql request.
140-
# TODO: This needs to be refactored.
141-
graphql.extra_cache_metadata:
142-
class: Drupal\Core\Cache\CacheableMetadata
143-
calls:
144-
- { method: addCacheContexts, arguments: [['gql', 'user']] }
145-
- { method: addCacheTags, arguments: [['graphql_response']] }
118+
graphql.query_routes:
119+
class: Drupal\graphql\Routing\QueryRoutes
120+
arguments: ['@plugin.manager.graphql.schema']
146121
tags:
147-
- { name: graphql_extra_cache_metadata }
122+
- { name: 'event_subscriber' }
148123

149124
# Cache request and response policies for graphql requests.
150125
graphql.request_policy:
@@ -181,7 +156,7 @@ services:
181156
# Schema introspection service.
182157
graphql.introspection:
183158
class: Drupal\graphql\GraphQL\Utility\Introspection
184-
arguments: ['@service_container', '@graphql.reducer_manager', '@graphql.schema']
159+
arguments: ['@graphql.query_processor']
185160

186161
# Support for subrequests.
187162
graphql.subrequest_subscriber:
@@ -197,20 +172,23 @@ services:
197172
graphql.batched_resolver:
198173
class: Drupal\graphql\GraphQL\Batching\BatchedFieldResolver
199174

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-
208175
# Plugin system based schema manager.
209176
graphql.pluggable_schema_manager:
210177
class: Drupal\graphql\Plugin\GraphQL\PluggableSchemaManager
211178
tags:
212179
- { name: service_collector, tag: graphql_plugin_manager, call: addPluginManager }
213180

181+
# Plugin manager for schemas
182+
plugin.manager.graphql.schema:
183+
class: Drupal\graphql\Plugin\GraphQL\SchemaPluginManager
184+
arguments:
185+
- 'Plugin/GraphQL/Schemas'
186+
- '@container.namespaces'
187+
- '@module_handler'
188+
- '\Drupal\graphql\Plugin\GraphQL\Schemas\SchemaPluginBase'
189+
- '\Drupal\graphql\Annotation\GraphQLSchema'
190+
- 'graphql_schemas'
191+
214192
# Plugin manager implementations for schema types.
215193
plugin.manager.graphql.union_type:
216194
class: Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManager
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Schemas;
4+
5+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
6+
use Drupal\graphql\Plugin\GraphQL\SchemaPluginInterface;
7+
use Drupal\graphql\Plugin\GraphQL\Schemas\SchemaPluginBase;
8+
use Symfony\Component\DependencyInjection\ContainerInterface;
9+
use Youshido\GraphQL\Config\Schema\SchemaConfig;
10+
use Youshido\GraphQL\Schema\InternalSchemaMutationObject;
11+
use Youshido\GraphQL\Schema\InternalSchemaQueryObject;
12+
13+
/**
14+
* Default generated schema.
15+
*
16+
* @GraphQLSchema(
17+
* id = "default",
18+
* name = "Default",
19+
* path = "/graphql"
20+
* )
21+
*/
22+
class DefaultSchema extends SchemaPluginBase implements SchemaPluginInterface, ContainerFactoryPluginInterface {
23+
24+
/**
25+
* The pluggable schema manager service.
26+
*
27+
* @var \Drupal\graphql\Plugin\GraphQL\PluggableSchemaManagerInterface
28+
*/
29+
protected $schemaManager;
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
35+
/** @var \Drupal\graphql\Plugin\GraphQL\PluggableSchemaManagerInterface $schemaManager */
36+
$schemaManager = $container->get('graphql.pluggable_schema_manager');
37+
38+
$query = new InternalSchemaQueryObject(['name' => 'RootQuery']);
39+
$query->addFields($schemaManager->getRootFields());
40+
41+
$mutation = new InternalSchemaMutationObject(['name' => 'RootMutation']);
42+
$mutation->addFields($schemaManager->getMutations());
43+
44+
$types = $schemaManager->find(function() {
45+
return TRUE;
46+
}, [
47+
GRAPHQL_UNION_TYPE_PLUGIN,
48+
GRAPHQL_TYPE_PLUGIN,
49+
GRAPHQL_INPUT_TYPE_PLUGIN,
50+
]);
51+
52+
return new static($configuration + ['schema' => [
53+
'query' => $query,
54+
'mutation' => $mutation,
55+
'types' => $types,
56+
]], $plugin_id, $plugin_definition);
57+
}
58+
}

modules/graphql_core/tests/modules/graphql_cache_test/src/Counter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Counter {
1111
*
1212
* @var int
1313
*/
14-
private $count = 0;
14+
protected static $count = 0;
1515

1616
/**
1717
* Increase the counter and return it.
@@ -23,8 +23,8 @@ class Counter {
2323
* The current count value.
2424
*/
2525
public function count($amount = 1) {
26-
$this->count += $amount;
27-
return $this->count;
26+
static::$count += $amount;
27+
return static::$count;
2828
}
2929

3030
}

modules/graphql_core/tests/modules/graphql_cache_test/src/Plugin/GraphQL/Fields/A.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
* secure = true,
1818
* type = "Int",
1919
* types = {"Root", "Object"},
20-
* cache_tags = {"a"},
21-
* cache_contexts = {"graphql_test"}
20+
* response_cache_tags = {"a"},
21+
* response_cache_contexts = {"graphql_test"}
2222
* )
2323
*/
2424
class A extends FieldPluginBase implements ContainerFactoryPluginInterface {

modules/graphql_core/tests/modules/graphql_cache_test/src/Plugin/GraphQL/Fields/B.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* name = "b",
1212
* type = "Int",
1313
* types = {"Root", "Object"},
14-
* cache_tags = {"b"},
15-
* cache_contexts = {"graphql_test"}
14+
* response_cache_tags = {"b"},
15+
* response_cache_contexts = {"graphql_test"}
1616
* )
1717
*/
1818
class B extends A {

modules/graphql_core/tests/modules/graphql_cache_test/src/Plugin/GraphQL/Fields/C.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* name = "c",
1212
* type = "Int",
1313
* types = {"Root"},
14-
* cache_tags = {"c"},
15-
* cache_contexts = {"graphql_test_root_field"}
14+
* response_cache_tags = {"c"},
15+
* response_cache_contexts = {"graphql_test_root_field"}
1616
* )
1717
*/
1818
class C extends A {

modules/graphql_core/tests/src/Kernel/EnumTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testEnumAnnotations() {
4949
*/
5050
public function testEnumTypeNames() {
5151
/** @var \Youshido\GraphQL\Schema\AbstractSchema $schema */
52-
$schema = \Drupal::service('graphql.schema');
52+
$schema = \Drupal::service('plugin.manager.graphql.schema')->createInstance('default');
5353
$types = TypeCollector::collectTypes($schema);
5454
foreach ($types as $type) {
5555
if ($type instanceof EnumType && $type->getName() === NULL) {

modules/graphql_core/tests/src/Kernel/LanguageTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Drupal\Tests\graphql_core\Kernel;
44

55
use Drupal\language\Entity\ConfigurableLanguage;
6+
use Drupal\Tests\graphql\Traits\ByPassAccessTrait;
67
use Drupal\Tests\graphql_core\Traits\GraphQLFileTestTrait;
78
use Drupal\Tests\language\Kernel\LanguageTestBase;
89

@@ -13,6 +14,7 @@
1314
*/
1415
class LanguageTest extends LanguageTestBase {
1516
use GraphQLFileTestTrait;
17+
use ByPassAccessTrait;
1618

1719
/**
1820
* {@inheritdoc}

0 commit comments

Comments
 (0)