apollographql / apollographql/federation

Interface type explosion causes error on query

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

Description

**Describe the bug**
Attempting to query an interface with many implementations results in an error.

**To Reproduce**

We have an interface (`FactoryUnit`) with more than 70 implementations. It's defined as follows:

```graphql
interface FactoryUnit {
id: ID!
# ...
unitState: FactoryUnitState
}

type FactoryUnitState {
id: ID!
# ...
}
```

We try to run the following query, which gives an error:

Query:
```graphql
query {
identity(id: "...") {
id
... on FactoryUnit { unitState { id } }
}
}
```

**Expected behavior**
It should not return an error when querying.

**Output**
```json
{
"errors": [
{
"message": "value retrieval failed: router bridge error: the deno runtime raised an error: `request: couldn't receive response couldn't deserialize payload `6476623271848066925`: `deno: couldn't deserialize response : `Error(\"invalid neither null nor empty object: found Object {\\\"code\\\": String(\\\"QUERY_PLANNING_FAILED\\\"), \\\"exception\\\": Object {\\\"stacktrace\\\": Array [String(\\\"RangeError: Invalid array length\\\")]}}\", line: 0, column: 0)``.`",
"extensions": {
"code": "INTERNAL_SERVER_ERROR"
}
}
]
}
```

**Desktop (please complete the following information):**
macOS Monterey, but issue also visible on the server running Alpine Linux 3.16

**Additional context**
We've traced this issue to the following function in Apollo Federation, which appears to be a function with exponential time and space complexity: https://github.com/apollographql/federation/blob/92a9dffdc50f6cbc73aa6225d0536d448cb7bf9a/query-graphs-js/src/graphPath.ts#L2167

It fails when trying to construct an array of length 2^70. Given the comment above `flatCartesianProduct`, it seems that this is a known issue.

Contributor guide

Open the contributing guide

Research direction

Start with query-graphs-js/src/graphPath.ts at line 2167 and inspect the flatCartesianProduct function and its surrounding comment. Reproduce the query against an interface with more than 70 implementations, then verify that query planning completes without the reported Invalid array length error.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
api, backend-api-design
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.