jeffwillette / graphql-go-pets-example

An example of neelance/graphql-go, resolves against sqlite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

graphql: panic occurred: graphql: could not marshal

zhaogaolong opened this issue · comments

hi
I'm clone your code and running it. but I found this error

{
  getUser(id: 1) {
    name
    id
  }
}

http response

"json: error calling MarshalJSON for type json.RawMessage: invalid character '}' looking for beginning of value\n"

backend error

2018/04/15 23:43:59 graphql: panic occurred: graphql: could not marshal
goroutine 778 [running]:
golang.zhaogaolong.com/graphql-go-pets-example/vendor/github.com/graph-gophers/graphql-go/log.(*DefaultLogger).LogPanic(0x47a21f8, 0x458f060, 0xc420214480, 0x44c3d00, 0xc4202183c0)
        /Users/zhaogaolong/go/src/golang.zhaogaolong.com/graphql-go-pets-example/vendor/github.com/graph-gophers/graphql-go/log/log.go:21 +0x77
golang.zhaogaolong.com/graphql-go-pets-example/vendor/github.com/graph-gophers/graphql-go/internal/exec.(*Request).handlePanic(0xc420218180, 0x458f060, 0xc420214480)
        /Users/zhaogaolong/go/src/golang.zhaogaolong.com/graphql-go-pets-example/vendor/github.com/graph-gophers/graphql-go/internal/exec/exec.go:29 +0x87
panic(0x44c3d00, 0xc4202183c0)
        /usr/local/Cellar/go/1.10/libexec/src/runtime/panic.go:505 +0x229
golang.zhaogaolong.com/graphql-go-pets-example/vendor/github.com/graph-gophers/graphql-go/internal/exec.(*Request).execSelectionSet(0xc420218180, 0x458f060, 0xc420214d50, 0x0, 0x0, 0x0, 0x458d060, 0xc4201741c0, 0xc42026ad00, 0x44da5e0, ...)
        /Users/zhaogaolong/go/src/golang.zhaogaolong.com/graphql-go-pets-example/vendor/github.com/graph-gophers/graphql-go/internal/exec/exec.go:273 +0xb40
golang.zhaogaolong.com/graphql-go-pets-example/vendor/github.com/graph-gophers/graphql-go/internal/exec.execFieldSelection(0x458f060, 0xc420214480, 0xc420218180, 0xc420170380, 0xc42026ad00, 0x1)
        /Users/zhaogaolong/go/src/golang.zhaogaolong.com/graphql-go-pets-example/vendor/github.com/graph-gophers/graphql-go/internal/exec/exec.go:205 +0x2cc
golang.zhaogaolong.com/graphql-go-pets-example/vendor/github.com/graph-gophers/graphql-go/internal/exec.(*Request).execSelections.func1(0xc4200a47a0, 0xc420218180, 0x458f060, 0xc420214480, 0xc42026a9c0, 0xc420170380)
        /Users/zhaogaolong/go/src/golang.zhaogaolong.com/graphql-go-pets-example/vendor/github.com/graph-gophers/graphql-go/internal/exec/exec.go:74 +0x16c
created by golang.zhaogaolong.com/graphql-go-pets-example/vendor/github.com/graph-gophers/graphql-go/internal/exec.(*Request).execSelections
        /Users/zhaogaolong/go/src/golang.zhaogaolong.com/graphql-go-pets-example/vendor/github.com/graph-gophers/graphql-go/internal/exec/exec.go:70 +0x476

I not edit your code. I use dep ensuredowload require golang codes.

go version: go version go1.10 darwin/amd64

I guess graphql.ID cannot be a typecast. It should be fixed in 04a074e. Please try it again and reopen if you still have an issue

I seew this commit. I found answer from my colleague. he tell me use base64 format this ID

userId := graphql.ID(base64.StdEncoding.EncodeToString([]byte(string(u.Model.ID))))
return &userId

if you think fix this question, I can apply for a merge request.

I'm not sure that this is any different than what I have there already. Is there a reason for doing all the base64 stuff instead of just fmt.Sprinting it?

I think you used graphql Id, but graphql id is use base64 fomat to string, this is graphql id's rule.
docs: https://graphql.org/learn/schema/#scalar-types

  • ID: The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be human‐readable.

Sorry for the late reply, busy times these days. I think the PR is a good idea. If you do it, can you also change the example requests in the docs to have the correct ID in the query?

Thanks