-
-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
233 additions
and
179 deletions.
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
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
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
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,61 @@ | ||
# Field Argument Directives | ||
|
||
Field argument directives can be applied to field arguments (see [Field Arguments](https://spec.graphql.org/June2018/#sec-Field-Arguments)). | ||
|
||
## ArgManipulator | ||
|
||
An [`\Nuwave\Lighthouse\Support\Contracts\ArgManipulator`](https://github.com/nuwave/lighthouse/tree/master/src/Support/Contracts/ArgManipulator.php) | ||
directive can be used to manipulate the schema AST of a field argument or its parents. | ||
|
||
For example, you might want to add a directive that automagically derives the arguments | ||
for a field based on an object type. A skeleton for this directive might look something like this: | ||
|
||
```php | ||
namespace App\GraphQL\Directives; | ||
|
||
use GraphQL\Language\AST\FieldDefinitionNode; | ||
use GraphQL\Language\AST\InputObjectTypeDefinitionNode; | ||
use GraphQL\Language\AST\InputValueDefinitionNode; | ||
use GraphQL\Language\AST\ObjectTypeDefinitionNode; | ||
use Nuwave\Lighthouse\Schema\AST\DocumentAST; | ||
use Nuwave\Lighthouse\Schema\Directives\BaseDirective; | ||
use Nuwave\Lighthouse\Support\Contracts\ArgManipulator; | ||
|
||
final class ModelArgsDirective extends BaseDirective implements ArgManipulator | ||
{ | ||
public static function definition(): string | ||
{ | ||
return /** @lang GraphQL */ <<<'GRAPHQL' | ||
""" | ||
Automatically generates an input argument based on a type. | ||
""" | ||
directive @typeToInput( | ||
""" | ||
The name of the type to use as the basis for the input type. | ||
""" | ||
name: String! | ||
) on ARGUMENT_DEFINITION | ||
GRAPHQL; | ||
} | ||
|
||
public function manipulateArgDefinition( | ||
DocumentAST &$documentAST, | ||
InputValueDefinitionNode &$argDefinition, | ||
FieldDefinitionNode &$parentField, | ||
ObjectTypeDefinitionNode &$parentType | ||
): void { | ||
$typeName = $this->directiveArgValue('name'); | ||
$type = $documentAST->types[$typeName]; | ||
|
||
$input = $this->generateInputFromType($type); | ||
$argDefinition->name->value = $input->value->name; | ||
|
||
$documentAST->setTypeDefinition($input); | ||
} | ||
|
||
protected function generateInputFromType(ObjectTypeDefinitionNode $type): InputObjectTypeDefinitionNode | ||
{ | ||
// TODO generate this type based on rules and conventions that work for you | ||
} | ||
} | ||
``` |
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,56 @@ | ||
# Input Field Directives | ||
|
||
Input field directives can be applied to input fields (see [InputFieldsDefinition](https://spec.graphql.org/June2018/#InputFieldsDefinition)). | ||
|
||
## InputFieldManipulator | ||
|
||
An [`\Nuwave\Lighthouse\Support\Contracts\InputFieldManipulator`](https://github.com/nuwave/lighthouse/tree/master/src/Support/Contracts/InputFieldManipulator.php) | ||
directive can be used to manipulate the schema AST of an input field or its parent. | ||
|
||
For example, the following directive automatically adds translations for the input field description. | ||
|
||
```php | ||
namespace Nuwave\Lighthouse\Schema\Directives; | ||
|
||
use Nuwave\Lighthouse\Support\Contracts\InputFieldManipulator; | ||
use GraphQL\Language\Parser; | ||
|
||
final class TranslateDescriptionDirective extends BaseDirective implements InputFieldManipulator | ||
{ | ||
public static function definition(): string | ||
{ | ||
return /** @lang GraphQL */ <<<'GRAPHQL' | ||
""" | ||
Extends the description with automatic translations. | ||
""" | ||
directive @translateDescription on INPUT_FIELD_DEFINITION | ||
GRAPHQL; | ||
} | ||
|
||
public function manipulateInputFieldDefinition( | ||
DocumentAST &$documentAST, | ||
InputValueDefinitionNode &$inputField, | ||
InputObjectTypeDefinitionNode &$parentInput, | ||
): void { | ||
$inputField->description = implode('\n\n', [ | ||
$inputField->description, | ||
\Translate::spanish($inputField->description), | ||
\Translate::german($inputField->description), | ||
]); | ||
} | ||
} | ||
``` | ||
|
||
```diff | ||
input CreateCommentInput { | ||
- "Very nice." | ||
+ """ | ||
+ Very nice. | ||
+ | ||
+ Muy bien. | ||
+ | ||
+ Sehr gut. | ||
+ """ | ||
content: String! @translateDescription | ||
} | ||
``` |
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
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
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
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
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
61 changes: 61 additions & 0 deletions
61
docs/master/custom-directives/field-argument-directives.md
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,61 @@ | ||
# Field Argument Directives | ||
|
||
Field argument directives can be applied to field arguments (see [Field Arguments](https://spec.graphql.org/June2018/#sec-Field-Arguments)). | ||
|
||
## ArgManipulator | ||
|
||
An [`\Nuwave\Lighthouse\Support\Contracts\ArgManipulator`](https://github.com/nuwave/lighthouse/tree/master/src/Support/Contracts/ArgManipulator.php) | ||
directive can be used to manipulate the schema AST of a field argument or its parents. | ||
|
||
For example, you might want to add a directive that automagically derives the arguments | ||
for a field based on an object type. A skeleton for this directive might look something like this: | ||
|
||
```php | ||
namespace App\GraphQL\Directives; | ||
|
||
use GraphQL\Language\AST\FieldDefinitionNode; | ||
use GraphQL\Language\AST\InputObjectTypeDefinitionNode; | ||
use GraphQL\Language\AST\InputValueDefinitionNode; | ||
use GraphQL\Language\AST\ObjectTypeDefinitionNode; | ||
use Nuwave\Lighthouse\Schema\AST\DocumentAST; | ||
use Nuwave\Lighthouse\Schema\Directives\BaseDirective; | ||
use Nuwave\Lighthouse\Support\Contracts\ArgManipulator; | ||
|
||
final class ModelArgsDirective extends BaseDirective implements ArgManipulator | ||
{ | ||
public static function definition(): string | ||
{ | ||
return /** @lang GraphQL */ <<<'GRAPHQL' | ||
""" | ||
Automatically generates an input argument based on a type. | ||
""" | ||
directive @typeToInput( | ||
""" | ||
The name of the type to use as the basis for the input type. | ||
""" | ||
name: String! | ||
) on ARGUMENT_DEFINITION | ||
GRAPHQL; | ||
} | ||
|
||
public function manipulateArgDefinition( | ||
DocumentAST &$documentAST, | ||
InputValueDefinitionNode &$argDefinition, | ||
FieldDefinitionNode &$parentField, | ||
ObjectTypeDefinitionNode &$parentType | ||
): void { | ||
$typeName = $this->directiveArgValue('name'); | ||
$type = $documentAST->types[$typeName]; | ||
|
||
$input = $this->generateInputFromType($type); | ||
$argDefinition->name->value = $input->value->name; | ||
|
||
$documentAST->setTypeDefinition($input); | ||
} | ||
|
||
protected function generateInputFromType(ObjectTypeDefinitionNode $type): InputObjectTypeDefinitionNode | ||
{ | ||
// TODO generate this type based on rules and conventions that work for you | ||
} | ||
} | ||
``` |
Oops, something went wrong.