Skip to content

"wrong type" error when passing integer as graphql.Time #357

@MaxInertia

Description

@MaxInertia

Version: 38a077b (current master as of post date)

Details to reproduce

Type

type Mutation {
    setTime(
        time: Time!
    ): String!
}

Resolver

func (r RootResolver) SetTime(ctx context.Context, args struct {Time graphql.Time}) (string, error) {
	return "success!", nil
}

Query

mutation {
  setTime(
    time: 1270212221,
  )
}

Error

{
  "errors": [
    {
      "message": "wrong type"
    }
  ],
  "data": {}
}

Cause

On a 64-bit machine int is an alias for int64, so the following switch-case in time.go only catches int64:

	case int:
		t.Time = time.Unix(int64(input), 0)
		return nil

However the value passed in is type int32 in this case

the type of the variable being switched on (value) in this case is int32. So it throws "wrong type". (Not sure why it's int32, didn't do enough digging to find out.)

Fix

This is fixed by replacing that one switch case with these two:

	case int32:
		t.Time = time.Unix(int64(input), 0)
		return nil
	case int64:
		t.Time = time.Unix(input, 0)
		return nil

Any reason I'm not seeing for why the switch needs to catch int instead of int32 and int64? As far as I know int will only ever refer to int32 or int64.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions