-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adam Scarr
committed
Jul 24, 2018
1 parent
8098ed8
commit 898526d
Showing
8 changed files
with
311 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package validator | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/vektah/gqlparser/ast" | ||
"github.com/vektah/gqlparser/gqlerror" | ||
) | ||
|
||
// CoerceVariableValues checks the variables for a given operation are valid. mutates variables to include default values where they were not provided | ||
func CoerceVariableValues(schema *ast.Schema, op *ast.OperationDefinition, variables map[string]interface{}) (map[string]interface{}, *gqlerror.Error) { | ||
if variables == nil { | ||
variables = map[string]interface{}{} | ||
} | ||
validator := operationValidator{ | ||
path: []interface{}{"variable"}, | ||
schema: schema, | ||
} | ||
|
||
for _, v := range op.VariableDefinitions { | ||
validator.path = append(validator.path, v.Variable) | ||
|
||
val, found := variables[v.Variable] | ||
if !found { | ||
if v.DefaultValue != nil { | ||
var err error | ||
val, err = v.DefaultValue.Value(variables) | ||
if err != nil { | ||
return nil, gqlerror.WrapPath(validator.path, err) | ||
} | ||
variables[v.Variable] = val | ||
} else if v.Type.NonNull { | ||
return nil, gqlerror.ErrorPathf(validator.path, "must be defined") | ||
} | ||
} | ||
|
||
rv := reflect.ValueOf(val) | ||
if rv.Kind() == reflect.Ptr { | ||
if v.Type.NonNull && rv.IsNil() { | ||
return nil, gqlerror.ErrorPathf(validator.path, "cannot be null") | ||
} | ||
rv = rv.Elem() | ||
} | ||
|
||
if err := validator.validateVarType(v.Type, rv); err != nil { | ||
return nil, err | ||
} | ||
|
||
validator.path = validator.path[0 : len(validator.path)-1] | ||
} | ||
|
||
return variables, nil | ||
} | ||
|
||
type operationValidator struct { | ||
path []interface{} | ||
schema *ast.Schema | ||
} | ||
|
||
func (v *operationValidator) validateVarType(typ *ast.Type, val reflect.Value) *gqlerror.Error { | ||
if typ.Elem != nil { | ||
if val.Kind() != reflect.Slice { | ||
return gqlerror.ErrorPathf(v.path, "must be an array") | ||
} | ||
|
||
for i := 0; i < val.Len(); i++ { | ||
v.path = append(v.path, i) | ||
field := val.Index(i) | ||
|
||
if field.Kind() == reflect.Ptr { | ||
if typ.NonNull && field.IsNil() { | ||
return gqlerror.ErrorPathf(v.path, "cannot be null") | ||
} | ||
field = field.Elem() | ||
} | ||
|
||
err := v.validateVarType(typ.Elem, val.Field(i)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
v.path = v.path[0 : len(v.path)-1] | ||
} | ||
|
||
return nil | ||
} | ||
|
||
def := v.schema.Types[typ.NamedType] | ||
if def == nil { | ||
return gqlerror.ErrorPathf(v.path, "missing definition for %s", typ.String()) | ||
} | ||
|
||
switch def.Kind { | ||
case ast.Scalar, ast.Enum: | ||
// todo scalar coercion, assuming valid for now | ||
case ast.Object: | ||
return gqlerror.ErrorPathf(v.path, "input objects must be used for inputs") | ||
case ast.Interface: | ||
return gqlerror.ErrorPathf(v.path, "interfaces are not currently supported as inputs") | ||
case ast.Union: | ||
return gqlerror.ErrorPathf(v.path, "unions are not currently supported as inputs") | ||
case ast.InputObject: | ||
if val.Kind() != reflect.Map { | ||
return gqlerror.ErrorPathf(v.path, "must be a %s", def.Name) | ||
} | ||
|
||
// check for unknown fields | ||
for _, name := range val.MapKeys() { | ||
val.MapIndex(name) | ||
fieldDef := def.Fields.ForName(name.String()) | ||
v.path = append(v.path, name) | ||
|
||
if fieldDef == nil { | ||
return gqlerror.ErrorPathf(v.path, "unknown field %s", fieldDef) | ||
} | ||
v.path = v.path[0 : len(v.path)-1] | ||
} | ||
|
||
for _, fieldDef := range def.Fields { | ||
v.path = append(v.path, fieldDef.Name) | ||
|
||
field := val.MapIndex(reflect.ValueOf(fieldDef.Name)) | ||
if !field.IsValid() { | ||
if fieldDef.Type.NonNull { | ||
return gqlerror.ErrorPathf(v.path, "must be defined") | ||
} | ||
continue | ||
} | ||
|
||
if field.Kind() == reflect.Ptr { | ||
if typ.NonNull && field.IsNil() { | ||
return gqlerror.ErrorPathf(v.path, "cannot be null") | ||
} | ||
field = field.Elem() | ||
} | ||
|
||
err := v.validateVarType(fieldDef.Type, field) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
v.path = v.path[0 : len(v.path)-1] | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
type Query { | ||
intArg(i: Int!): Boolean! | ||
structArg(i: InputType!): Boolean! | ||
arrayArg(i: [InputType!]): Boolean! | ||
} | ||
|
||
input InputType { | ||
name: String! | ||
} |
Oops, something went wrong.