graphql-go / graphql-go/graphql
ID scalar should parse integers into int, not string
- 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
Assessment
This issue has not been assessed yet.