简体   繁体   English

GraphQL 查询 - 如何在不同类型上重用内联片段字段

[英]GraphQL query - how to reuse inline fragments field on different types

I have a working graphQL query which looks something like this我有一个工作的 graphQL 查询,它看起来像这样

query( $ownerId: ID!){
    getPets(ownerId:$ownerId){
        id,
        ownerDescription
    ...on Dog {
            id
            name
        }
    ...on Cat {
            id
            name

        }
    }
}

What I want (but do not know how to achieve) is following我想要的(但不知道如何实现)如下

query( $ownerId: ID!){
    getPets(ownerId:$ownerId){
        id,
        ownerDescription
    ...on (Dog | Cat) {
            id
            name
        }
}

Is it even possible to reuse fields, in a way I want?甚至可以以我想要的方式重用字段吗?

I have no control over the schema, so I hope to find a solution that can be implemented in the same place, where I do query.我无法控制模式,所以我希望找到一个可以在我做查询的同一个地方实现的解决方案。

It depends on the schema.这取决于架构。 GraphQL supports interface inheritance. GraphQL 支持接口继承。 So both Dog and Cat could inherit from, say Pet with the properties id and name .所以DogCat都可以继承,比如具有属性idname Pet In that scenario you could refer the the parent instead在那种情况下,您可以改为引用父级

   ... on Pet {
     id
     name
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM