Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for null vs missing input value #430

Merged
merged 1 commit into from
Mar 5, 2021

Conversation

Ackar
Copy link
Contributor

@Ackar Ackar commented Jan 15, 2021

This is an attempt to solve #210 by adding support for nullable non-pointer types. This is inspired by the Null types in the sql package.

The idea is to have custom types where we can call UnmarshalGraphQL with a nil input, thus allowing to differentiate between no value (method not called) and a null value (method called with nil).

The main change is to allow a non-pointer type to represent a nullable value if it implements the following interface:

type NullUnmarshaller interface {
	Unmarshaler
	Nullable()
}

Also added nullable types for existing scalars: NullString, NullInt, NullFloat, NullBool and NullTime.

Example

input MyInput {
    a: String
}

type Query {
    myQuery(input: MyInput!): String!
}
type MyInput struct {
	A graphql.NullString
}

type resolver struct{}

func (r *resolver) MyQuery(args struct {
	Input MyInput
}) string {
	if args.Input.A.Valid {
		if args.Input.A.Value == nil {
			// null value provided
		} else {
			// non-null value provided
		}
	} else {
		// field omitted
	}

	return "hello world"
}

@pavelnikolov
Copy link
Member

Hi @Ackar
Thank you for your contribution. I'll take a closer look tomorrow.

nullable_types.go Outdated Show resolved Hide resolved
This allows to differentiate between an omitted value and a null value
in an input struct.
@pavelnikolov pavelnikolov merged commit 6859f27 into graph-gophers:master Mar 5, 2021
@pavelnikolov
Copy link
Member

Thank you

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.

2 participants