gpt4 book ai didi

reactjs - 如何在嵌套查询的解析器函数中传递根参数?

转载 作者:行者123 更新时间:2023-12-01 22:22:44 25 4
gpt4 key购买 nike

我有以下性质的查询

Category1(name: $cat1){
Category2(secondName: $cat2){
secondName
}}

我的架构是这样的:

const Query = new GraphQLObjectType({
name: 'Query',
fields: {
Category1: {
type: new GraphQLList(Category1Type),
args: { name },
resolve: resolveCategory1
}}
})

然后 Category1Type 定义为:

const Category1Type = new GraphQLObjectType({
name: 'Category1',
description: '<>',
fields: () => ({
name: { type: GraphQLString },
category2: {
type: new GraphQLList(CategoryType2),
args: { secondName },
resolve: resolveCategory2
}
})
});

为简单起见,假设类别2如下所示:

const Category2Type = new GraphQLObjectType({
name: 'Category2',
description: '<>',
fields: () => ({
name: { type: GraphQLString },
})
});

现在我想获取 Category1 下的所有 Category2 项目并选择过滤,如下所示:

Category1(name: $name){
name
category2(name: $name){
name
}}

我的解析器定义如下:

    # Category1 resolver
function cat1resolve (root, args) {
return SELECT * from data WHERE category1_name = args.name
}

# Category2 resolver
function cat2Resolve (root, args) {
return SELECT * from data WHERE category1_name = rootargs.name and categort2_name = args.secondName }

现在的问题是 cat2Resolve 的“解析器”无法查看或接收 rootargs.name 来进行此类过滤。

最佳答案

解析函数签名包含 4 个参数。来自 Apollo 的docs :

  1. obj: The object that contains the result returned from the resolver on the parent field, or, in the case of a top-level Query field, the rootValue passed from the server configuration. This argument enables the nested nature of GraphQL queries.
  2. args: An object with the arguments passed into the field in the query. For example, if the field was called with author(name: "Ada"), the args object would be: { "name": "Ada" }.
  3. context: This is an object shared by all resolvers in a particular query, and is used to contain per-request state, including authentication information, dataloader instances, and anything else that should be taken into account when resolving the query. If you’re using Apollo Server, read about how to set the context in the setup documentation.
  4. info: This argument should only be used in advanced cases, but it contains information about the execution state of the query, including the field name, path to the field from the root, and more. It’s only documented in the GraphQL.js source code.

注意:这些文档适用于 graphql-tools 的 makeExecutableSchema (我强烈推荐),但同样适用于普通的旧 GraphQL.JS。

这里的关键点是,特定字段的解析器通常不知道其他解析器做什么或传递给它们的信息。它拥有自己的父字段值、自己的参数、上下文,并预期可以使用它们。

但是,有一个利用 info 参数的解决方法。传递给 info 的对象很大,解析起来可能很复杂,但实际上包含有关所请求的查询本身的所有信息。有一些库可以帮助解析它,但您可能想将整个内容打印到控制台并浏览(这非常酷!)。

使用 lodash 的 get 之类的东西,我们可以这样做:

const category1id = get(info, 'operation.selectionSet.selections[0].arguments[0].value.value')

并在查询中利用该值。上面的内容非常脆弱,因为它假设您的请求仅包含一个查询,并且您在 Category1 字段上只有一个参数。在实践中,您可能希望利用 Array.find 并按名称查找字段/参数,但这应该为您提供一个起点。

关于reactjs - 如何在嵌套查询的解析器函数中传递根参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48094695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com