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

Han*_*vad 2 graphql graphql-js graphql-tag

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

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

提前致谢!

Yas*_*nik 6

是的,您可以在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,
      }
    }
`;
Run Code Online (Sandbox Code Playgroud)