Custom Omittable Type - #4082
Draft
deitrix wants to merge 10 commits into
Draft
Conversation
…inter unmarshallers
…table underlying type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
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:
The signature of the referenced function must be one of:
Where
Uis 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:
A more complete example can be found here
Model Generation
Model generation works in a similar fashion to the existing
modelconfiguration, where it will choose the first specified type in the list ofomittable_type, and use that for all generated models. For instance, with the following configuration, the corresponding models would be generated:config:
schema:
generated go model
github.com/seambiz/opt/omitnull.Fromwould still be available to gqlgen for the purpose of automatically mapping non-generated, bound types, but it would not be used for modelgen. The existinggraphql.Omittabletype 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: