master
master v0.17.56 v0.17.55 v0.17.54 v0.17.53 v0.17.52 v0.17.51 v0.17.50 v0.17.49 v0.17.48 v0.17.47 v0.17.46 v0.17.45 v0.17.44 v0.17.43 v0.17.42 v0.17.41 v0.17.40 v0.17.39 v0.17.38 v0.17.37

Model Generation

Model generation
[edit]
You are looking at the docs for the unreleased master branch. The latest version is v0.17.56.

While we do our best to create Go models that are equivalent to their GraphQL counterparts, it can sometimes be advantageous, or even necessary, to have control over some aspects of this output depending on runtime environment.

json “,omitempty”

By default, fields that are marked as nullable in the GraphQL schema, e.g. field: String rather than field: String!, have the json “,omitempty” field tag applied to them. This is probably fine if the downstream consumers of json serialized representations of this model are all written in Go, but obviously this is not always true.

To that end, you expressly disable the addition of the ,omitempty json field tag by setting the top-level config field enable_model_json_omitempty_tag to false:

Examples

# graphql

type OmitEmptyJsonTagTest {
    ValueNonNil: String!
    Value: String
}

Without enable_model_json_omitempty_tag configured:

type OmitEmptyJSONTagTest struct {
	ValueNonNil string  `json:"ValueNonNil" database:"OmitEmptyJsonTagTestValueNonNil"`
	Value       *string `json:"Value,omitempty" database:"OmitEmptyJsonTagTestValue"`
}

With enable_model_json_omitempty_tag: true (same as un-configured):

type OmitEmptyJSONTagTest struct {
	ValueNonNil string  `json:"ValueNonNil" database:"OmitEmptyJsonTagTestValueNonNil"`
	Value       *string `json:"Value,omitempty" database:"OmitEmptyJsonTagTestValue"`
}

With enable_model_json_omitempty_tag: false:

type OmitEmptyJSONTagTest struct {
	ValueNonNil string  `json:"ValueNonNil" database:"OmitEmptyJsonTagTestValueNonNil"`
	Value       *string `json:"Value" database:"OmitEmptyJsonTagTestValue"`
}