查询在 GraphiQL 中有效,但在使用 React Apollo 时在实际代码中 returns 空数组
Query works in GraphiQL but returns empty array in Actual Code while using React Apollo
我有一个像这样的简单查询
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
}
}
}
`;
在 GraphiQL 中,returns 结果类似于 -
{
"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"
}
]
}
}
}
但是在使用 React Apollo 的查询组件时,它 returns hashtag
正确但 returns todos
作为一个空数组 [] & 我似乎无法弄清楚查询有什么问题。
如果我从 todos
之外的查询中获取额外的参数,那么它 returns 它们正确但只有 todos
returns 空数组 []
.
我要求它 -
<Query
query={GET_TODOS_BY_PRODUCT}
variables={{ id: state.get("selectedProduct.id") }}
>
{({ data: { product } }) => {
return <Main todos={product.todos} hashtag={product.hashtag} />;
}}
</Query>
而且我似乎无法弄清楚为什么会出现问题?有什么建议吗?
感谢@dkulkarni,我找到了解决方案。在我的 GraphiQL 中,completed
是 true
而在 Apollo 中,我没有设置它,所以它是 false
。
而且我的产品没有任何 completed
等于 false
所以它搞砸了。
再次感谢@dkulkarni 我为这件事浪费了 2 个小时
我有一个像这样的简单查询
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
}
}
}
`;
在 GraphiQL 中,returns 结果类似于 -
{
"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"
}
]
}
}
}
但是在使用 React Apollo 的查询组件时,它 returns hashtag
正确但 returns todos
作为一个空数组 [] & 我似乎无法弄清楚查询有什么问题。
如果我从 todos
之外的查询中获取额外的参数,那么它 returns 它们正确但只有 todos
returns 空数组 []
.
我要求它 -
<Query
query={GET_TODOS_BY_PRODUCT}
variables={{ id: state.get("selectedProduct.id") }}
>
{({ data: { product } }) => {
return <Main todos={product.todos} hashtag={product.hashtag} />;
}}
</Query>
而且我似乎无法弄清楚为什么会出现问题?有什么建议吗?
感谢@dkulkarni,我找到了解决方案。在我的 GraphiQL 中,completed
是 true
而在 Apollo 中,我没有设置它,所以它是 false
。
而且我的产品没有任何 completed
等于 false
所以它搞砸了。
再次感谢@dkulkarni 我为这件事浪费了 2 个小时