如何在嵌套查询的解析器函数中传递根参数?[英] How to pass root parameters in the resolver function of a nested query?

本文是小编为大家收集整理的关于如何在嵌套查询的解析器函数中传递根参数?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有以下性质的查询

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

我的模式就像:

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

,然后类别1Type定义为:

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 },
    })
});

现在,我想在类别1下获取所有类别2项,并具有过滤选项,例如:

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的"分辨率"无法看到或接收rootargss.name让我进行这种过滤.

推荐答案

Resolve函数签名包括4个参数.来自阿波罗的 docs ::

  1. obj:包含从父字段上的解析器返回的结果,或者在顶级查询字段的情况下 root值从服务器配置传递.该参数可以启用 GraphQl查询的嵌套性质.
  2. args:一个带有参数的对象传递到查询中的字段.例如,如果该字段被称为作者(名称:" ADA"),则 args对象将是:{" name":" ada"}.
  3. 上下文:这是特定查询中所有解析器共享的对象,用于包含每次要求状态,包括 身份验证信息,数据加载器实例以及其他任何内容 解决查询时应考虑到这一点.如果你是 使用Apollo服务器,请阅读有关如何在设置中设置上下文的信息 文档.
  4. 信息:此参数仅在高级情况下使用,但它包含有关查询的执行状态的信息,包括 字段名称,从根到字段的路径等等.这只是 在graphql.js源代码中记录.

注意:这些文档适用于GraphQl-Tools'makeExecutableSchema(我强烈建议),但同样适用于普通的旧graphql.js.

这里的要点是,特定字段的解析器通常对其他解析器所做的事情或传递给它们的信息不可知.它已交出了自己的父田间价值,自己的论点,上下文,并期望与之合作.

但是,使用info参数有一个解决方法.传递给信息的对象是巨大的,可以复杂地解析,但实际上包含有关请求查询本身的所有信息.有图书馆可以帮助解析它,但是您可能想打印整个内容以坐下并戳四周(这很酷!).

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

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

并在查询中利用该值.以上非常脆弱,因为它假设您的请求仅包含一个查询,并且您在Category1字段上只有一个参数.实际上,您可能想使用Array.find并按名称查找字段/参数,但这应该给您一个起点.

本文地址:https://www.itbaoku.cn/post/1938034.html

问题描述

I have a query of the following nature

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

My schema is like so:

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

And then the Category1Type is defined as:

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

For simplicity sake, assume category2 is like so:

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

Now I want to fetch all Category2 items under Category1 with option to filter, like so:

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

My resolvers are defined like so:

    # 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 }

Now the problem is that the 'resolver' for cat2Resolve is not able to see or receive the rootargs.name for me to do this kind of filtering.

推荐答案

The resolve function signature includes 4 parameters. From Apollo's 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.

Note: These docs are for graphql-tools' makeExecutableSchema (which I highly recommend) but the same applies to plain old GraphQL.JS.

The key point here is that a resolver for a particular field is generally agnostic to what other resolvers do or what information is passed to them. It's handed its own parent field value, its own arguments, the context and expected to work with that.

However, there is a workaround utilizing the info parameter. The object passed to info is huge and can be complicated to parse, but contains virtually all the information about the requested query itself. There are libraries out to help with parsing it, but you may want to print the whole thing to console and poke around (it's pretty cool!).

Using something like lodash's get, we can then do:

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

and utilize that value inside your query. The above is pretty fragile, since it assumes your request only contains the one query, and you only have one argument on the Category1 field. In practice, you'd probably want to utilize Array.find and look up the fields/arguments by name, but this should give you a starting point.