graphql-codegen-go
Generate Go structs from your GraphQL schema.
This code generator helps you derive Go structures directly from GraphQL schema. The schema
can be located either locally or can be fetched from GIT repository.
Install it using go get:
$ go get -u github.com/jkrajniak/graphql-codegen-go
Quick start
Simply, define the GQL schema
type Person {
name: String!
age: Int!
weight: Int
likes: [String]
donts: [String!]
}
and save it, e.g. in schema.gql file. Then run the code generator
$ graphql-codegen-go -schema schema.gql -packageName pkg -out models.go
As a result, you will get a models.go file with the following Go code
// Code generated by go generate; DO NOT EDIT.
// This file was generated from GraphQL schema schema.gql
package pkg
type Person struct {
Name string `json:"name"`
Age int64 `json:"age"`
Weight *int64 `json:"weight"`
Likes []*string `json:"likes"`
Donts []string `json:"donts"`
}
Notice that not required (weight) fields are converted to the pointers. The packageName option is optional. The generator
will try to derive the package name from the current running path of the code, or if run by go:generate from $GOPACKAGE env variable.
GIT
The schema does not have to be located locally. The program supports also Git repositories.
Let's assume that you the schema.gql file is placed in github.com/orange/repo1 repository, inside a deployment directory.
Then, to create the structures you can run the generator as follows
$ graphql-codegen-go -schema https://github.com/orange/repo1.git/deployment/schema.gql -packageName pkg -out models.go
or via ssh
$ graphql-codegen-go -schema git@github.com:orange/repo1.git/deployment/schema.gql -packageName pkg -out models.go
By default, the schema is pulled from the HEAD. To point a specific commit, you can place a commit hash after the file name, e.g.,
$ graphql-codegen-go -schema git@github.com:orange/repo1.git/deployment/schema.gql#a56351vc -packageName pkg -out models.go
Moreover, you can also point the specific branch or tag by using @ sign
$ graphql-codegen-go -schema git@github.com:orange/repo1.git/deployment/schema.gql@branch -packageName pkg -out models.go
or
$ graphql-codegen-go -schema git@github.com:orange/repo1.git/deployment/schema.gql@tag1 -packageName pkg -out models.go
Entities
By default generator will output structures for all of the entities found in the schema. To output only a subset of structures
you can use -entities option.
For example
$ graphql-codegen-go -schema schema.gql -packageName pkg -out models.go -entities Person
will create file models.go only with a single structure Person.
Note: currently generator does not track the dependencies between entities.
Union
The union type is supported in the following way. Let's consider a schema
type Et {
value: String
}
type Pt {
label: String!
}
union Ett = Et | Pt
type Entity {
id: String!
etpt: Ett
}
The union type Ett is converted into a Go struct Ett
type Et struct {
Value *string `json:"value"`
}
type Pt struct {
Label string `json:"label"`
}
type Ett struct {
TypeName string `json:"__typeName"`
Et
Pt
}
type Entity struct {
Id string `json:"id"`
Etpt *Ett `json:"etpt"`
}
go:generate
The generator can work perfectly fine with the go:generate directive. The examples of how to include it can be found in examples/ directory.