JS_LnMstr
JS_LnMstr

Reputation: 378

GraphQL error: Expected type Int. Int cannot represent non-integer value

I'm currently learning GraphQL with Apollo for React and I have a question that I can't find the answer online: I've done a form to send data for my mongoDB and I have this query:

const addContactMutation = gql`
    mutation($name: String!, $address: String!, $vat: Int!, $zip: String!, $email: String!, $contact: Int!){
        addContact(name: $name, address: $address, vat: $vat, zip: $zip, email: $email, contact: $contact){
            name
            vat
            email
        }
    }
`

However, my $vat and $contact are big numbers and when submit the form I receive this error:

Variable "$vat" got invalid value "232214336"; Expected type Int. Int cannot represent non-integer value

Which type should I use for that?

Upvotes: 2

Views: 27428

Answers (5)

Sarath Baiju
Sarath Baiju

Reputation: 294

I got the same error in my Grapql+NestJs application. What I did is changed the graph field type from Int to Float. It works

Upvotes: 0

mondyfy
mondyfy

Reputation: 534

I also encountered a similar and weird kind of issue, as types were mentioned clearly but that wasn't accepted in variables by graphql-request version 3.1.4, I mapped them again in a variable that is working at my end so far.

const variables = {
    orderId: +orderId,
    vendorIds: vendorIds.map(id => +id),
  };

Upvotes: 0

Anis
Anis

Reputation: 11

I faced this problem before and I resolved it by : first I checked if the order of types in the front-end is the same as in graphql-types then in the input form I replaced: target.value by: target.valueAsNumber

Upvotes: 1

VikR
VikR

Reputation: 5142

This error also happens when a database field that is typed as an integer, contains a value of NULL, and that field is returned by your GraphQL query. One way to fix this, is to set up your database to give integer fields a default value so that they are never NULL when the row is created.

Upvotes: 3

Fraction
Fraction

Reputation: 12984

232214336 is not so big, the max int number is 9007199254740991

console.log(Number.MAX_SAFE_INTEGER);

I think the problem is you've passed a string value "232214336" when an Int value is expected as the error says Expected type Int, so you need to convert the value to an integer before or when passing variables to the mutation using parseInt()

addContact({ variables: { vat: parseInt(vat, 10), contact: parseInt(contact, 10), ... } })

Upvotes: 4

Related Questions