是否可以将变量放入 GraphQL 标签中?

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

现在我在下面有这个标签。它是静态的,并且总是会收到 ID 为 3 的评论。是否有可能将变量放入此 graphQL 标签中。所以我可以重新使用 graphQL 标签,只需更改变量 ID?

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

提前致谢!

是的,您可以在 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,
      }
    }
`;