Apollo Server:将参数传递给嵌套的解析器 [英] Apollo Server: pass arguments to nested resolvers

查看:24
本文介绍了Apollo Server:将参数传递给嵌套的解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 GraphQL 查询如下所示:

My GraphQL query looks like this:

{
    p1: property(someArgs: "some_value") {
        id
        nestedField {
            id
            moreNestedField {
                id
            }
        }
    }
}

在服务器端,我使用 Apollo Server.我有 property 的解析器和 nestedFieldmoreNestedField 的其他解析器.我需要在嵌套解析器上检索 someArgs 的值.我尝试使用解析器上可用的 context 来做到这一点:

On the server side, I'm using Apollo Server. I have a resolver for the property and other resolvers for nestedField and moreNestedField. I need to retrieve the value of someArgs on my nested resolvers. I tried to do this using the context available on the resolver:

property: (_, {someArgs}, ctx) => {
    ctx.someArgs = someArgs;

    // Do something
}

但这不会起作用,因为上下文在所有解析器之间共享,因此如果我的查询有多个 property,上下文值将不会很好.

But this won't work as the context is shared among all resolvers, thus if I have multiple propertyon my query, the context value won't be good.

我还尝试在我的嵌套解析器上使用 info 上可用的 path.我可以转到 property 字段,但这里没有参数...

I also tried to use the path available on info on my nested resolvers. I'm able to go up to the property field but I don't have the arguments here...

我也尝试在 info 上添加一些数据,但它没有在嵌套解析器上共享.

I also tried to add some data on info but it's not shared on nested resolvers.

在所有解析器上添加参数不是一种选择,因为它会使查询变得非常臃肿且编写起来很麻烦,我不希望那样.

Adding arguments on all resolvers is not an option as it would make query very bloated and cumbersome to write, I don't want that.

有什么想法吗?

谢谢!

推荐答案

可以使用当前返回的值将参数传递给子解析器.稍后将从响应中删除其他数据.

Params can be passed down to child resolvers using currently returned value. Additional data will be removed from response later.

我将借用"Daniel 的代码,但没有特定参数 - 将 args 作为参考传递(适合/更干净/对于更多 args 更具可读性):

I'll 'borow' Daniel's code, but without specific params - pass args down as reference (suitable/cleaner/more readable for more args):

function propertyResolver (parent, args) {
  const property = await getProperty()
  property.propertyArgs = args
  return property
}

// if this level args required in deeper resolvers
function nestedPropertyResolver (parent, args) {
  const nestedProperty = await getNestedProperty()
  nestedProperty.propertyArgs = parent.propertyArgs
  nestedProperty.nestedPropertyArgs = args
  return nestedProperty
}

function moreNestedPropertyResolver (parent) {
  // do something with parent.propertyArgs.someArgs
}

正如 Daniels 所说,此方法的功能有限.您可以chain 结果并在子解析器中有条件地做一些事情.您将拥有父级和过滤后的子级...未使用子级条件过滤父级(例如在 SQL ... WHERE ... AND ... AND ... 连接表上),这可以在父解析器中完成.

As Daniels stated this method has limited functionality. You can chain results and make something conditionally in child resolver. You'll have parent and filtered children ... not filtered parent using child condition (like in SQL ... WHERE ... AND ... AND ... on joined tables), this can be done in parent resolver.

这篇关于Apollo Server:将参数传递给嵌套的解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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