【问题标题】:GraphQL interpolation between queries查询之间的 GraphQL 插值
【发布时间】:2021-05-26 18:11:22
【问题描述】:

我有基本上查询两个数据源的 graphQL 查询。我需要将从查询的第一部分返回的一些数据传递给第二部分。即将config查询返回的hostname传递给GetCertificate查询中的CommonName。例如

query get_config {
  config {
    id
    hostname
  }
  getCertificate(
      CommonName: "siggi") {
    Certificate
    Key
  }
  }

很确定这可以做到,但所有示例似乎都只提供静态变量。

【问题讨论】:

  • 嗯,那个帖子对我来说没有意义。它似乎没有任何解释。
  • 如果 API 不支持,那么根本不可能,发出 2 个请求
  • 那么我们是说 GraphQL 不支持这个?
  • 可以但通常不会

标签: graphql


【解决方案1】:

这是可能的。您需要将证书作为 config 的子级,并使用模式委托将父级解析器返回值传递给子级解析器。

query get_config {
  config {
    id
    hostname
    certificate {
      Certificate
      Key
    }
  }
}

您的架构将查询接受主机名的证书:

  getCertificate(hostname: String) {
    Certificate
    Key
  }

解析器使用 delegateToSchema 函数。

resolvers: {
    config: {
        fragment: `fragment ConfigFragment on config { hostname }`,
        resolve(config, args, context, info) {
            return info.mergeInfo?.delegateToSchema({
                schema: theSchema,
                operation: "query",
                fieldName: "getCertificate",
                args: {
                    hostname: config.hostname,
                },
                context,
                info,
            });
        },
    },
},

有关详细示例,请参阅schema delegation 上的此文档。

【讨论】:

  • 假设是关于 js env 和访问 BE 代码
  • 现在我们正在讨论让我试试这个。
  • 谢谢@glen Thomas。好的,所以我正在使用 Hasura 附带的 GraphiQL 资源管理器。但是,我不确定您将在哪里添加架构并定义解析器。
猜你喜欢
  • 2020-07-02
  • 2019-09-30
  • 2023-03-19
  • 2020-02-03
  • 2020-04-13
  • 2021-12-21
  • 2017-05-25
  • 2018-08-11
  • 2019-12-07
相关资源
最近更新 更多