【问题标题】:How to format a graphQL query to be called from graphiql client?如何格式化要从 graphiql 客户端调用的 graphQL 查询?
【发布时间】:2020-08-07 12:37:27
【问题描述】:

我是 GraphQL 新手,正在玩一个示例 React-GraphQL-Apollo 项目。当我尝试查询时:

{ 
  getStudentDetails(id: 12763454) {
        node(id:$id) {
            ... on Class_Student {
                id
                name
            }
        }
 }

在我的 GraphiQL 选项卡中,它会抛出 InvalidSyntax 错误。我的代码中有以下内容。不知道我在这里缺少什么。任何帮助表示赞赏!

const querySchema = gql`
    query getStudentDetails($id: ID!) {
        node(id:$id) {
            ... on Class_Student {
                id
                name
            }
        }
    }
`;

const query = graphql(querySchema, {
    options: (props) => ({
        variables: {
            id: props.selectedStudentId
        },
        errorPolicy: "all"
    })
});

【问题讨论】:

    标签: graphql apollo react-apollo


    【解决方案1】:

    在代码中进行查询时,您(通常)无法将要作为参数传递给查询的值硬编码。因此,当您的代码运行正确时,您可以为查询命名并声明一些将传递给它的变量:

    query getStudentDetails($id: ID!)
    

    您可以通过以下两种方式之一在 Graphiql 中传递参数:

    1.

    使用 graphiql 的“查询变量”功能(位于左下角附近)。

    2.

    硬编码你的变量直接架构期望他们:

        query {
            node(id: 12763454) {
                ... on Class_Student {
                    id
                    name
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2018-02-18
      • 2017-09-01
      • 2020-11-12
      • 2017-03-19
      • 2018-03-25
      • 2017-04-07
      • 2020-01-23
      • 2022-11-24
      • 2019-07-26
      相关资源
      最近更新 更多