Khan / genqlient

a truly type-safe Go GraphQL client
MIT License
1.02k stars 99 forks source link

change use_struct_references behaviour when it would generate invalid type #343

Closed Fazt01 closed 6 days ago

Fazt01 commented 1 week ago

A possible fix for https://github.com/Khan/genqlient/issues/342. More description and discussion in the issue itself. I am definitely fine for going another approach (reason for draft) if the breaking change is not appropriate.

This PR fixes the issue by changing what type is generated for non-nullable input field types with no default value: for example the added test currently generates:

type UseStructReferencesInput struct {
    Struct         StructInput  `json:"struct"`
    NullableStruct *StructInput `json:"nullableStruct,omitempty"`
}

Previously, it would generate:

type UseStructReferencesInput struct {
    Struct         *StructInput `json:"struct,omitempty"`
    NullableStruct *StructInput `json:"nullableStruct,omitempty"`
}

and after https://github.com/Khan/genqlient/pull/338 this would cause an error during generation as the validation is trying to prevent a type that could cause runtime error by omitting a value for a type for which omitting is not possible.

I have: