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

查看:63
本文介绍了如何在嵌套查询的解析器函数中传递根参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下性质的查询

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

我的模式如下:

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

然后将Category1Type定义为:

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

为简单起见,假定category2像这样:

For simplicity sake, assume category2 is like so:

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

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

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 }

现在的问题是cat2Resolve的'resolver'无法看到或接收rootargs.name来让我进行这种过滤.

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.

推荐答案

resolve函数签名包括4个参数.摘自Apollo的 docs :

The resolve function signature includes 4 parameters. From Apollo's docs:

  1. obj:包含解析器在父字段上返回的结果的对象,或者在顶级查询字段的情况下,包含 从服务器配置传递的rootValue.此参数启用 GraphQL查询的嵌套性质.
  2. args:带有参数的对象,该参数已传递到查询的字段中.例如,如果使用author(name:"Ada")调用了该字段, args对象为:{"name":"Ada"}.
  3. 上下文:这是特定查询中所有解析程序共享的对象,用于包含每个请求的状态,包括 身份验证信息,数据加载器实例以及其他任何内容 解决查询时应考虑到这一点.如果你是 使用Apollo Server,了解如何在设置中设置上下文 文档.
  4. info:仅在高级情况下应使用此参数,但它包含有关查询的执行状态的信息,包括 字段名称,从根到字段的路径等等.这只是 记录在GraphQL.js源代码中.
  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.

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.

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

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!).

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

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

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

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

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.

这篇关于如何在嵌套查询的解析器函数中传递根参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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