Skip to content

maxhaensel/gophers-golang-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gophers-golang-example

Run it Local

go get -v all
go mod vendor
go generate
go run main.go

How to create new Querys, Mutations or Resolvers

  1. 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
    }
  2. Create a new package e.g. todoresolver in the resolver-folder

    package todoresolver
  3. Create a struct and name it like ResolverTodo

    type ResolverTodo struct{}
  4. Create a model that represent the results

    package model
     // Todo Model
     type Todo struct {
     UID string `json:"uid"`
     Name string `json:"name"`
     }
  5. Bind model to Response

    type ResponseTodo struct {
    todo *model.Todo
    }
  6. 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
    }
  7. 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
    }
  8. Bind resolver to rootresolver

    package rootresolver
    
    import (
        todoresolver "graphql/resolver/todo"
        ...
    )
    // Resolver export
    type Resolver struct {
        todoresolver.ResolverTodo
        ...
    }

Build Binary

t.b.a

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages