shurcooL / githubv4

Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://docs.github.com/en/graphql).
MIT License
1.12k stars 90 forks source link

Custom JSON tags in query data structure will cause response not to get populated. #14

Closed dmitshur closed 7 years ago

dmitshur commented 7 years ago

If you do a query like this:

type query struct {
    Viewer struct {
        Login githubql.String `json:"username"`
    }
}

(Imagine the user wants to serialize the response later, or uses some struct that happens to have json tags defined for other reasons.)

The JSON-encoded response from GraphQL server will be:

{
    "data": {
        "viewer": {
            "login": "gopher"
        }
    }
}

So query.Viewer.Login will not be populated, since the Go struct has a JSON tag calling it "username", but the field is "login" in the response, which doesn't match.

This happens because encoding/json package, which is currently used to decode the JSON response from GraphQL server into the query data structure, is affected by json struct field tags.

Related to #10, because a solution to that will resolve this too.