Khan / genqlient

a truly type-safe Go GraphQL client
MIT License
1.02k stars 99 forks source link

Errors with Strawberry Generated Schema including `JSON` Scalar #307

Closed btsomogyi closed 5 months ago

btsomogyi commented 5 months ago

Describe the bug When using genqlient with a schema that is generated from Strawberry that includes strawberry.scalars import JSON and includes a stanza

"""
The `JSON` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
"""
scalar JSON @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")

in the schema file, an attempt to use a field with this scalar in a query results in the error during go generate:

me@MAC mydir % go generate
/mydir/graphql/schema.graphql:93: unknown scalar JSON: please add it to "bindings" in genqlient.yaml
Example: https://github.com/Khan/genqlient/blob/main/example/genqlient.yaml#L12
exit status 1
generate.go:1: running "go": exit status 1

However, adding

bindings:
  JSON:
    type: string

to the genqlient.yaml file only produces the following error when attempting to execute a query accessing this JSON containing field:

2024/02/08 15:52:29 ERROR error while retrieving Interfaces device=<snip> port="" !BADKEY="json: cannot unmarshal object into Go struct field DevicesDevicesDeviceSchema.data.devices.state of type string"
2024/02/08 15:52:29 ERROR failed to get <snip> devices: %e !BADKEY="json: cannot unmarshal object into Go struct field DevicesDevicesDeviceSchema.data.devices.state of type string"
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x10065ec20]

To Reproduce Provided above, using Golang version 1.21.7 and genqlient github.com/Khan/genqlient v0.6.0 % go version go version go1.21.7 darwin/arm64

Expected behavior I would expect the JSON field to be stored in a string.

genqlient version v0.6.0

Additional context There are no errors occurring on the Strawberry GraphQL server (providing a 200 response to client)

benjaminjkraft commented 5 months ago

You probably want to bind to encoding/json.RawMessage, which is the Go convention for "leave this part of the JSON as JSON"; string has to map to a JSON string. If that doesn't work, I think you'll need to post a more complete repro (e.g. a full schema + config + sample server response) so we can see what's going on! (And if it does, documentation PRs to make this clearer are very welcome!)