Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL large integer error: Int cannot represent non 32-bit signed integer value

I´m trying to store a UNIX timestamp in MongoDB using GraphQL, but it seens that GraphQL has a limit to handle integers. See the mutation below:

const addUser = {
    type: UserType,
    description: 'Add an user',
    args: {
        data: {
            name: 'data',
            type: new GraphQLNonNull(CompanyInputType)
        }
    },
    resolve(root, params) {

        params.data.creationTimestamp = Date.now();

        const model = new UserModel(params.data);
        const saved = model.save();

        if (!saved)
            throw new Error('Error adding user');

        return saved;
    }
}

Result:

  "errors": [
    {
      "message": "Int cannot represent non 32-bit signed integer value: 1499484833027",
      "locations": [
        {
          "line": 14,
          "column": 5
        }
      ],
      "path": [
        "addUser",
        "creationTimestamp"
      ]
    }

I´m currently using GraphQLInteger for this field on type definition:

creationTimestamp: { 
    type: GraphQLInt
}

How can I solve that situation if there is no larger GraphQLInt available in GraphQL ?

like image 756
Mendes Avatar asked Jul 08 '17 03:07

Mendes


2 Answers

GraphQL doesn't support integers larger than 32 bits as the error indicates. You're better off using a custom scalar like GraphQL Date. There's also a "Long" type available here. Or you could roll your own custom type; there's a great example from Apollo here.

If you're curious why GraphQL does not support anything bigger, you can check out this issue on Github.

like image 146
Daniel Rearden Avatar answered Nov 18 '22 22:11

Daniel Rearden


⚠️Not recommended...

... but if you want a quick fix (maybe you don't have the time to implement a custom scalar), you can use the Float type instead of Int.

like image 29
johannchopin Avatar answered Nov 18 '22 21:11

johannchopin