Khan / genqlient

a truly type-safe Go GraphQL client
MIT License
1.07k stars 107 forks source link

Add `omitempty` for pointer input fields #264

Closed dharijanto closed 7 months ago

dharijanto commented 1 year ago

When optional: "pointer" is configured, omitempty is needed for input fields for the generated code to be semantically correct. Without omitempty an empty pointer would generate an "empty value" which is incorrect.

Consider the following graphql schema generated by Hasura:

input task_insert_input {
  id: Int
}

Without this fix, the following golang struct will be generated:

type Task_insert_input struct {
  Id: *int `json:"id"`
}

When we populate the struct without Id being set, it'd serialize to { id: 0 } as oppose to {} (i.e. so that the id would be generated by Hasura)

I have:

dharijanto commented 1 year ago

Created for this: https://github.com/Khan/genqlient/issues/260

partounian commented 1 year ago

This would be super awesome. The GraphQL server I'm interacting with has deeply nested inputs and without this feature it breaks.

Seems like use_struct_references: false and optional: pointer in the yaml might improve my issue. Lots of types need to be updated, so code is getting messy, but maybe later I can see how to clean this up.

benjaminjkraft commented 7 months ago

Closing in favor of #308 which uses a config option