Murakami
Murakami

Reputation: 3760

Passing argument to react-apollo-hooks useQuery

How can I pass an argument to useQuery when using react-apollo-hook?

This does not work:

  const { data, loading, error } = useQuery(GET_DATA,
{ variables: { id: 'testId' }});

And the query itself:

export const GET_DATA = gql`
{ query ($id: String) {
  document(id: $uid) {
    uid
  }  
 }
}
`;

Upvotes: 6

Views: 4487

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84837

Your query is not valid because 1) it's wrapped in an extraneous pair of curly brackets and 2) you reference a variable named uid, while the variable you defined is actually called id. Corrected, this query would look like:

query ($id: String) {
  document(id: $id) {
    uid
  }
}

Keep in mind, I don't know what your actual schema is so this query could still be invalid if any of the fields or arguments do not exist or are of the wrong type.

Upvotes: 6

Related Questions