0

Intention:

trying to query from apollo client based on dynamic id. Have successfully checked in server provided interface which is working... and trying to do same from the client.

From the doc it looks like i need to use variables which i did.

Problem:

query using variables looks good but i am getting undefined in client.

Query which is working in graphql API:

query abc {
    getCategoryProduct(id:"NzI1NDc1MTM1") {
      id
      title
      description
      favorited
      published
      price_per_day
      price_per_week
      price_per_month
      price_per_weekend
      picture
      pictures {
        id
        url
      }
      createdAt
      updatedAt
    }
  }

Problematic code in client

const GETDETAILS = gql`
  query abc($id: String!) {
    getCategoryProduct(id: $id) {
      id
      title
      description
      favorited
      published
      price_per_day
      price_per_week
      price_per_month
      price_per_weekend
      picture
      pictures {
        id
        url
      }
      createdAt
      updatedAt
    }
  }
`;

const DetailScreen = () => {

  const { loading, error, data } = useQuery(GETDETAILS, {
    variables: { id: "NzI1NDc1MTM1" },
  });

  useEffect(() => {
    if (loading == false) {
      console.log("=====data=====", data);  // DATA IS EMPTY DO NOT NOT WHY??
    } 
  }, [data]);

}

rosnk
  • 1,068
  • 1
  • 14
  • 36

1 Answers1

0

I was getting the same bug, and it looked like I had tried everything to solve it, including following the instruction in useQuery returns undefined, But returns data on gql playground, but it still didn't work.

Later, I change the variable name—in your case $id—to something else, so it's different from the name in typeDefs (getCategoryProduct(id:ID)), and it now works for me .

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Sam
  • 1
  • 2