Skip to content

Commit

Permalink
Merge pull request #172 from PentoHQ/master
Browse files Browse the repository at this point in the history
feat: do not mark __typename field as unknown in the schema validation
  • Loading branch information
StevenACoffman authored Jan 26, 2022
2 parents c55e33b + b1a3d57 commit 994c7e4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
8 changes: 5 additions & 3 deletions validator/vars.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package validator

import (
"fmt"
"reflect"
"strings"

"fmt"

"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
)
Expand Down Expand Up @@ -171,7 +170,10 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) (reflec
resetPath()
v.path = append(v.path, ast.PathName(name.String()))

if fieldDef == nil {
switch {
case name.String() == "__typename":
continue
case fieldDef == nil:
return val, gqlerror.ErrorPathf(v.path, "unknown field")
}
}
Expand Down
17 changes: 15 additions & 2 deletions validator/vars_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package validator_test

import (
"encoding/json"
"io/ioutil"
"testing"

"encoding/json"

"github.com/stretchr/testify/require"

"github.com/vektah/gqlparser/v2"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/validator"
Expand Down Expand Up @@ -127,6 +127,19 @@ func TestValidateVars(t *testing.T) {
require.EqualError(t, gerr, "input: variable.var.foobard unknown field")
})

t.Run("unknown __typefield", func(t *testing.T) {
q := gqlparser.MustLoadQuery(schema, `query foo($var: InputType!) { structArg(i: $var) }`)
vars, gerr := validator.VariableValues(schema, q.Operations.ForName(""), map[string]interface{}{
"var": map[string]interface{}{
"name": "foobar",
"__typename": "InputType",
},
})
require.Nil(t, gerr)
require.EqualValues(t, map[string]interface{}{"__typename": "InputType", "name": "foobar"}, vars["var"])

})

t.Run("enum input object", func(t *testing.T) {
q := gqlparser.MustLoadQuery(schema, `query foo($var: InputType!) { structArg(i: $var) }`)
_, gerr := validator.VariableValues(schema, q.Operations.ForName(""), map[string]interface{}{
Expand Down

0 comments on commit 994c7e4

Please sign in to comment.