apollographql / apollographql/federation

Federation doesn't support fragment query against a referenced entity that satisfies an interface

Open
#759 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
725
Forks
276
Avg merge
1h 47m
Merged PRs (30d)
1

Description

If I have Service A, which defines entity `Cat`:
```
interface Animal {
id: ID!
}

type Cat implements Animal @key(fields: "id") {
id: ID!
meows: Boolean
}
```
And Service B, which duplicates the interface, and also provides entity `Dog`:
```
interface Animal {
id: ID!
}

type Cat @key(fields: "id") {
id: ID!
}

type Dog implements Animal @key(fields: "id") {
id: ID!
barks: Boolean
}

extend type Query {
animals: [Animal]
}
```
In Service B, we've marked `Cat` as a referenced entity. We've also made a `Dog` that satisfies the same interface.

Service B provides an aggregate query field: `animals`. This should be able to return any entity that satisfies the interface. If Service B creates stubs of `Cat`s in the return value of `animals`, they should resolve to Service A to load each `Cat`.

**Actual behavior**
In this setup, both services come up, and they compose properly. However, when trying to perform this query:

```
animals {
id
...on Cat {
meows
}
...on Dog {
barks
}
}
```

It raises a runtime error:

> Fragment cannot be spread here as objects of type "Animal" can never be of type "Cat".

Even though `Cat` satisfies the interface, its seems like the gateway is getting the `Cat` type entirely from Service B. The gateway thinks `Cat` doesn't satisfy the interface.

**Expected behavior**
- The query should go through with this setup and not error
- Animals should be able to return an array of Dogs, mixed in with an array of Cat stubs and their IDs
- Federation kicks in and the reference resolver in Service A fills in the missing Cat fields

Contributor guide

Open the contributing guide

Research direction

Recreate the two-service federation setup from the issue and run the animals query with inline Cat and Dog fragments. Start by tracing how the gateway represents the referenced Cat type and its Animal interface; done means the query validates, mixed Cat stubs and Dogs are returned, and Cat fields resolve through Service A.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.