go get -v all
go mod vendor
go generate
go run main.go
-
Extend the .graphql-schema-files
type Todo { uid: ID name: String comment(id: String): [Comment] }
create some querys and mutations
type Query { todo(uid: ID!): Todo todos(uid: ID!): [Todo] } type Mutation { createTodo(todo: String!): Todo }
-
Create a new package e.g.
todoresolver
in the resolver-folderpackage todoresolver
-
Create a struct and name it like
ResolverTodo
type ResolverTodo struct{}
-
Create a model that represent the results
package model // Todo Model type Todo struct { UID string `json:"uid"` Name string `json:"name"` }
-
Bind model to Response
type ResponseTodo struct { todo *model.Todo }
-
Create the Resolver-Functions that match the model-properties
each propertie need his own resolver-function
func (r *ResponseTodo) Name() *string { return &r.todo.Name }
-
Creating the resolver for the querys or mutations and bind them
// Todo Resolver func (r *ResolverTodo) Todo(ctx context.Context, args *struct { UID string }) (*ResponseTodo, error) { todo := &ResponseTodo{todo: &model.Todo{Name: "name", UID: "id1"}} return todo, nil }
-
Bind resolver to
rootresolver
package rootresolver import ( todoresolver "graphql/resolver/todo" ... ) // Resolver export type Resolver struct { todoresolver.ResolverTodo ... }
t.b.a