Skip to content

Commit

Permalink
More directive docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vektah committed Aug 8, 2019
1 parent 8f0d9b4 commit 139e4e8
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions docs/content/reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ enum Role {
When we next run go generate, gqlgen will add this directive to the DirectiveRoot
```go
type DirectiveRoot struct {
HasRole func(ctx context.Context, next graphql.Resolver, role Role) (res interface{}, err error)
HasRole func(ctx context.Context, obj interface{}, next graphql.Resolver, role Role) (res interface{}, err error)
}
```

The arguments are:
- *ctx*: the parent context
- *obj*: the object containing the value this was applied to, eg:
- for field definition directives, the object/input object that contains the field
- for argument directives, a map containing all arguments
- *next*: the next directive in the directive chain, or the field resolver. This should be called to get the
value of the field/argument/whatever. You can block access to the field by not calling next for permission
checks etc.
- *...args*: Any args to the directive will be passed in too.

## Use it in the schema

Expand All @@ -47,7 +56,7 @@ package main

func main() {
c := Config{ Resolvers: &resolvers{} }
c.Directives.HasRole = func(ctx context.Context, next graphql.Resolver, role Role) (interface{}, error) {
c.Directives.HasRole = func(ctx context.Context, obj interface{}, next graphql.Resolver, role Role) (interface{}, error) {
if !getCurrentUser(ctx).HasRole(role) {
// block calling the next resolver
return nil, fmt.Errorf("Access denied")
Expand Down

0 comments on commit 139e4e8

Please sign in to comment.