Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#726: Mark field as resolver with graphql directives #727

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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