Skip to content

Generate graphql golang boiler-plate code from graphql schema

License

Notifications You must be signed in to change notification settings

DealTap/graphql-gen-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphql-gen-go

Early Stage

Generates boiler-plate code for golang graphql from graphql schema files.

Install from source

Prerequisites

  1. make (http://www.gnu.org/software/make/)
  2. Go and Go workspace
  3. glide (https://github.com/Masterminds/glide)

Download and Build

  1. go get -u github.com/DealTap/graphql-gen-go
  2. Change current directory cd $GOPATH/src/github.com/dealtap/graphql-gen-go
  3. Run make install build

Getting Started

  1. Download the latest binary from releases or build from the source
  2. To generate code, run graphql-gen-go SOME_FILE --out_dir SOME_PATH --pkg SOME_PACKAGE. You can pass more than one file
  3. Run graphql-gen-go --help to get usage details

Parameters

  • out_dir - destination directory of the generated files. Default is current directory
  • pkg - package name of the generated files. Default is main

Notes

  • Resolver function is not generated for a GraphQL type which has a property with arguments. It is assumed that such a property would require additional logic; so, it should be implemented manually. Take a look at sample/api/api-extra.go for an example
  • A server.gql.go file is also generated which implements a custom http handler and runs a GraphQL server. It has dependency on graphql-go and cors libraries. You can use this or your own http handler or built in one in graphql-go

How to Use Generated Code

With generated http handler

A sample test server is included: sample/test-server.go. You can start this sample server by running make run-sample and it can be queried like

curl -XPOST localhost:7050/graphql -H "Content-Type: application/graphql" \
-d '{ person(id: "1000") { name email friends { name email } } }'

With http handler from graphql-go

var schema *graphql.Schema
type resolver struct{}

func (r *resolver) Person(request api.PersonRequest) api.PersonResolver {
  ...
}

func init() {
  schema, err = graphql.ParseSchema(api.Schema, &resolver{})
    if err != nil {
      panic(err)
    }
}

func main() {
  http.Handle("/graphql", &relay.Handler{Schema: schema})
  err := http.ListenAndServe(":7050", nil)
  if err != nil {
    panic(err)
  }
}

Status

  • Minimal Api
  • Generate Code for graphql types
    • query
    • mutation
    • interface
    • object
    • enum
    • input object
    • union
  • Custom http handler
    • minimal implementation
    • Add facebook data loader in custom http handler for batching
    • option to specify middleware/interceptors that will run before/after executing incoming graphql request i.e., auth check
  • Improve api/code
    • refactor duplicate code
    • handle imports dynamically instead of hard-coding
    • option to disable generation of custom http handler

Credit

Hard fork of graphql-gen-go and inspired by protoc-gen-go. Thanks to old: neelance -> new: graph-gophers for the great graphql-go library. Http custom handler is inspired by graphql-go-example

About

Generate graphql golang boiler-plate code from graphql schema

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published