Skip to content

Commit 417c308

Browse files
committed
WIP
1 parent ce2bc1f commit 417c308

File tree

5 files changed

+229
-83
lines changed

5 files changed

+229
-83
lines changed

src/GraphQL/Utility/AST.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\graphql\GraphQL\Utility;
6+
7+
use GraphQL\Language\AST\DocumentNode;
8+
use GraphQL\Language\AST\NodeList;
9+
use GraphQL\Utils\AST as ASTBase;
10+
11+
/**
12+
* Forward port of GraphQL 15 function in AST class.
13+
*
14+
* @todo Remove when upgrading to GraphQL v15.
15+
* @internal
16+
*/
17+
class AST extends ASTBase {
18+
19+
/**
20+
* Provided a collection of ASTs, presumably each from different files,
21+
* concatenate the ASTs together into batched AST, useful for validating many
22+
* GraphQL source files which together represent one conceptual application.
23+
*
24+
* @param array<\GraphQL\Language\AST\DocumentNode> $documents
25+
*
26+
* @api
27+
*/
28+
public static function concatAST(array $documents): DocumentNode {
29+
/** @var array<int, \GraphQL\Language\AST\Node&\GraphQL\Language\AST\DefinitionNode> $definitions */
30+
$definitions = [];
31+
foreach ($documents as $document) {
32+
foreach ($document->definitions as $definition) {
33+
$definitions[] = $definition;
34+
}
35+
}
36+
37+
return new DocumentNode(['definitions' => new NodeList($definitions)]);
38+
}
39+
40+
}

src/Plugin/GraphQL/Schema/AlterableComposableSchema.php

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
use Drupal\Core\Extension\ModuleHandlerInterface;
88
use Drupal\graphql\Event\AlterSchemaDataEvent;
99
use Drupal\graphql\Event\AlterSchemaExtensionDataEvent;
10-
use Drupal\graphql\Plugin\SchemaExtensionPluginInterface;
10+
use Drupal\graphql\GraphQL\Utility\AST;
1111
use Drupal\graphql\Plugin\SchemaExtensionPluginManager;
1212
use GraphQL\Language\Parser;
13+
use GraphQL\Language\Source;
1314
use Symfony\Component\DependencyInjection\ContainerInterface;
1415

1516
/**
@@ -112,29 +113,41 @@ public function __construct(
112113
* @see \Drupal\graphql\Plugin\GraphQL\Schema\ComposableSchema::getSchemaDocument()
113114
*/
114115
protected function getSchemaDocument(array $extensions = []) {
115-
// Only use caching of the parsed document if we aren't in development mode.
116-
$cid = "schema:{$this->getPluginId()}";
117-
if (empty($this->inDevelopment) && $cache = $this->astCache->get($cid)) {
118-
return $cache->data;
116+
$baseDefinition = $this->getSchemaDefinition();
117+
if (!$baseDefinition instanceof Source) {
118+
@trigger_error('Returning a ' . get_debug_type($baseDefinition) . ' from `getSchemaDefinition` is deprecated in graphql:4.6 and is disallowed from graphql:5.0.0. Return \GraphQL\Language\Source instead. See https://www.drupal.org/node/', E_USER_DEPRECATED);
119+
$baseDefinition = new Source($baseDefinition);
119120
}
121+
$sources = [$baseDefinition->body];
120122

121-
$extensions = array_filter(array_map(function (SchemaExtensionPluginInterface $extension) {
122-
return $extension->getBaseDefinition();
123-
}, $extensions), function ($definition) {
124-
return !empty($definition);
125-
});
126-
$schema = array_merge([$this->getSchemaDefinition()], $extensions);
123+
foreach ($extensions as $id => $extension) {
124+
$definition = $extension->getBaseDefinition();
125+
if (!$definition instanceof Source && $definition !== NULL) {
126+
@trigger_error('Returning a ' . get_debug_type($definition) . ' from `getBaseDefinition` is deprecated in graphql:4.6 and is disallowed from graphql:5.0.0. Return \GraphQL\Language\Source|NULL instead. See https://www.drupal.org/node/', E_USER_DEPRECATED);
127+
$definition = new Source($definition);
128+
}
129+
130+
if (empty($definition)) {
131+
continue;
132+
}
133+
134+
$sources[$id] = $definition->body;
135+
}
127136
// Event in order to alter the schema data.
128-
$event = new AlterSchemaDataEvent($schema);
137+
$event = new AlterSchemaDataEvent($sources);
129138
$this->dispatcher->dispatch(
130139
$event,
131140
AlterSchemaDataEvent::EVENT_NAME
132141
);
133-
$ast = Parser::parse(implode("\n\n", $event->getSchemaData()));
134-
if (empty($this->inDevelopment)) {
135-
$this->astCache->set($cid, $ast, CacheBackendInterface::CACHE_PERMANENT, ['graphql']);
142+
$documents = [];
143+
foreach ($event->getSchemaData() as $schemaDatum) {
144+
if (empty($schemaDatum)) {
145+
continue;
146+
}
147+
$documents[] = Parser::parse($schemaDatum);
136148
}
137-
return $ast;
149+
150+
return AST::concatAST($documents);
138151
}
139152

140153
/**
@@ -154,30 +167,33 @@ protected function getSchemaDocument(array $extensions = []) {
154167
* @see \Drupal\graphql\Plugin\GraphQL\Schema\ComposableSchema::getSchemaDocument()
155168
*/
156169
protected function getExtensionDocument(array $extensions = []) {
157-
// Only use caching of the parsed document if we aren't in development mode.
158-
$cid = "extension:{$this->getPluginId()}";
159-
if (empty($this->inDevelopment) && $cache = $this->astCache->get($cid)) {
160-
return $cache->data;
170+
foreach ($extensions as $id => $extension) {
171+
$definition = $extension->getExtensionDefinition();
172+
if (!$definition instanceof Source && $definition !== NULL) {
173+
@trigger_error('Returning a ' . get_debug_type($definition) . ' from `getExtensionDefinition` is deprecated in graphql:4.6 and is disallowed from graphql:5.0.0. Return \GraphQL\Language\Source|NULL instead. See https://www.drupal.org/node/', E_USER_DEPRECATED);
174+
$definition = new Source($definition);
175+
}
176+
177+
if (empty($definition)) {
178+
continue;
179+
}
180+
181+
$sources[$id] = $definition->body;
161182
}
162183

163-
$extensions = array_filter(array_map(function (SchemaExtensionPluginInterface $extension) {
164-
return $extension->getExtensionDefinition();
165-
}, $extensions), function ($definition) {
166-
return !empty($definition);
167-
});
168184

169185
// Event in order to alter the schema extension data.
170-
$event = new AlterSchemaExtensionDataEvent($extensions);
186+
$event = new AlterSchemaExtensionDataEvent($sources);
171187
$this->dispatcher->dispatch(
172188
$event,
173189
AlterSchemaExtensionDataEvent::EVENT_NAME
174190
);
175-
$ast = !empty($extensions) ? Parser::parse(implode("\n\n", $event->getSchemaExtensionData())) : NULL;
176-
if (empty($this->inDevelopment)) {
177-
$this->astCache->set($cid, $ast, CacheBackendInterface::CACHE_PERMANENT, ['graphql']);
191+
$documents = [];
192+
foreach ($event->getSchemaExtensionData() as $schemaDatum) {
193+
$documents[] = Parser::parse($schemaDatum);
178194
}
179195

180-
return $ast;
196+
return AST::concatAST($documents);
181197
}
182198

183199
}

0 commit comments

Comments
 (0)