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

Viewed 550

I have a working graphQL query which looks something like this

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.

1 Answers

It depends on the schema. GraphQL supports interface inheritance. So both Dog and Cat could inherit from, say Pet with the properties id and name. In that scenario you could refer the the parent instead

   ... on Pet {
     id
     name
   }
Related