gqlgen is a Go library for building GraphQL servers without any fuss. gqlgen is:
- Schema first — Define your API using the GraphQL Schema Definition Language.
- Type safe — You should never see
map[string]interface{}
here. - Codegen — Let us generate the boring bits, so you can build your app quickly.
First work your way through the Getting Started tutorial.
If you can't find what your looking for, look at our examples for example usage of gqlgen, or visit godoc.
If you think you've found a bug, or something isn't behaving the way you think it should, please raise an issue on GitHub.
Read our Contribution Guidelines for information on how you can help out gqlgen.
When you have nested or recursive schema like this:
type User {
id: ID!
name: String!
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.
Write a custom model that omits the Friends model:
type User struct {
ID int
Name string
}
And reference the model in gqlgen.yml
:
# gqlgen.yml
models:
User:
model: github.com/you/pkg/model.User # go import path to the User struct above
If you want to Keep using the generated model: mark the field as requiring a resolver explicitly in gqlgen.yml
:
# gqlgen.yml
models:
User:
fields:
friends:
resolver: true # force a resolver to be generated
After doing either of the above and running generate we will need to provide a resolver for friends:
func (r *userResolver) Friends(ctx context.Context, obj *User) ([]*User, error) {
// select * from user where friendid = obj.ID
return friends, nil
}
You can by remapping it in config:
models:
ID: # The GraphQL type ID is backed by
model:
- github.com/vndocker/encrypted-graphql/graphql.IntID # An go integer
- github.com/vndocker/encrypted-graphql/graphql.ID # or a go string
This means gqlgen will be able to automatically bind to strings or ints for models you have written yourself, but the first model in this list is used as the default type and it will always be used when:
- Generating models based on schema
- As arguments in resolvers
There isnt any way around this, gqlgen has no way to know what you want in a given context.