【问题标题】:How to Convert GraphQLSchema Object to Type Definitions如何将 GraphQLSchema 对象转换为类型定义
【发布时间】:2018-10-13 14:14:47
【问题描述】:

假设我有一个类似的 GraphQL Schema:

type author {
    id: Int
    name: String
    rating: Int  
}

您可以使用此类型定义来生成可以查询的可执行架构。

但是如果我没有上面的类型定义,而只有通过自省查询获得的GraphQLSchema对象,我该如何生成上面的类型定义呢?

我检查了 graphql/utilities ,它有一个 printSchema 方法,它接受一个 GraphQLSchema 对象并打印一个字符串。但我似乎无法单独过滤特定节点的输出,因为我不想要整个架构。解析字符串输出是不可取的。

感谢您指出正确的方法。

【问题讨论】:

    标签: graphql graphql-js


    【解决方案1】:

    printSchema 不同,文档中没有提到它,但graphql-js 导出printType 方法正是为此!请参阅the sources。例如graphql-iso-date

    const { printType } = require('graphql');
    const { GraphQLDate } = require('graphql-iso-date');
    
    console.log(printType(GraphQLDate));
    

    打印出来:

    """
    A date string, such as 2007-12-03, compliant with the `full-date` format
    outlined in section 5.6 of the RFC 3339 profile of the ISO 8601 standard for
    representation of dates and times using the Gregorian calendar.
    """
    scalar Date
    

    这可以简单地放在makeExecutableSchema函数typeDefs参数中。

    【讨论】:

      猜你喜欢
      • 2013-01-15
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-11
      • 1970-01-01
      相关资源
      最近更新 更多