GraphQL variable not working. #225
-
I am using this query. query getSku($take: Int!, $skip: Int!) {
data
} I am calling this like this. const { loading, error } = useQuery(getSku, {
onCompleted: (data) => {
console.log(data);
},
onError: (err) => {
console.error(err);
},
variables: {
take: 10,
skip: 0,
},
}); This is my graphql.query("getSku", (req, res, ctx) => {
const { take, skip } = req.variables;
console.log(req);
[...Array(20)].map(() => db.sku.create());
return res(
ctx.data({
data: db.sku.findMany({
sort: {
skuLength: "asc",
},
take,
skip,
}),
})
);
}), The value of |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Hey, @gajrajantino. I'm sorry you're experiencing this issue. Could you please prepare a small reproduction repository where I could look into it? From your code, everything looks okay. We've got automated tests around GraphQL variables passing as well, so it may be something contextual around your use case. I'd love to look into that. |
Beta Was this translation helpful? Give feedback.
-
If you take a look at the Network tab and inspect the request you're making, you will see that there are no variables being sent by Apollo: If there are no variables in the original request, then MSW cannot possibly know about them. For all the library cares, the variables were intentionally empty. The issue here is in how you're using your const getSku = gql`
query getSku($take: Int!, $skip: Int!) {
+ data
- data(take: $take, skip: $skip)
}
` With this change applied, you can now see that And so your handler can now expose the // src/mocks/handlers.js
export const handlers = [
graphql.query('getSku', (req, res, ctx) => {
const { take, skip } = req.variables
console.log(take) // "10"
console.log(skip) // "0"
})
] |
Beta Was this translation helpful? Give feedback.
If you take a look at the Network tab and inspect the request you're making, you will see that there are no variables being sent by Apollo:
If there are no variables in the original request, then MSW cannot possibly know about them. For all the library cares, the variables were intentionally empty.
The issue here is in how you're using your
$take
and$skip
variables. Looks like Apollo does some query sanitization and if you're not using your variables anywhere in your query, they will be removed. To fix this, forward your variables to any of the fields of your query:With this…