https://github.com/99designs/gqlgen
This is just I imagined how we should do graphql in Go. last time we checked graphql (https://github.com/theplant/graphql), There is no such good library in Go.
Just a quick show for this library:
https://graphql.github.io/graphql-spec/draft/#sec-Type-System
type Todo {
id: ID!
text: String!
done: Boolean!
user: User!
}
type User {
id: ID!
name: String!
}
type Query {
todos: [Todo!]!
}
input NewTodo {
text: String!
userId: String!
}
type Mutation {
createTodo(input: NewTodo!): Todo!
}
$ go run github.com/99designs/gqlgen
Write your own model struct to remove fields that you want to use resolver
api/models.go
type Todo struct {
ID string `json:"id"`
Text string `json:"text"`
Done bool `json:"done"`
}
gqlgen.yml
...
models:
Todo:
model: github.com/sunfmin/go-gqlgen-graphql-demo/api.Todo
Note that I removed user: User!
field compare to schema.graphql
definition. specify the model in gqlgen.yml
file, Then gqlgen will generate the User field as an resolver. means left you to implement this func in a different Todo resolver, Not in queryResolver anymore:
func (r *todoResolver) User(ctx context.Context, obj *api.Todo) (*api.User, error) {
fmt.Println("Calling User resolver", obj)
return &api.User{
ID: "123",
Name: "Felix",
}, nil
}
func (r *queryResolver) Todos(ctx context.Context) ([]*api.Todo, error) {
fmt.Println("Calling Todos resolver")
return storeTodos, nil
}
$ brew install modd
$ go get -v ./...
$ modd
Then access http://localhost:8080/