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

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

问题描述

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

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

有没有办法让我的孩子只接受Y实施方案?

因为我正在这样做:

{
    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
    ]
}

谢谢:)

推荐答案

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

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.

类型条件,例如Y上的 ... 只是一种根据运行时类型过滤要解析哪些字段的方法.它们不是筛选实际结果列表的方法.实际上, GraphQL中没有内置的过滤器.

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.

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

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天全站免登陆