查询类型为的所有片段



是否有一种方法可以查询字段的所有类型。

示例

{
allPosts {
... on PostType {
title
}
... on Post2Type {
title
}
}
}

将会有两个以上的PostType,所以我想得到的是这个。AllPostTypes是合并的所有PostTypes。

{
allPosts {
... on AllPostTypes {
title
}
}
}

这可能吗?感谢

是的,假设您的模式如下所示:

type Query {
allPosts: [AllPostTypes!]!
}
interface AllPostTypes {
title: String!
}

接口定义了一个或多个字段,实现类型也必须定义。因此,实现AllPostTypes的类型还必须定义title字段。如果您有一个返回AllPostTypes的字段,我们可以使用AllPostTypes作为on条件来请求任何此类公共字段:

{
allPosts {
... on AllPostTypes {
title
}
}
}

然而,在这里传播的碎片是不必要的。因为这些字段对于allPosts返回的任何对象都是通用的,所以我们可以写:

{
allPosts {
title
}
}

任何特定于特定实现类型的字段仍然需要使用片段扩展来添加,不过:

{
allPosts {
title
... on Post2Type {
someOtherField
}
}
}

最新更新