【问题标题】:GraphQL - How to get field types from the retrieved schema?GraphQL - 如何从检索到的模式中获取字段类型?
【发布时间】:2020-02-24 13:20:48
【问题描述】:

知道架构(通过getIntrospectionQuery 获取),我如何获得特定字段的类型?

例如,假设我运行这个查询:

query {
  User {
    name
    lastUpdated
    friends {
      name
    }
  }
}

并得到这个结果:

{
  "data": {
    "User": [
      {
        "name": "alice",
        "lastUpdated": "2018-02-03T17:22:49+00:00",
        "friends": []
      },
      {
        "name": "bob",
        "lastUpdated": "2017-09-01T17:08:49+00:00",
        "friends": [
          {
            "name": "eve"
          }
        ]
      }
    ]
  }
}

我想知道字段的类型并构造如下内容:

{
  "name": "String",
  "lastUpdated": "timestamptz",
  "friends": "[Friend]"
}

如果没有对服务器的额外请求,我怎么能做到这一点?

【问题讨论】:

    标签: graphql


    【解决方案1】:

    检索架构后,您可以将其构建为 JSON 对象(如果您的 graphql 框架尚未为您完成)。

    使用 JSON 解析器,您可以检索每个字段的类型。

    我不会详细说明,因为这取决于您使用的技术。

    【讨论】:

      猜你喜欢
      • 2019-07-07
      • 2019-02-05
      • 2021-10-03
      • 2018-12-04
      • 2013-12-03
      • 2018-06-16
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      相关资源
      最近更新 更多