v0.17.38
v0.17.38 v0.17.37 v0.17.36 v0.17.35 v0.17.34 v0.17.33 v0.17.32 v0.17.31 v0.17.30 v0.17.29 v0.17.28 v0.17.27 v0.17.26 v0.17.25 v0.17.24 v0.17.23 v0.17.22 v0.17.21 v0.17.20 v0.17.19 master

Model Generation

Model generation
[edit]

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