shurcooL / graphql

Package graphql provides a GraphQL client implementation.
MIT License
708 stars 282 forks source link

Issue when using a null parameter #25

Open YannickB opened 6 years ago

YannickB commented 6 years ago

Hi,

When calling mutate function with a null variables I have the following error :

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x98 pc=0x8edbc6]

goroutine 21 [running]:
testing.tRunner.func1(0xc4204200f0)
    /usr/lib/go-1.10/src/testing/testing.go:742 +0x29d
panic(0x9cf5e0, 0xda4180)
    /usr/lib/go-1.10/src/runtime/panic.go:505 +0x229
github.com/shurcooL/graphql.writeArgumentType(0xafc4c0, 0xc42024aee0, 0x0, 0x0, 0xc420446101)
    /opt/go/src/github.com/shurcooL/graphql/query.go:58 +0x26
github.com/shurcooL/graphql.queryArguments(0xc4203fec00, 0xc4200e6210, 0xc420178180)
    /opt/go/src/github.com/shurcooL/graphql/query.go:46 +0x22b
github.com/shurcooL/graphql.constructMutation(0x9bf0e0, 0xc4200e6210, 0xc4203fec00, 0x4643fc, 0xda4bc0)
    /opt/go/src/github.com/shurcooL/graphql/query.go:24 +0x6a
github.com/shurcooL/graphql.(*Client).do(0xc420177f28, 0xb04e20, 0xc4200a4050, 0xc420177b01, 0x9bf0e0, 0xc4200e6210, 0xc4203fec00, 0x0, 0x0)
    /opt/go/src/github.com/shurcooL/graphql/graphql.go:53 +0x504
github.com/shurcooL/graphql.(*Client).Mutate(0xc420177f28, 0xb04e20, 0xc4200a4050, 0x9bf0e0, 0xc4200e6210, 0xc4203fec00, 0x0, 0x0)
    /opt/go/src/github.com/shurcooL/graphql/graphql.go:43 +0x65
gitlab.com/YBuron/lib-go/models.(*ModelDefinition).CreateRecordGraphql(0xdaba80, 0xc4204200f0, 0xc420177f28, 0xa81ef1, 0x10, 0xa7f83b, 0xb, 0xc420177cd8, 0xc420177c78, 0x412c28)

My receiving struct

type requirementCreateMutation struct {Requirement requirementResult graphql:"createRequirement(title: $title, parentID: $parentID)"}

My variables : err := client.Mutate(context.Background(), createMutation, map[string]interface{ 'title': 'test', 'parentID': nil, })

My graphql schema : createRequirement( title: String! parentID: ID)

So it seems the error appears if we try to send a nil value to a non required graphql field. I plan to use different struct if parent is nil as workaround, but imho having a traceback in such case is a bug. Ideally, it should just send a nil value to the graphql server.

Best regards,

dmitshur commented 6 years ago

Thanks for reporting this. graphql shouldn't panic, and I will fix that.

But I suspect I see what the issue is. You're not specifying the exact pointer type for the nil variable. You should do this instead:

err := client.Mutate(context.Background(), createMutation, map[string]interface{}{
    "title":    githubql.String("test"),
    "parentID": (*githubql.ID)(nil),
})

Otherwise, it's not possible for graphql to know the types of "title" and "parentID" variables when constructing the query. See https://github.com/shurcooL/graphql#arguments-and-variables for more info.

YannickB commented 6 years ago

It's true, thanks for the tip!