问题
I have a simple query like this
import gql from "graphql-tag";
export const GET_TODOS_BY_PRODUCT = gql`
query getTodosByProduct(
$id: ID!
$completed: Boolean = false
$limit: Int = 100
) {
product(id: $id) {
hashtag
todos(completed: $completed, limit: $limit) {
body
}
}
}
`;
In GraphiQL, it returns results like -
{
"data": {
"product": {
"todos": [
{
"body": "Remove Custom Font from Tweet #a2k",
"product": {
"id": "1439"
},
"__typename": "Todo"
},
{
"body": "Make Site a PWA #a2k",
"product": {
"id": "1439"
},
"__typename": "Todo"
}
]
}
}
}
But while using React Apollo's Query component, it returns the hashtag
properly but returns todos
as an empty array [] & I can't seem to figure out what's wrong with the query.
If I fetch additional parameters from the query outside of todos
then it returns them properly but only todos
returns empty array []
.
I am requesting it like -
<Query
query={GET_TODOS_BY_PRODUCT}
variables={{ id: state.get("selectedProduct.id") }}
>
{({ data: { product } }) => {
return <Main todos={product.todos} hashtag={product.hashtag} />;
}}
</Query>
And I can't seem to figure out why something's wrong? Any suggestions?
回答1:
Thanks to @dkulkarni I found the solution. In my GraphiQL, completed
was true
& in Apollo, I didn't set it up so it was false
.
And my product didn't have any completed
equal to false
so it was messing up.
Thanks again @dkulkarni I wasted 2 hours for this thing 😂
来源:https://stackoverflow.com/questions/51693784/query-works-in-graphiql-but-returns-empty-array-in-actual-code-while-using-react