#graphql pagination variable type issue

5 messages · Page 1 of 1 (latest)

olive sky
#

Using V2 graphql - having a problem where the passed variable is not an Int so the page query doesn't work:

[{"message":"Variable \"$page\" of required type \"Int!\" was not provided.","locations":[{"line":2,"column":16}]}]

PAGINATEDPOSTS Query:

    Posts(limit: 9, page: $page) {
      docs {
        title
        slug
      }
      totalPages
      page
    }
  }```

Fetch request:
```const { data, errors } = await fetch(`${process.env.NEXT_PUBLIC_CMS_URL}/api/graphql`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      query: PAGINATEDPOSTS,
      variable: {
        page: pageNum,
      },
    }),
    next: {
      tags: ['posts'],
    },
  }).then(res => res.json())```

`pageNum` comes from next url param such as `1` - this happens on playground also if the input variable is given as a string (double quotes) but not when given as an Int or single quotes will return the correct query but ignores the page variable entirely.

Not sure if I am doing something wrong, or if this is a bug?
keen streamBOT
zenith anvil
#

i am not sure i fully understnad, couldnt you just parseInt it before putting it in body of fetch?

olive sky
#

I did try this (just in case I was being silly) but this doesn't change the error, presumably because we're stringifying the body (as we must) - but at this point I might just be overcooking because I am very confused why this wouldn't work.

olive sky
#

I am an idiot - it's variables not variable - simple typo!