如何获取所有graphql模式查询、变异、订阅



我使用nodejs与graphql和graphql组合所以我把这个方案定义为

schemaComposer.Query.addFields({
Me: MyResolvers,
Info: MyInfoResolvers,
Post: PostResolvers
})
schemaComposer.Mutation.addFields({
Login: MyLoginResolvers,
Register: RegisterResolvers
})
schemaComposer.Subscription.addFields({
Subscribe: Mysubscribe
})

所以任何函数都可以获取所有的查询、变异和订阅列表数组例如,我需要打印到

[
{ operator: "Me", name: "Query" }
{ operator: "Info", name: "Query" }
{ operator: "Post", name: "Query" }
{ operator: "Login", name: "Mutation" }
{ operator: "Register", name: "Mutation" }
{ operator: "Subscribe", name: "Subscription" }
]

or Object is best
{
Query: ["Me", "Info", "Post"],
Mutation: ["Login", "Register"],
Subscription: ["Subscribe"]
}

我可以使用graphql库中的解析函数解析请求中的查询,以使用获取运算符和名称

import { parse } from "graphql"

const query:any = parse(context.req.body?.query)
const operator = query.definitions[0]?.operation
const name = query.definitions[0]?.selectionSet?.selections[0]?.name?.value

现在我需要列出模式中所有定义的查询、突变、订阅?有可能吗?

感谢

你可以试着打开内省。[docs]

相关内容

  • 没有找到相关文章

最新更新