gqlgen is a Go library for building GraphQL servers without any fuss.
map[string]interface{}
here.Still not convinced enough to use gqlgen? Compare gqlgen with other Go graphql implementations
go get github.com/99designs/gqlgen
in your project directory.go run github.com/99designs/gqlgen init
.You could find a more comprehensive guide to help you get started here.
We also have a couple of real-world examples that show how to GraphQL applications with gqlgen seamlessly,
You can see these examples here 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.
We welcome contributions, Read our Contribution Guidelines to learn more about contributing to gqlgen
When you have nested or recursive schema like this:
type User {
id: ID!
name: String!
friends: [User!]!
}
You need to tell gqlgen that it should only fetch friends if the user requested it. There are two ways to do this;
Write a custom model that omits the friends field:
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
like this:
# 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 also use inline config with directives to achieve the same result
directive @goModel(model: String, models: [String!]) on OBJECT
| INPUT_OBJECT
| SCALAR
| ENUM
| INTERFACE
| UNION
directive @goField(forceResolver: Boolean, name: String) on INPUT_FIELD_DEFINITION
| FIELD_DEFINITION
type User @goModel(model: "github.com/you/pkg/model.User") {
id: ID! @goField(name: "todoId")
friends: [User!]! @goField(forceResolver: true)
}
Yes! You can by remapping it in config as seen below:
models:
ID: # The GraphQL type ID is backed by
model:
- github.com/99designs/gqlgen/graphql.IntID # An go integer
- github.com/99designs/gqlgen/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:
There isn't any way around this, gqlgen has no way to know what you want in a given context.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。