hasura / go-graphql-client

Package graphql provides a GraphQL client implementation.
MIT License
395 stars 91 forks source link

jsonutil.keyHasGraphQLName Incorrectly parses objects #149

Open coltonBelfils opened 1 month ago

coltonBelfils commented 1 month ago

Currently keyHasGraphQLName checks for the field name in the graphql tag by reading the tag value as the field name until it gets to one of the following: "(:@", checking for, as the function comments, field arguments, aliases, directives. This does not account for just simple objects. This would be relevant for scalars where the object definition is not determined by the accompanying struct. An example of this would be:

Alerts jsonValue.Json `json:"alerts" graphql:"alerts{actions{primary show title url}content dismissibleHandle icon severity title}" scalar:"true"

This would be a single field within a larger struct and query. I want to query for the Alerts, but would like to put it into my own custom type, jsonValue.Json, and not a struct that describes the object contents. jsonValue.Json is essentially a glorified interface{} that implements MarshalJSON() and UnmarshalJSON().

It seems that keyHasGraphQLName should also check for "{" as well as the current "(:@" when it checks to see when the field name is done.

If I have missed something and there Is already some way to accomplish this, great, please let me know.

hgiasac commented 1 month ago

That's expected. The library was originally designed to parse the struct to GraphQL and ensure it can be decoded back to the struct.

var query struct {
    Me struct {
        Name string
    }
}

// query {
//     me {
//      name
//    }
// }

You can't input the entire raw graphql query to the tag. The library can't validate to ensure that the output is a valid GraphQL string. It's better to use the ExecRaw method for your use case.