Skip to content

Commit

Permalink
#726: Mark field as resolver with graphql directives
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfmin committed May 24, 2019
1 parent ba7092c commit 65e88b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

- **Schema first** — Define your API using the GraphQL [Schema Definition Language](http://graphql.org/learn/schema/).
- **Type safe** — You should never see `map[string]interface{}` here.
- **Codegen** — Let us generate the boring bits, so you can build your app quickly.
- **Codegen** — Let us generate the boring bits, so you can build your app quickly.

[Feature Comparison](https://gqlgen.com/feature-comparison/)

Expand Down Expand Up @@ -36,7 +36,7 @@ type User {
friends: [User!]!
}
```
You need to tell gqlgen that we should only fetch friends if the user requested it. There are two ways to do this.
You need to tell gqlgen that we should only fetch friends if the user requested it. There are three ways to do this.

1. Write the model yourself and leave off friends.

Expand Down Expand Up @@ -65,13 +65,25 @@ models:
resolver: true # force a resolver to be generated
```
3. Use graphql directives @resolver on field to mark the field as requiring a resolver explicitly
```graphql
directive @resolver on FIELD_DEFINITION

type User {
id: ID!
name: String!
friends: [User!]! @resolver # force a resolver to be generated
}
```

After doing either of the above and running generate we will need to provide a resolver for friends:
```go
func (r *userResolver) User(ctx context.Context, obj *User) ([]*User, error) {
// select * from user where friendid = obj.ID
return friends, nil
}
```
```

### IDs are strings but I like ints, why cant I have ints?

Expand Down
3 changes: 3 additions & 0 deletions codegen/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ func (b *builder) bindField(obj *Object, f *Field) error {
case obj.Root:
f.IsResolver = true
return nil
case f.FieldDefinition.Directives.ForName("resolver") != nil:
f.IsResolver = true
return nil
case b.Config.Models[obj.Name].Fields[f.Name].Resolver:
f.IsResolver = true
return nil
Expand Down

0 comments on commit 65e88b9

Please sign in to comment.