如何将GraphQLSchema对象转换为类型定义 [英] How to Convert GraphQLSchema Object to Type Definitions

查看:308
本文介绍了如何将GraphQLSchema对象转换为类型定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类似GraphQL的架构:

Let's say i have a GraphQL Schema like:

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

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

You can use this type definition to generate an executable schema that can be queried on.

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

But in case i don't have the above type definitions, but only have GraphQLSchema object obtained through the introspection query, how do i generate the above type definitions?

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

I checked out graphql/utilities which had a printSchema method, that accepts a GraphQLSchema object and prints a string. But i can't seem to filter the output for a particular node alone, since i don't want the entire schema. Parsing the string output is not desirable.

赞赏指出正确的方法.

推荐答案

printSchema 在文档中未提及,但是 graphql-js 导出printType方法正是为此!请参见来源.例如,使用 graphql-iso-date .

Unlike printSchema it is not mentioned in the docs, but graphql-js exports printType method exactly for this! See in the sources. For example with 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参数.

Which can be simply put to makeExecutableSchema functions typeDefs parameter.

这篇关于如何将GraphQLSchema对象转换为类型定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆