首页 文章

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

提问于
浏览
1

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

  • 使用graphql-js摄取?

  • 将其转换为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
   }
   ...

1 回答

  • 1

    如果您运行的是GraphQL服务器(而不仅仅是json架构),那么您可以使用Apollo的graphql-tools中的gqlschema来执行此操作:

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

    另见this question .

相关问题