graphql icon indicating copy to clipboard operation
graphql copied to clipboard

Add support for null literals

Open evulse opened this issue 6 years ago • 7 comments

Choice of implementation was based on not breaking existing code. For instance the following will not break with this change

price, priceOk := params.Args["price"].(float64)

However you can now detect null by adding another type assertion

price, priceOk := params.Args["price"].(float64)
_, priceNull := params.Args["price"].(graphql.NullValue)

As you can see the value of graphql.NullValue is irrelevant, we just need to test for its existence.

The same thing can be accomplished by using a type switch, which there is an example of in the new example crud-null

The motivation for this change is to bring inline with spec and that forcing null values on database fields is rather common. Plus obviously needed this functionality for current application use.

Also addresses https://github.com/graphql-go/graphql/issues/178

evulse avatar Sep 23 '18 11:09 evulse

Coverage Status

Coverage decreased (-0.2%) to 91.545% when pulling fccf08bcf4f445023044a20b2c1a45a0683b7d87 on carted:master into a7e15c0298411af18c694ca387e733acef287104 on graphql-go:master.

coveralls avatar Sep 23 '18 11:09 coveralls

@evulse Glad to see someone working on support for this! One question though: Why is adding a new type to represent null values necessary? Couldn't we just do this using nil?

func main() {
	m := map[string]interface{}{}

	_, priceOk := m["foo"].(float64)
	v, priceNull := m["foo"]
	fmt.Println(priceOk, priceNull && v == nil)

	m["foo"] = nil

	_, priceOk = m["foo"].(float64)
	v, priceNull = m["foo"]
	fmt.Println(priceOk, priceNull && v == nil)
}
false false
false true

– https://play.golang.org/p/tXyzbrMsXHy

ccbrown avatar Oct 02 '18 17:10 ccbrown

@ccbrown I guess thats completely possible, I guess it was more to avoid any possible situations of the value being nil. Instead of going through and confirming that nil is never possibly outputted from maybe a nil pointer or struct it was easier to just add a new type knowing that nothing could possibly be using the new type.

evulse avatar Oct 09 '18 04:10 evulse

Any news on this pull request ?

clery avatar Mar 15 '19 16:03 clery

Facing a problem and I think this pull request fixes that, any updates of this PR?

cettoana avatar Jun 25 '19 01:06 cettoana

@chris-ramon Do you have some time to review this PR?

What can I do to help getting this patch merged?

jorgebay avatar Mar 20 '20 14:03 jorgebay

Are there any chances this pull request will be merged into master? Is there anything we can do to help?

seriallink avatar Dec 04 '21 11:12 seriallink