graphql
graphql copied to clipboard
Add support for null literals
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
Coverage decreased (-0.2%) to 91.545% when pulling fccf08bcf4f445023044a20b2c1a45a0683b7d87 on carted:master into a7e15c0298411af18c694ca387e733acef287104 on graphql-go:master.
@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 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.
Any news on this pull request ?
Facing a problem and I think this pull request fixes that, any updates of this PR?
@chris-ramon Do you have some time to review this PR?
What can I do to help getting this patch merged?
Are there any chances this pull request will be merged into master? Is there anything we can do to help?