Is it possible to put variables inside a GraphQL-tag?
Asked Answered
G

1

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!

Gammon answered 1/10, 2018 at 9:46 Comment(1)
In your code why query GET_COMMENTS is not there? You can check my answer below...Junkie
J
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,
      }
    }
`;
Junkie answered 1/10, 2018 at 9:55 Comment(3)
Thanks a lot! I found out. That i used some of my queries the wrong way. Implemented your method, and now works :-)Gammon
can someone help me with what is wrong here? export const getMeSomething = gql` query GetSomething( $X: ID! $Y: ID! $Z: ID! ) { getSomething( X: $X Y: $Y ) { Funds(filter: { MyID: { eq: $Z } }) { items { Amount MyID } } } } `;Titlark
@Titlark What error are you getting?Junkie

© 2022 - 2024 — McMap. All rights reserved.