sveltekit-urql-example
sveltekit-urql-example copied to clipboard
Variable "$first" got invalid value false
Hi, first of all, thanks for this repo which helps me a lot to understand urql, graphql with sveltekit. When running the repo after modifying the graphql endpoint and the query, I run into this issue "GraphQLError:Variable "$first" got invalid value false; Expected type Int; Int cannot represent non-integer value: false" and indeed when i click on the previous posts button, the $first value get false, while everything works great for the next posts button. Here is the query :
const posts_per_page = 5
return {
props: {
posts: await context.query( `query($first: Int, $after: String, $last: Int, $before: String) {
posts(first: $first, after: $after, last: $last, before: $before) {
slug
title
...
}
}`, {
first: (after || !before) && posts_per_page,
after,
last: before && posts_per_page,
before
},
and here are the buttons: previous button
<a href={'/?' + new URLSearchParams({ last: String(posts_per_page), before: $posts.data.pageInfo.startCursor})}>previous posts</a>
<a href={'/?' + new URLSearchParams({ first: String(posts_per_page), after: $posts.data.pageInfo.endCursor})}>next posts</a>
I was wondering if you had encountered this issue already. I guess the problem comes from the line first: (after || !before) && posts_per_page
but I can't figure why.
Thanks a lot for your help here
Yes first: (after || !before) && posts_per_page
seems strange to me too!
But I guess that it can come from: first: String(posts_per_page)
and false in String will do "false" and not "0"?
I'm maybe wrong...
You can also debug by adding in your svelte {posts_per_page} to see what is going on?
Good luck!