Skip to content

Commit 46a63fd

Browse files
authored
fix(schema): Fix altering schema extensions when they are empty initially (#1398)
1 parent 40a9b80 commit 46a63fd

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed

src/Plugin/GraphQL/Schema/AlterableComposableSchema.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ protected function getExtensionDocument(array $extensions = []) {
169169
$event,
170170
AlterSchemaExtensionDataEvent::EVENT_NAME
171171
);
172-
$ast = !empty($extensions) ? Parser::parse(implode("\n\n", $event->getSchemaExtensionData()), ['noLocation' => TRUE]) : NULL;
172+
$extensions = array_filter($event->getSchemaExtensionData());
173+
$ast = !empty($extensions) ? Parser::parse(implode("\n\n", $extensions), ['noLocation' => TRUE]) : NULL;
173174

174175
// No AST caching here as that will be done in getFullSchemaDocument().
175176
return $ast;

tests/modules/graphql_alterable_schema_test/src/EventSubscriber/GraphQlSubscriber.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,18 @@ public function alterSchemaExtensionData(AlterSchemaExtensionDataEvent $event):
3333
$schemaData = $event->getSchemaExtensionData();
3434
// I do not recommend direct replace, better user parsing or regex.
3535
// But this is an example of what you can do.
36-
$schemaData['graphql_alterable_schema_test'] = str_replace('position: Int', 'position: Int!', $schemaData['graphql_alterable_schema_test']);
36+
$schemaData['graphql_alterable_schema_test'] = str_replace('position: Int', 'position: Int!', $schemaData['graphql_alterable_schema_test'] ?? '');
37+
38+
// Test empty extensions can still extend the schema.
39+
// https://github.com/drupal-graphql/graphql/issues/1395
40+
if (empty($schemaData['graphql_alterable_schema_test'])) {
41+
$schemaData['graphql_alterable_schema_test'] = <<<GQL
42+
extend type Result {
43+
empty: Boolean!
44+
}
45+
GQL;
46+
}
47+
3748
$event->setSchemaExtensionData($schemaData);
3849
}
3950

tests/src/Kernel/AlterableSchemaTest.php

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php
22

3-
namespace Drupal\Tests\graphql\Kernel\Framework;
3+
namespace Drupal\Tests\graphql\Kernel;
44

55
use Drupal\graphql\GraphQL\ResolverRegistry;
66
use Drupal\graphql\Plugin\GraphQL\SchemaExtension\SdlSchemaExtensionPluginBase;
77
use Drupal\graphql\Plugin\SchemaExtensionPluginManager;
8-
use Drupal\Tests\graphql\Kernel\GraphQLTestBase;
98
use Drupal\Tests\graphql\Kernel\Schema\AlterableComposableTestSchema;
109

1110
/**
@@ -104,6 +103,40 @@ public function testSchemaExtensionAlteredQueryResultPropertyToNonNull(): void {
104103
], json_decode($result->getContent(), TRUE));
105104
}
106105

106+
/**
107+
* Test if schema extension altering is working with empty extensions.
108+
*/
109+
public function testEmptySchemaExtensionAlteredQueryResultPropertyAdded(): void {
110+
$result = $this->query('query { alterableQuery(id: 1) { id, empty } }');
111+
$this->assertSame(200, $result->getStatusCode());
112+
// Here should be error that query result empty variable cannot be null.
113+
// This leads to the internal server error with reference to the variable.
114+
$this->assertSame([
115+
'errors' => [
116+
0 => [
117+
'message' => 'Internal server error',
118+
'extensions' => [
119+
'category' => 'internal',
120+
],
121+
'locations' => [
122+
0 => [
123+
'line' => 1,
124+
'column' => 37,
125+
],
126+
],
127+
'path' => [
128+
'alterableQuery',
129+
// Reference to our variable in the error.
130+
'empty',
131+
],
132+
],
133+
],
134+
'data' => [
135+
'alterableQuery' => NULL,
136+
],
137+
], json_decode($result->getContent(), TRUE));
138+
}
139+
107140
/**
108141
* {@inheritdoc}
109142
*/
@@ -124,15 +157,24 @@ protected function mockSchema($id, $schema, array $extensions = []): void {
124157
->method('getBaseDefinition')
125158
->willReturn('');
126159

127-
$extensions['graphql_alterable_schema_test']->expects(static::any())
128-
->method('getExtensionDefinition')
129-
->willReturn(
130-
<<<GQL
160+
// Different extension definition for different tests.
161+
switch ($this->getName()) {
162+
case 'testEmptySchemaExtensionAlteredQueryResultPropertyAdded':
163+
$extensionDefinition = '';
164+
break;
165+
166+
default:
167+
$extensionDefinition = <<<GQL
131168
extend type Result {
132169
position: Int
133170
}
134-
GQL
135-
);
171+
GQL;
172+
break;
173+
}
174+
175+
$extensions['graphql_alterable_schema_test']->expects(static::any())
176+
->method('getExtensionDefinition')
177+
->willReturn($extensionDefinition);
136178

137179
$extensionManager->expects(static::any())
138180
->method('getExtensions')

0 commit comments

Comments
 (0)