首页 文章

如何将GraphQLSchema对象转换为类型定义

提问于
浏览
1

假设我有一个GraphQL Schema,如:

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

您可以使用此类型定义生成可以查询的可执行模式 .

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

我检查了具有 printSchema 方法的graphql / utilities,它接受GraphQLSchema对象并打印一个字符串 . 但我可以't seem to filter the output for a particular node alone, since i don't想要整个架构 . 解析字符串输出是不可取的 .

指出正确的方法是值得赞赏的 .

1 回答

  • 0

    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 参数 .

相关问题