graphql-go / graphql-go/graphql

ID scalar should parse integers into int, not string

Open
#474 4 comments 6 reactions 0 assignees View on GitHub
question
Dominant language
Go
Stars
10.1k
Forks
845
PR merge metrics
No merged PRs in 30d

Description

> The `ID` scalar type represents a unique identifier, often used to
> refetch an object or as key for a cache. The ID type appears in a JSON
> response as a String; however, it is not intended to be human-readable.
> When expected as an input type, any string (such as `\"4\"`) or integer
> (such as `4`) input value will be accepted as an ID.

The ID scalar type accepts string and integer, but in the `ParseLiteral` function, it returns everything as a string, even the integers.
```go
var ID = NewScalar(ScalarConfig{
Name: "ID",
Description: "The `ID` scalar type represents a unique identifier, often used to " +
"refetch an object or as key for a cache. The ID type appears in a JSON " +
"response as a String; however, it is not intended to be human-readable. " +
"When expected as an input type, any string (such as `\"4\"`) or integer " +
"(such as `4`) input value will be accepted as an ID.",
Serialize: coerceString,
ParseValue: coerceString,
ParseLiteral: func(valueAST ast.Value) interface{} {
switch valueAST := valueAST.(type) {
case *ast.IntValue:
return valueAST.Value
case *ast.StringValue:
return valueAST.Value
}
return nil
},
})
```

I think we should change that and resolve it like in the `Int` scalar's `ParseLiteral`, so a solution would look like this
``` go
func(valueAST ast.Value) interface{} {
switch valueAST := valueAST.(type) {
case *ast.IntValue:
if intValue, err := strconv.Atoi(valueAST.Value); err == nil {
return intValue
}
case *ast.StringValue:
return valueAST.Value
}
return nil
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.