从字段解析器上的父节点获取数据 [英] Get data from parent node on field resolver

查看:14
本文介绍了从字段解析器上的父节点获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果可能的话,我正在尝试做一个像这样的 GraphQL :

<代码>{人们 {_ID名称演过{_ID标题合作者{名称}}}}

所以我正在做的事情是找演员(人),然后找他们演的电影,效果很好.所以我试图让那部电影中的合作演员.我正在考虑将当前演员的 id 作为这样的参数传递给 co-actors 字段:

<代码>{人们 {_ID名称演过{_ID标题合作者(演员:people._id){名称}}}}

显然,我收到了一个错误,不知道是否可以在内部进行.

这里是我的类型:

<前>const MovieType = 新 GraphQLObjectType({name: '电影',字段:() => ({_ID: {类型:GraphQLInt},标题: {类型:GraphQLString},标语: {类型:GraphQLString},发布:{类型:GraphQLInt},演员:{类型:新的 GraphQLList(PersonType),解决:(电影)=> {返回 [];}},合作者:{类型:新的 GraphQLList(PersonType),参数:{演员:{类型:GraphQLInt}},解决:(电影,参数)=> {getCoActorsFor(movie, args.actor)//我该如何做这样的事情.then((演员) => {回归演员;})}}})});const PersonType = new GraphQLObjectType({name: '人',字段:()=> ({_ID: {类型:GraphQLInt},名称: {类型:GraphQLString},出生:{类型:GraphQLInt},扮演:{类型:新的 GraphQLList(MovieType),解决:(人)=> {返回 [];}}})});

解决方案

如果不将查询分成两个查询,这是不可能的,因此您将第一个的结果作为变量提供给第二个..

代替使用变量,您可以让acted"返回的电影对象包含对演员的引用,例如当您要求coactors"时,您手头有该信息来做您想做的事情做.

然而,这种类型的 API 也是一种反模式 - 如果子对象依赖于来自父对象的上下文,那么缓存和理解就会困难得多.我认为您应该问自己为什么 Movie 对象除了返回演员之外还必须返回合作演员.如果 coactors 只是相同的 actor 列表,但删除了原始 actor,这似乎很容易在客户端上发生,因为上下文信息更容易获得.

So what I'm trying to do its to make a GraphQL like this if possible:

{
  people {
    _id
    name
    acted {
      _id
      title
      coactors {
        name
      }
    }
  }
}

So what I'm doing, it's getting actors (people), then get the movies they acted, that works fine. So then I'm trying to get the co-actors in that movie. I'm thinking to pass the current actor's id to the co-actors field as an argument like this:

{
  people {
    _id
    name
    acted {
      _id
      title
      coactors(actor: people._id) {
        name
      }
    }
  }
}

Obviously, I'm getting an error and don't know if that could be made internally.

So here are my Types:

const MovieType = new GraphQLObjectType({
    name: 'movie',
    fields: () => ({
        _id: {
            type: GraphQLInt
        },
        title: {
            type: GraphQLString
        },
        tagline: {
            type: GraphQLString
        },
        released: {
            type: GraphQLInt
        },
        actors: {
            type: new GraphQLList(PersonType),
            resolve: (movie) => {
                return [];
            }
        },
        coactors: {
            type: new GraphQLList(PersonType),
            args: {
                actor: {
                    type: GraphQLInt
                }
            },
            resolve: (movie, args) => {
                getCoActorsFor(movie, args.actor) // How can I do something like this
                    .then((actors) => {
                        return actors;
                    })
            }
        }
    })
});

const PersonType = new GraphQLObjectType({
    name: 'person',
    fields: ()=> ({
        _id: {
            type: GraphQLInt
        },
        name: {
            type: GraphQLString
        },
        born: {
            type: GraphQLInt
        },
        acted: {
            type: new GraphQLList(MovieType),
            resolve: (person) => {

                return [];

            }
        }
    })
});

解决方案

This isn't possible without breaking the query into two queries, so you have the results of the first to supply as variables for the actor to supply to the second.

Instead of using variables, you could have the movies objects returned by "acted" to include a reference to the actor such than when you ask for "coactors" that you have that information at hand to do what you're trying to do.

However this type of API is also an anti pattern - if a subobject relies on context from a parent object then it is much more difficult to cache and understand. I think you should ask yourself why a Movie object must return coactors in addition to actors. If coactors are just the same list of actors but with the original actor removed, that seems like something which could easily happen on the client where that contextual information is far more readily available.

这篇关于从字段解析器上的父节点获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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