Code Monkey home page Code Monkey logo

Comments (4)

pyrossh avatar pyrossh commented on July 21, 2024

Is this issue fixed?

from graphql.

chris-ramon avatar chris-ramon commented on July 21, 2024

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

Could you confirm this fixes #66 @pyros2097 ?

from graphql.

pyrossh avatar pyrossh commented on July 21, 2024

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

from graphql.

sogko avatar sogko commented on July 21, 2024

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!

from graphql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.