/**
 * Variable default values of correct type
 *
 * A GraphQL document is only valid if all variable default values are of the
 * type expected by their definition.
 */
func DefaultValuesOfCorrectType(context *validation.Context) RuleVisitor {
	return RuleVisitor{
		Enter: func(node lang.INode, info lang.VisitInfo) *lang.GraphQLError {
			if varDefAST, ok := node.(lang.VariableDefinition); ok {
				name := varDefAST.Variable.Name.Value
				defaultValue := varDefAST.DefaultValue
				typ := context.GetInputType()
				if typ, ok := typ.(typs.GraphQLNonNull); ok {
					return lang.NewGraphQLError(
						defaultForNonNullArgMessage(name, typ, typ.OfType),
						[]lang.INode{defaultValue})
				}
				if typ != nil && !util.IsValidLiteralValue(typ, defaultValue) {
					return lang.NewGraphQLError(
						badValueForDefaultArgMessage(name, typ, lang.Print(defaultValue)),
						[]lang.INode{defaultValue})
				}
			}
			return nil
		},
	}
}
Пример #2
0
func (r *Request) Print() string {
	return language.Print(r.documentAST)
}