graphql icon indicating copy to clipboard operation
graphql copied to clipboard

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

Open mrdulin opened this issue 4 years ago • 2 comments

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?

mrdulin avatar Aug 12 '20 07:08 mrdulin

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.

Fontinalis avatar Aug 12 '20 08:08 Fontinalis

@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.

mrdulin avatar Aug 12 '20 11:08 mrdulin