Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add directive support #94

Merged
merged 2 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
internal/jsonutil: combine field name separators, add test for '@'
The strings.Index check for "@" is the third separator we look for.
There's no reason to look separately, since we just want the first
field name separator.

Add test covering a GraphQL directive that immediately follows the
field name.
  • Loading branch information
dmitshur committed Jun 6, 2022
commit 41059dec68f051aa8cd11a5cc48420cf5198d595
10 changes: 3 additions & 7 deletions internal/jsonutil/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,9 @@ func hasGraphQLName(f reflect.StructField, 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 {
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
Expand Down
33 changes: 33 additions & 0 deletions internal/jsonutil/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,39 @@ func TestUnmarshalGraphQL_multipleValues(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) {
/*
{
Expand Down