graphql-go / graphql

An implementation of GraphQL for Go / Golang
MIT License
9.87k stars 839 forks source link

[Q] Is type assertion check needed for resolver params? #564

Open mrdulin opened 4 years ago

mrdulin commented 4 years ago

Type cast code: https://github.com/graphql-go/graphql/blob/master/examples/todo/main.go#L156

idQuery, isOK := params.Args["id"].(string)

args for type: https://github.com/graphql-go/graphql/blob/master/examples/todo/main.go#L149

Args: graphql.FieldConfigArgument{
    "id": &graphql.ArgumentConfig{
        Type: graphql.String,
    },
},

Since the arguments validation is done in GraphQL schema layer.The id parameter must be a string type in resolver. So I think it's unnecessary to check isOk variable. Am I correct?

Fontinalis commented 4 years ago

In this specific case, since the id argument could be null, isOk can be either true or false. But if you specify the argument's type as String!, which is a non null string type, you'll be right and isOk should be always true.

mrdulin commented 4 years ago

@Fontinalis Thanks. So I am thinking can this package can do this type assertion based on GraphQL fields' type for developer. So we don't need write this thing again and again.