-
Notifications
You must be signed in to change notification settings - Fork 843
Requests are not validated before execution (validator
has not been implemented)
#67
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
Comments
Is this issue fixed? |
Nope this doesn't fix #66 it doesn't hit the scalar parsing functions when I pass the input as variables package main
import (
"errors"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/kinds"
)
func validate(value string) error {
if len(value) < 3 {
return errors.New("The minimum length required is 3")
}
return nil
}
func main() {
ID := graphql.NewScalar(graphql.ScalarConfig{
Name: "ID",
Serialize: func(value interface{}) interface{} {
println("Serialize")
return value
},
ParseValue: func(value interface{}) interface{} {
println("parsing Value")
var err error
switch value.(type) {
case string:
err = validate(value.(string))
default:
err = errors.New("Must be of type string")
}
if err != nil {
println(err.Error()) // TODO: This panic kills the server
}
return value
},
ParseLiteral: func(valueAst ast.Value) interface{} {
println("parsing literal")
if valueAst.GetKind() == kinds.StringValue {
err := validate(valueAst.GetValue().(string))
if err != nil {
println(err.Error()) // TODO: This panic kills the server
}
return valueAst
} else {
panic("Must be of type string")
}
},
})
ObjectType := graphql.NewObject(graphql.ObjectConfig{
Name: "User",
Description: "A typical user",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: ID,
},
},
})
Schema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"object": &graphql.Field{
Type: ObjectType,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return map[string]interface{}{
"id": "test",
}, nil
},
},
},
}),
Mutation: graphql.NewObject(graphql.ObjectConfig{
Name: "Mutation",
Fields: graphql.Fields{
"ObjectCreate": &graphql.Field{
Type: ObjectType,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: ID,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return map[string]interface{}{
"id": "test",
}, nil
},
},
},
}),
})
if err != nil {
panic(err)
}
// // Returns the right error
// params := graphql.Params{
// Schema: Schema,
// RequestString: `
// mutation M {
// ObjectCreate(id: "t") {
// id
// }
// }
// `,
// // VariableValues: variables,
// }
// graphql.Do(params)
// Does not validate input
params2 := graphql.Params{
Schema: Schema,
RequestString: `
mutation M($input: String!) {
ObjectCreate(id: $input) {
id
}
}
`,
VariableValues: map[string]interface{}{
"input": "t",
},
}
graphql.Do(params2)
} |
Hi @pyros2097 Just going through old open issues. The reason why the Parse/Serialize functions for your Scalar did not run was because there was a validation error in your query. If you print out the results from $ go run main.go
&graphql.Result{
Data: nil,
Errors: {
{
Message: "Variable \"$input\" of type \"String!\" used in position expecting type \"ID\".",
Locations: {
{Line:2, Column:18},
{Line:3, Column:26},
},
},
},
} So simply update your query to:
Running the update query gives the following output: $ go run main.go
parsing Value
The minimum length required is 3
parsing Value
The minimum length required is 3
Serialize
&graphql.Result{
Data: map[string]interface {}{
"ObjectCreate": map[string]interface {}{
"id": "test",
},
},
Errors: nil,
} I'll go ahead and close this issue since your question seems to address a different issue. Cheers! |
While GraphQL spec does indicate the server-side validation is optional, it might be useful to implement it.
It might help with debugging issues that are query-related.
Related issue: #66
The text was updated successfully, but these errors were encountered: