-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: added directives to extensions #1216
Open
blazeyo
wants to merge
6
commits into
drupal-graphql:8.x-4.x
Choose a base branch
from
blazeyo:enable-directives-in-extensions
base: 8.x-4.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
df4d52c
feat: added directives to extensions
blazeyo 4760e44
chore: make phpstan happy
blazeyo 5204494
chore: make phpstan happier
blazeyo bb0dcb9
chore: make phpstan even happier
blazeyo 96aa7b3
chore: smiling happy people holding hands
blazeyo 6b4c105
chore: fixed a syntax error in php 7.2
blazeyo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Drupal\graphql\GraphQL; | ||
|
||
/** | ||
* Interface for Schema extensions that provide new directives. | ||
* | ||
* @package Drupal\graphql\GraphQL | ||
*/ | ||
interface DirectiveProviderExtensionInterface { | ||
|
||
/** | ||
* Retrieve all directive definitions as a string. | ||
* | ||
* @return string | ||
* Directive definitions in SDL. | ||
*/ | ||
public function getDirectiveDefinitions() : string; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Drupal\graphql\GraphQL; | ||
|
||
use GraphQL\Language\AST\DocumentNode; | ||
|
||
/** | ||
* Interface for schema extensions that need to inspect the host schema. | ||
* | ||
* @package Drupal\graphql\GraphQL | ||
*/ | ||
interface ParentAwareSchemaExtensionInterface { | ||
|
||
/** | ||
* Pass the parent schema document to the extension. | ||
* | ||
* @param \GraphQL\Language\AST\DocumentNode $document | ||
* The parent schema document. | ||
*/ | ||
public function setParentSchemaDocument(DocumentNode $document): void; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...modules/graphql_extension_directives_test/graphql/extension_directives_test.base.graphqls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type Car @dimensions(includeDepth: true) { | ||
brand: String! | ||
model: String! | ||
} |
3 changes: 3 additions & 0 deletions
3
...es/graphql_extension_directives_test/graphql/extension_directives_test.extension.graphqls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
extend type Query { | ||
cars: [Car] | ||
} |
7 changes: 7 additions & 0 deletions
7
tests/modules/graphql_extension_directives_test/graphql_extension_directives_test.info.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: Graphql Extension Directives Test | ||
type: module | ||
description: 'Test module for directives provided by extensions.' | ||
package: Testing | ||
core_version_requirement: ^8 || ^9 | ||
dependencies: | ||
- graphql:graphql |
135 changes: 135 additions & 0 deletions
135
...ctives_test/src/Plugin/GraphQL/SchemaExtension/ExtensionDirectivesTestSchemaExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
namespace Drupal\graphql_extension_directives_test\Plugin\GraphQL\SchemaExtension; | ||
|
||
use Drupal\graphql\GraphQL\DirectiveProviderExtensionInterface; | ||
use Drupal\graphql\GraphQL\ParentAwareSchemaExtensionInterface; | ||
use Drupal\graphql\GraphQL\ResolverBuilder; | ||
use Drupal\graphql\GraphQL\ResolverRegistryInterface; | ||
use Drupal\graphql\Plugin\GraphQL\SchemaExtension\SdlSchemaExtensionPluginBase; | ||
use GraphQL\Language\AST\ObjectTypeDefinitionNode; | ||
|
||
/** | ||
* Schema extension plugin. | ||
* | ||
* @SchemaExtension( | ||
* id = "extension_directives_test", | ||
* name = "Extension Directives Test", | ||
* description = "Provides resolvers for testing extension directives.", | ||
* schema = "composable" | ||
* ) | ||
*/ | ||
class ExtensionDirectivesTestSchemaExtension extends SdlSchemaExtensionPluginBase implements ParentAwareSchemaExtensionInterface, DirectiveProviderExtensionInterface { | ||
|
||
/** | ||
* The parent schema's AST. | ||
* | ||
* @var \GraphQL\Language\AST\DocumentNode | ||
*/ | ||
protected $parentAst; | ||
|
||
/** | ||
* Stores found types with the dimensions directive. | ||
* | ||
* @var array | ||
*/ | ||
protected $typesWithDimensions = []; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setParentSchemaDocument($document): void { | ||
$this->parentAst = $document; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getDirectiveDefinitions(): string { | ||
return <<<GQL | ||
directive @dimensions( | ||
includeDepth: Boolean! | ||
) on OBJECT | ||
GQL; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getExtensionDefinition() { | ||
$schema = []; | ||
$typesWithDimensions = $this->getTypesWithDimensions(); | ||
|
||
foreach ($typesWithDimensions as $typeWithDimensions) { | ||
$schema[] = "extend type $typeWithDimensions[type_name] {"; | ||
$schema[] = " width: String!"; | ||
$schema[] = " height: String!"; | ||
if (isset($typeWithDimensions['args']['includeDepth']) && $typeWithDimensions['args']['includeDepth']) { | ||
$schema[] = " depth: String!"; | ||
} | ||
$schema[] = "}"; | ||
} | ||
|
||
array_unshift($schema, parent::getExtensionDefinition()); | ||
return implode("\n", $schema); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function registerResolvers(ResolverRegistryInterface $registry): void { | ||
$builder = new ResolverBuilder(); | ||
$registry->addFieldResolver('Query', 'cars', $builder->callback(function () { | ||
return [(object) ['brand' => 'Brand', 'model' => 'Model']]; | ||
})); | ||
foreach ($this->getTypesWithDimensions() as $typeWithDimensions) { | ||
$registry->addFieldResolver($typeWithDimensions['type_name'], 'width', $builder->callback(function () { | ||
return '1'; | ||
})); | ||
$registry->addFieldResolver($typeWithDimensions['type_name'], 'height', $builder->callback(function () { | ||
return '1'; | ||
})); | ||
if (isset($typeWithDimensions['args']['includeDepth']) && $typeWithDimensions['args']['includeDepth']) { | ||
$registry->addFieldResolver($typeWithDimensions['type_name'], 'depth', $builder->callback(function () { | ||
return '1'; | ||
})); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Retrieve all directive calls in the host schema. | ||
* | ||
* @throws \Drupal\Component\Plugin\Exception\PluginException | ||
*/ | ||
public function getTypesWithDimensions(): array { | ||
if (count($this->typesWithDimensions) === 0) { | ||
// Search for object type definitions ... | ||
foreach ($this->parentAst->definitions->getIterator() as $definition) { | ||
// ... that have directives. | ||
if ($definition instanceof ObjectTypeDefinitionNode) { | ||
foreach ($definition->directives->getIterator() as $directive) { | ||
/** @var \GraphQL\Language\AST\DirectiveNode $directive */ | ||
$directiveName = $directive->name->value; | ||
if ($directiveName != 'dimensions') { | ||
continue; | ||
} | ||
$typeName = $definition->name->value; | ||
$args = []; | ||
foreach ($directive->arguments->getIterator() as $arg) { | ||
/** @var \GraphQL\Language\AST\ArgumentNode $arg */ | ||
$args[$arg->name->value] = $arg->value->value; | ||
} | ||
$this->typesWithDimensions[] = [ | ||
'directive_name' => $directiveName, | ||
'type_name' => $typeName, | ||
'args' => $args, | ||
]; | ||
} | ||
} | ||
} | ||
} | ||
return $this->typesWithDimensions; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\graphql\Kernel; | ||
|
||
/** | ||
* Test the entity_definition data producer and friends. | ||
* | ||
* @group graphql | ||
*/ | ||
class ExtensionDirectivesTest extends GraphQLTestBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $strictConfigSchema = FALSE; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any reason why we disable that? Please add a code comment. |
||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected static $modules = [ | ||
'graphql_extension_directives_test', | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->createTestServer( | ||
'composable', | ||
'/extension-directives-test', | ||
[ | ||
'schema_configuration' => [ | ||
'composable' => [ | ||
'extensions' => [ | ||
'extension_directives_test' => 'extension_directives_test', | ||
], | ||
], | ||
], | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Tests that retrieving an entity definition works. | ||
*/ | ||
public function testFields(): void { | ||
$query = <<<GQL | ||
query { | ||
cars { | ||
brand | ||
model | ||
width | ||
height | ||
depth | ||
} | ||
} | ||
GQL; | ||
|
||
$this->assertResults($query, [], | ||
[ | ||
'cars' => | ||
[ | ||
[ | ||
'brand' => 'Brand', | ||
'model' => 'Model', | ||
'width' => '1', | ||
'height' => '1', | ||
'depth' => '1', | ||
], | ||
], | ||
] | ||
); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wrong comment?