从自省模式生成 graphql typedef 字符串

generate graphql typedef string from introspected schema

我有一个内省模式的结果 JSON 文件,有没有简单的方法:

  1. 使用 graphql-js 摄取?
  2. 转换成typedef的字符串字面量风格?

所以:

   {
     "data": {
       "__schema": {
         "queryType": {
           "name": "Query"
         },
         "types": {
         {
           "kind": "OBJECT",
           "name": "UserNode",
           "description": "",
           "fields": [
           {
             "name": "firstName",
             "description": "",
             "args": [],
             "type": {
               "kind": "SCALAR",
               "name": "String",
               "ofType": null
            },
            "isDeprecated": false,
            "deprecationReason": null
          },
          ...
   }

会变成:

   schema {
     query: Query
   }

   user {
     firstName: String
   }
   ...

如果你有一个 GraphQL 服务器 运行(而不仅仅是 json 模式),那么你可以使用 Apollo 的 graphql-tools gqlschema 来做到这一点:

gqlschema http://localhost:3000/graphql -t

另见 this question