Skip to content

Commit

Permalink
document custom directives
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed May 13, 2024
1 parent 21b1857 commit bdee327
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/master/custom-directives/field-directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ directive can be used to manipulate the schema AST.
This directive type is implemented as an abstract class rather than a pure interface and allows
you to define complex validation rules for a field with ease.

[Read more about it in the Validation section](../security/validation.md#validate-fields).
[Read more about it in the Validation section](../security/validation.md#validator-for-fields).

## ComplexityResolverDirective

Expand Down
18 changes: 10 additions & 8 deletions docs/master/custom-directives/getting-started.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Implementing Your Own Directives

As you grow your GraphQL schema, you may find the need for more specialized functionality.
Learn how you can abstract logic in a composable and reusable manner by using custom directives.

Directives are implemented as PHP classes, each directive available
in the schema corresponds to a single class.
You can abstract logic in a composable and reusable manner by using custom directives.

## Naming Convention

Directives are implemented as PHP classes.
Each directive available in the schema corresponds to a single class.

Directive names themselves are typically defined in **camelCase**.
The class name of a directive must follow the following pattern:

Expand Down Expand Up @@ -51,12 +51,11 @@ GRAPHQL;
## Directive Interfaces

At this point, the directive does not do anything.
Depending on what your directive should do, you can pick one or more of the provided
directive interfaces to add functionality. They serve as the point of contact to Lighthouse.
Depending on what your directive should do, you can pick one or more of the provided directive interfaces to add functionality.
They serve as the point of contact to Lighthouse.

In this case, our directive needs to run after the actual resolver.
Just like [Laravel Middleware](https://laravel.com/docs/middleware),
we can wrap around it by using the `FieldMiddleware` directive.
Just like [Laravel Middleware](https://laravel.com/docs/middleware), we can wrap around it by using the `FieldMiddleware` directive.

```php
namespace App\GraphQL\Directives;
Expand Down Expand Up @@ -87,6 +86,9 @@ final class UpperCaseDirective extends BaseDirective implements FieldMiddleware
}
```

Given there are a lot of use cases for custom directives, the documentation does not provide examples for most interfaces.
It is advised to look at the Lighthouse source code to find directives that implement certain interfaces to learn more.

## Register Directives

Now that we defined and implemented the directive, how can Lighthouse find it?
Expand Down

0 comments on commit bdee327

Please sign in to comment.