Skip to content

ID scalar should parse integers into int, not string #474

@Fontinalis

Description

@Fontinalis

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.

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

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions