v0.17.56
v0.17.56 v0.17.61 v0.17.60 v0.17.59 v0.17.58 v0.17.57 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 master

Model Generation

Model generation
[edit]
You are looking at the docs for an older version (v0.17.56). The latest version is v0.17.61.

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"`
}