Skip to content

Commit

Permalink
v6.31.0
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Jan 17, 2024
1 parent 4426483 commit 4b0c9c5
Show file tree
Hide file tree
Showing 17 changed files with 233 additions and 179 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

## v6.31.0

### Added

- Add `InputFieldManipulator` directive interface https://github.com/nuwave/lighthouse/pull/2476
Expand Down
2 changes: 1 addition & 1 deletion docs/6/api-reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -3417,7 +3417,7 @@ they are passed along to the resolver:
]
```
Note that Lighthouse spreads out the arguments **after** all other [ArgDirectives](../custom-directives/argument-directives.md)
Note that Lighthouse spreads out the arguments **after** all other [ArgDirectives](../custom-directives/field-argument-directives)
have been applied, e.g. validation, transformation.
## @subscription
Expand Down
2 changes: 1 addition & 1 deletion docs/6/concepts/arg-resolvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,4 @@ final class CreateDirective extends BaseDirective implements FieldResolver, ArgR
}
```

You may define your own nested arg resolver directives by implementing [`ArgResolver`](../custom-directives/argument-directives.md#argresolver).
You may define your own nested arg resolver directives by implementing [`ArgResolver`](../custom-directives/field-argument-directives#argresolver).
61 changes: 61 additions & 0 deletions docs/6/custom-directives/field-argument-directives.md
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
}
}
```
56 changes: 56 additions & 0 deletions docs/6/custom-directives/input-field-directives.md
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
}
```
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Argument Directives
# Input Value Directives

Argument directives can be applied to a [InputValueDefinition](https://graphql.github.io/graphql-spec/June2018/#InputValueDefinition).
Some directives can be applied to field arguments or input fields (any [InputValueDefinition](https://graphql.github.io/graphql-spec/June2018/#InputValueDefinition)).

As arguments may be contained within a list in the schema definition, you must specify
what your argument should apply to in addition to its function.
what your directive should apply to in addition to its function.

- If it applies to the individual items within the list,
implement the [`\Nuwave\Lighthouse\Support\Contracts\ArgDirective`](https://github.com/nuwave/lighthouse/tree/master/src/Support/Contracts/ArgDirective.php) interface.
Expand Down Expand Up @@ -212,70 +212,3 @@ that field resolvers are composed together.

For an in-depth explanation of the concept of composing arg resolvers,
read the [explanation of arg resolvers](../concepts/arg-resolvers.md).

## 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.

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 | INPUT_FIELD_DEFINITION
GRAPHQL;
}

/**
* Manipulate the AST.
*
* @param \Nuwave\Lighthouse\Schema\AST\DocumentAST $documentAST
* @param \GraphQL\Language\AST\InputValueDefinitionNode $argDefinition
* @param \GraphQL\Language\AST\FieldDefinitionNode $parentField
* @param \GraphQL\Language\AST\ObjectTypeDefinitionNode $parentType
* @return void
*/
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
}
}
```
2 changes: 1 addition & 1 deletion docs/6/security/sanitization.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ it is most practical to modify the given input to ensure it is valid or safe to

## Single arguments

A great way to deal with single values is to use an [`ArgTransformerDirective`](../custom-directives/argument-directives.md#argtransformerdirective).
A great way to deal with single values is to use an [`ArgTransformerDirective`](../custom-directives/field-argument-directives#argtransformerdirective).
Lighthouse offers a few built-in options, but it is also really easy to build your own.

Here is how you can remove whitespace of a given input string by using
Expand Down
4 changes: 3 additions & 1 deletion docs/6/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ module.exports = [
["custom-directives/getting-started", "Getting Started"],
"custom-directives/type-directives",
"custom-directives/field-directives",
"custom-directives/argument-directives",
"custom-directives/input-value-directives",
"custom-directives/field-argument-directives",
"custom-directives/input-field-directives",
],
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/master/api-reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -3417,7 +3417,7 @@ they are passed along to the resolver:
]
```
Note that Lighthouse spreads out the arguments **after** all other [ArgDirectives](../custom-directives/argument-directives.md)
Note that Lighthouse spreads out the arguments **after** all other [ArgDirectives](../custom-directives/field-argument-directives)
have been applied, e.g. validation, transformation.
## @subscription
Expand Down
2 changes: 1 addition & 1 deletion docs/master/concepts/arg-resolvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,4 @@ final class CreateDirective extends BaseDirective implements FieldResolver, ArgR
}
```

You may define your own nested arg resolver directives by implementing [`ArgResolver`](../custom-directives/argument-directives.md#argresolver).
You may define your own nested arg resolver directives by implementing [`ArgResolver`](../custom-directives/field-argument-directives#argresolver).
61 changes: 61 additions & 0 deletions docs/master/custom-directives/field-argument-directives.md
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
}
}
```
Loading

0 comments on commit 4b0c9c5

Please sign in to comment.