Skip to content

Custom Omittable Type - #4082

Draft
deitrix wants to merge 10 commits into
99designs:masterfrom
deitrix:custom-omittable
Draft

Custom Omittable Type#4082
deitrix wants to merge 10 commits into
99designs:masterfrom
deitrix:custom-omittable

Conversation

@deitrix

@deitrix deitrix commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

This is an attempt to expand the existing omittable behaviour to allow a user-configurable omittable types, a solution for #3227 (comment)

It adds a new piece of config to gqlgen.yml:

omittable_type: <omittable constructor function reference>

By default, this will continue to use the graphql.Omittable, but it can be overridden with a reference to a function which constructs an omittable of a different type. For example, to use Samber's Option type, the config would look like:

omittable_type: github.com/samber/mo.Some

The signature of the referenced function must be one of:

func [T any](v T) U
func [T any](v T) (U, error)
func [T any](v *T) U
func [T any](v *T) (U, error)

Where U is the type of the field being bound to.

To allow for multiple omittable implementations, say for the sake of binding to a type in an external package that uses the omitnull package, you can specify multiple omittable constructor functions, and gqlgen will find the most appropriate one to use on a case by case basis:

omittable_type:
- github.com/samber/mo.Some
- github.com/seambiz/opt/omitnull.From

A more complete example can be found here

Model Generation

Model generation works in a similar fashion to the existing model configuration, where it will choose the first specified type in the list of omittable_type, and use that for all generated models. For instance, with the following configuration, the corresponding models would be generated:

config:

omittable_type:
- github.com/samber/mo.Some
- github.com/seambiz/opt/omitnull.From

schema:

input MyInput {
  myField: String @goField(omittable: true)
}

generated go model

type MyModel struct {
  MyField mo.Option[*string] `json:"myField"`
}

github.com/seambiz/opt/omitnull.From would still be available to gqlgen for the purpose of automatically mapping non-generated, bound types, but it would not be used for modelgen. The existing graphql.Omittable type would be the default fallback type, if no configuration is provided, meaning all existing projects using gqlgen would continue to work without issue.

To-do:

  • Added tests covering the bug / feature (see testing)
  • Updated any relevant documentation (see docs)
  • Update modelgen to generate fields with the first defined omittable type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant