7

Right now I have this tag below. It's static and will always get a comment with the id of 3. Is there a possible way to put a variable inside this graphQL-tag. So I can re-use the graphQL-tag, and just change the variable ID?

export const GET_COMMENTS: any = gql`
    {
        comments(id: 3) {
            userId,
            text,
            creationDate,
      }
    }
`;

Thanks in advance!

Yashwardhan Pauranik
  • 5,370
  • 5
  • 42
  • 65

1 Answers1

15

Yeah, you can pass variables in the GQL queries. $xyz is kind of notation or variable name to pass variables in GQL.

export const GET_COMMENTS: any = gql`
    query GET_COMMENTS($id: Int){ // $id is the variable name
        comments(id: $id) {
            userId,
            text,
            creationDate,
      }
    }
`;
Yashwardhan Pauranik
  • 5,370
  • 5
  • 42
  • 65