是否可以将变量放在GraphQL标签内?

时间:2018-10-01 09:46:54

标签: graphql graphql-js graphql-tag

现在我在下面有这个标签。它是静态的,将始终获得ID为3的注释。是否有可能在此graphQL-tag中放入变量。这样我就可以重新使用graphQL-tag,而只需更改变量ID?

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

谢谢!

1 个答案:

答案 0 :(得分:4)

是的,您可以在 GQL 查询中传递变量。 $xyz是在 GQL 中传递变量的一种符号或变量名。

export const GET_COMMENTS: any = gql`
    query GET_COMMENTS($id: Int){ // $id is the variable name
        comments(id: $id) {
            userId,
            text,
            creationDate,
      }
    }
`;
相关问题