GraphQL:获取特定实现的子项 [英] GraphQL : Get children of specific implementation

查看:34
本文介绍了GraphQL:获取特定实现的子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个带有children"字段的 GraphQL 对象.该字段的类型为X",有两种不同的实现,Y"和Z".

因此,当我进行查询时,我可以这样做:

<代码>{一号场场二孩子们 {...在 Y {一号场场二}...在 Z {一号场}}}

有没有办法只让孩子使用 Y 实现?

因为如果我这样做:

<代码>{一号场场二孩子们 {...在 Y {一号场场二}}}

我会得到如下所示的对象:

<代码>{一号场场二孩子们 [{},//Z 实现{},//Z 实现{场一,场二},//Y 实现]}

谢谢:)

解决方案

您所看到的是预期行为.来自 规范:

<块引用>

片段可以在选择集中内联定义.这样做是为了根据运行时类型有条件地包含字段.

... on Y 这样的类型条件只是一种过滤基于运行时类型解析哪些字段的方法.它们不是过滤实际结果列表的方法.事实上,GraphQL 中没有内置过滤.

为了过滤列表以仅包含某些类型(或基于任何其他条件),有问题的字段需要有某种过滤参数和解析器逻辑,使用它来实际进行过滤.

如果您使用第三方 API 并且架构不支持过滤,那么作为客户端,除了事后处理响应之外,您无能为力.

I'm getting a GraphQL object with a 'children' field. That field is of the type 'X' and has two different implementations, 'Y' and 'Z'.

So when I'm doing my query I can do this :

{
    fieldOne
    fieldTwo
    children {
        ... on Y {
            fieldOne
            fieldTwo
        }
        ... on Z {
            fieldOne
        }
    }
}

Is there a way for me to only get the children with the Y implementation ?

Because if I'm doing this :

{
    fieldOne
    fieldTwo
    children {
        ... on Y {
            fieldOne
            fieldTwo
        }
    }
}

I will get and object that looks like this :

{
    fieldOne
    fieldTwo
    children [
        {}, // Z implementation
        {}, // Z implementation
        {
            fieldOne,
            fieldTwo
        }, // Y implementation
    ]
}

Thank you :)

解决方案

What you are seeing is expected behavior. From the spec:

Fragments can be defined inline within a selection set. This is done to conditionally include fields based on their runtime type.

Type conditions like ... on Y are just a way to filter what fields are resolved based on the runtime type. They are not a way to filter the actual list of results. In fact, there is no built-in filtering in GraphQL.

In order to filter a list to include only certain types (or based on any other condition), the field in question will need to have some kind of filter argument and resolver logic that uses it to actually do the filtering.

If you're using a third-party API and the schema doesn't support filtering, then as a client there's not much you can do outside of processing the response after the fact.

这篇关于GraphQL:获取特定实现的子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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