Skip to content

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

Closed
sogko opened this issue Nov 7, 2015 · 4 comments
Assignees

Comments

@sogko
Copy link
Member

sogko commented Nov 7, 2015

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

@ghost
Copy link

ghost commented Feb 24, 2016

Is this issue fixed?

@chris-ramon
Copy link
Member

Well I think so because Validator implementation is done and merged with #71.

Could you confirm this fixes #66 @pyros2097 ?

@ghost
Copy link

ghost commented Feb 24, 2016

Nope this doesn't fix #66 it doesn't hit the scalar parsing functions when I pass the input as variables
This is the updated code

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)
}

@sogko
Copy link
Member Author

sogko commented Jun 1, 2016

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 graphql.Do(params2), you will get the following output:

$ 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:

mutation M($input: ID) {  # Change String! to ID
  ObjectCreate(id: $input) {
    id
  }
}

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.
But please, feel free to open a separate issue if you still encounter issues.

Cheers!

@sogko sogko closed this as completed Jun 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants