graphql - 如何将 GraphQLSchema 对象转换为类型定义
问题描述
假设我有一个 GraphQL Schema,例如:
type author {
id: Int
name: String
rating: Int
}
您可以使用此类型定义来生成可以查询的可执行模式。
但是如果我没有上面的类型定义,而只有通过自省查询获得的 GraphQLSchema 对象,我该如何生成上面的类型定义呢?
我检查了 graphql/utilities 有一个printSchema
方法,它接受一个 GraphQLSchema 对象并打印一个字符串。但我似乎无法单独过滤特定节点的输出,因为我不想要整个架构。解析字符串输出是不可取的。
指出正确的方法值得赞赏。
解决方案
不像printSchema
文档中没有提到它,但是graphql-jsprintType
正是为此导出方法!请参阅来源。例如使用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
参数中。
推荐阅读
- typescript - 从 TypeScript 中的接口获取参数类型
- html - 为什么悬停时颜色不会改变?
- angular - 当我在填写表格时按 enter 时,bs 模式打开。如何预防?
- javascript - 当组件包装在父级中时防止重新安装
- terminal - 为什么终端显示乱码?
- angular - 当我在 IONIC3 中有包含许多项目的可滚动内容时无法打印整个页面
- regex - Kibana 自定义过滤器,如何创建正则表达式以消除所有带有数值的术语
- vba - MS Access VBA - Form.Filter
- python - JSON中的Postgresql'转义序列“\\&”无效'
- google-apps-script - 重新计算工作表后是否正在执行 Google Sheet onEdit 触发器