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

Support null literals as a value for arguments and input object fields #6

Closed
mpenick opened this issue Mar 23, 2020 · 3 comments · Fixed by #9
Closed

Support null literals as a value for arguments and input object fields #6

mpenick opened this issue Mar 23, 2020 · 3 comments · Fixed by #9

Comments

@mpenick
Copy link
Contributor

mpenick commented Mar 23, 2020

graphql-go doesn't support null literals, but it's supported by the the graphql-spec: graphql/graphql-spec@3ce8b79.

There's an existing issue and PR for graphql-go:

Using the existing PR I've (mostly) fixed support for null: https://github.com/mpenick/graphql/tree/null_support

@mpenick
Copy link
Contributor Author

mpenick commented Mar 23, 2020

@mpenick mpenick changed the title Support null literals as a value for arguments and input fields Support null literals as a value for field arguments and input object fields Mar 23, 2020
@mpenick mpenick changed the title Support null literals as a value for field arguments and input object fields Support null literals as a value for arguments and input object fields Mar 23, 2020
@mpenick
Copy link
Contributor Author

mpenick commented Mar 23, 2020

I'm currently using a value:

type nullValue struct {}

// Used to detect the difference between a "null" literal and not present
var NullValue = nullValue{}

I'm not sure if using a value is better or worse than using a type type NullValue struct{}. I need to dig into this more.

Ideally, I'd like to use Go's nil instead and cleanup graphql-go's handling of nil values. My current impression is that using nil might only break using default values for input variables. I think this could be fixed though. "Not present" should be used for default values and explicit nil values should be used for null.

@mpenick mpenick linked a pull request Mar 26, 2020 that will close this issue
@mpenick
Copy link
Contributor Author

mpenick commented Mar 26, 2020

The implementation for null support in graphql-go lives here: https://github.com/riptano/graphql-go and that implementation was added to the service in this PR: #9

Externally, the implementation uses nil as the representation for "null" in golang, and internally it differentiates between "not present" and "null" by using nil and a special value type nullValue, respectively.

inputs := map[string]interface{}{}

if input, ok := inputs["field1"]; ok {
	if input == nil {
		// Use nullValue{}
	} else {
		// Use value provided
	}
} else {
	// Not present, use `nil`
}

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 a pull request may close this issue.

1 participant