diff --git a/pkg/jsonutil/graphql.go b/pkg/jsonutil/graphql.go index 69ff527..69546fb 100644 --- a/pkg/jsonutil/graphql.go +++ b/pkg/jsonutil/graphql.go @@ -418,10 +418,9 @@ func keyHasGraphQLName(value, name string) bool { // GraphQL fragment. It doesn't have a name. return false } - if i := strings.Index(value, "("); i != -1 { - value = value[:i] - } - if i := strings.Index(value, ":"); i != -1 { + // Cut off anything that follows the field name, + // such as field arguments, aliases, directives. + if i := strings.IndexAny(value, "(:@"); i != -1 { value = value[:i] } return strings.TrimSpace(value) == name diff --git a/pkg/jsonutil/graphql_test.go b/pkg/jsonutil/graphql_test.go index 2182379..9b48714 100644 --- a/pkg/jsonutil/graphql_test.go +++ b/pkg/jsonutil/graphql_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/hasura/go-graphql-client" "github.com/hasura/go-graphql-client/pkg/jsonutil" ) @@ -449,6 +450,39 @@ func TestUnmarshalGraphQL_multipleValuesInOrderedMap(t *testing.T) { } } +func TestUnmarshalGraphQL_directives(t *testing.T) { + /* + query { + me { + name @include(if: true) + height @skip(if: false) + } + } + */ + type query struct { + Me struct { + Name graphql.String `graphql:"name @include(if: true)"` + Height graphql.Float `graphql:"height @skip(if: false)"` + } + } + var got query + err := jsonutil.UnmarshalGraphQL([]byte(`{ + "me": { + "name": "Luke Skywalker", + "height": 1.72 + } + }`), &got) + if err != nil { + t.Fatal(err) + } + var want query + want.Me.Name = "Luke Skywalker" + want.Me.Height = 1.72 + if !reflect.DeepEqual(got, want) { + t.Error("not equal") + } +} + func TestUnmarshalGraphQL_union(t *testing.T) { /* {