Query works in GraphiQL but returns empty array in Actual Code while using React Apollo

跟風遠走 提交于 2021-01-28 21:24:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!