# Federation strategies for interfaces

**URL:** <https://community.apollographql.com/t/federation-strategies-for-interfaces/2470>\
**Category:** Schema Design\
**Tags:** federation\
**Created:** [January 10, 2022, 5:07pm UTC](https://community.apollographql.com/t/federation-strategies-for-interfaces/2470 "2022-01-10T17:07:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alan\_Boshier](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/alan_boshier/32/1236_2.png) [@Alan\_Boshier](https://community.apollographql.com/u/Alan_Boshier)\
**Post date:** [January 10, 2022, 5:07pm UTC](https://community.apollographql.com/t/federation-strategies-for-interfaces/2470/1 "2022-01-10T17:07:27Z")

</div>

I have a subgraph _A_ which defines an interface _X_ and several types _X1_, _X2_, … _Xn_ that implement it.

I want to define a type _T_ in another subgraph _B_ that looks like this:

```graphql
type T {
  x: X!
 ...
}

```

I don’t want subgraph _B_ to need to know what possible types implement _X_ (so for example, I don’t want to declare a union type _X1 | X2 | … | Xn_ for _T.x_ in _B_).

How can I declare and reference _X_ in _B_, and resolve references to it in _A_ to achieve this? Ideally I’d like to do something as simple as returning `{ '__typename': 'X', 'id': <id> }` in my resolver for _T.x_, then leave it up to _A_’s reference resolver to deal with mapping that to an actual type instance

But all of the federation schema composition examples I’ve come across so far are based on types rather than interfaces, so I don’t believe this approach will work.

---

<div class="post-metadata">

**Author:** ![lennyburdette](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/lennyburdette/32/84_2.png) [@lennyburdette](https://community.apollographql.com/u/lennyburdette)\
**Post date:** [January 11, 2022, 9:01pm UTC](https://community.apollographql.com/t/federation-strategies-for-interfaces/2470/2 "2022-01-11T21:01:41Z")

</div>

This is a tough one. Because all subgraphs are valid GraphQL APIs, it’s not possible for a subgraph to use an interface name for \_\_typename like this:

```json
// this is invalid!
{
  "data": {
    "t": {
      "x": {
        "__typename": "X", // interface name
        "id": "123"
      }
    }
  }
}

```

In the current version of federation, our hands are tied. Can you invert the relationship between subgraphs and have the subgraph that knows about the concrete types of `X` provide this field? This should be possible if `T` is an entity:

```nohighlight
interface X {
  id: ID!
}

type A implements X {
  id: ID!
}

extend type T @key(fields: "id") {
  id: ID! @external
  x: X
}

```

There’s some discussion around allowing subgraphs to treat an interface as an object type, and teaching the query planner about jumping from an “interface object” to a concrete type in other subgraphs. But it’s a difficult problem to optimize so we don’t have a timeline for this feature.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Alan\_Boshier](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/alan_boshier/32/1236_2.png) [@Alan\_Boshier](https://community.apollographql.com/u/Alan_Boshier)\
**Post date:** [January 11, 2022, 9:54pm UTC](https://community.apollographql.com/t/federation-strategies-for-interfaces/2470/3 "2022-01-11T21:54:04Z")

</div>

Thanks for your reply @lennyburdette - unfortunately the nature of subgraphs A and B in my example means that its not really possible for A to provide the field (think of this as something like subgraph _A_ defines interface _X_=_Animal_ implemented by _X1_=_Dog_, _X2_=_Cat_, etc., and subgraph _B_ defines type _T_=_Person_ having property _x_=_pet_ - it wouldn’t make sense for _A_ to concern itself with knowing about people, or by extension any other concept where there is a relationship to an animal).

It feels to me that this is likely to be quite a common pattern in monolithic graphs, and that the current `{ __typename: <type>, id: <id> }` “stub pattern” is unsuitable in this case, not only because (as you rightly point out) you can’t assign an interface name to `__typename`, but also because I suspect its quite common that subgraph _B_ isn’t going to know what the actual type is (nor should it have to, because it’s better that _B_ isn’t coupled to _A_ in that way - I want to be able to add new implementations of _X_ to _A_ without the need to change _B_).

In essence what I need is for _B_ to be able to return the pseudo-reference `{ _interfacename: 'X', id: <id> }`, and for _A_ to be able to declare a reference resolver on the **interface** _X_ that, given an id, can return the appropiate concrete instance of some type implementing _X_.

---

<div class="post-metadata">

**Author:** ![lennyburdette](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/lennyburdette/32/84_2.png) [@lennyburdette](https://community.apollographql.com/u/lennyburdette)\
**Post date:** [January 11, 2022, 10:37pm UTC](https://community.apollographql.com/t/federation-strategies-for-interfaces/2470/4 "2022-01-11T22:37:21Z")

</div>

Agreed! I actually filed the oldest issue about this topic: [federation: queries for interface types require knowledge of concrete implementations · Issue #336 · apollographql/federation · GitHub](https://github.com/apollographql/federation/issues/336)

Keep an eye on the Apollo blog for news about Federation 2. Again, I don’t know the timeline on addressing this concern but we’re definitely working on it.

---

<div class="post-metadata">

**Author:** ![Alan\_Boshier](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/alan_boshier/32/1236_2.png) [@Alan\_Boshier](https://community.apollographql.com/u/Alan_Boshier)\
**Post date:** [January 12, 2022, 8:59am UTC](https://community.apollographql.com/t/federation-strategies-for-interfaces/2470/5 "2022-01-12T08:59:03Z")

</div>

Thanks for the issue pointer; I’ve added my 2 cents to the discussion on that because this seems to me like a pretty significant feature for graph decomposition.
