Skip to content

Commit

Permalink
Merge pull request #33 from vektah/unset-arguments
Browse files Browse the repository at this point in the history
Allow unset arguments
  • Loading branch information
vektah committed Feb 26, 2018
2 parents 9471835 + bc9e0e5 commit 0e78c0a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
23 changes: 23 additions & 0 deletions example/starwars/starwars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,29 @@ func TestStarwars(t *testing.T) {
require.Equal(t, "Leia Organa", resp.Droid.FriendsConnection.Edges[2].Node.Name)
})

t.Run("unset optional arguments", func(t *testing.T) {
var resp struct {
Hero struct {
FriendsConnection struct {
Friends []struct {
Name string
}
}
}
}
query := `
query a($first:Int, $after:ID) {
hero {
friendsConnection(first:$first, after:$after) {
friends { name }
}
}
}`
c.MustPost(query, &resp)

require.Len(t, resp.Hero.FriendsConnection.Friends, 3)
})

t.Run("mutations must be run in sequence", func(t *testing.T) {
var resp struct {
A struct{ Time string }
Expand Down
9 changes: 8 additions & 1 deletion graphql/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/vektah/gqlgen/neelance/common"
"github.com/vektah/gqlgen/neelance/query"
"github.com/vektah/gqlgen/neelance/schema"
)
Expand Down Expand Up @@ -34,7 +35,13 @@ func collectFields(doc *query.Document, selSet []query.Selection, satisfies []st
if len(sel.Arguments) > 0 {
f.Args = map[string]interface{}{}
for _, arg := range sel.Arguments {
f.Args[arg.Name.Name] = arg.Value.Value(variables)
if variable, ok := arg.Value.(*common.Variable); ok {
if val, ok := variables[variable.Name]; ok {
f.Args[arg.Name.Name] = val
}
} else {
f.Args[arg.Name.Name] = arg.Value.Value(variables)
}
}
}
return f
Expand Down

0 comments on commit 0e78c0a

Please sign in to comment.