【问题标题】:How to get all graphql schema query, mutation, subscribe如何获取所有 graphql 模式查询、变异、订阅
【发布时间】:2021-08-24 23:51:18
【问题描述】:

我正在使用带有 graphql 和 graphql-compose 的 nodejs 所以我将方案定义为

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 库中的 parse 函数解析请求中的查询,以获取运算符和名称使用

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

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

谢谢

【问题讨论】:

    标签: node.js graphql express-graphql graphql-compose graphql-compose-mongoose


    【解决方案1】:

    您可以尝试开启内省。 [docs]

    【讨论】:

    • 你能解释一下吗,有什么例子吗? @丹
    • 大多数 graphql 服务器都带有用于测试和构建查询的 Graphiql 接口。这些接口可以自动完成输入,因为图是强类型的。这些接口获取自动完成所需数据的方式是通过自省查询图形服务器自动生成的。我对它们了解不多,但如果有必要,可以使用它们。 IDK OP 正在尝试做什么..
    猜你喜欢
    • 2021-02-23
    • 2020-12-19
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 2022-09-27
    • 1970-01-01
    • 2016-08-18
    相关资源
    最近更新 更多