apollographql / apollographql/federation

Federation: unnecessary query

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

Description

Let's say you have `me` query which returns type `User` defined in `Accounts` service. The current user can have some reviews which are served from `Reviews` service.

Let's say current user ID is known based on JWT token in an Authorization header.

When asking for:

```
me {
reviews
}
```

the GQL gateway queries accounts service even though the client does not ask for any attributes from it. This seems unnecessary.

```
QueryPlan {
Sequence {
Fetch(service: "accounts") {
{
me {
__typename
}
}
},
Flatten(path: "me") {
Fetch(service: "reviews") {
{
... on User {
__typename
}
} =>
{
... on User {
reviews {
body
}
}
}
},
},
},
}
```

The expected behavior would be that only a single query to Reviews service is sent and no query gets sent to the Accounts service.

### Checklist for myself

- [x] A short, but descriptive title. The title doesn't need "Apollo" in it.
- [ ] The package name and version of Apollo showing the problem.
- [ ] If applicable, the last version of Apollo where the problem did _not_ occur.
- [ ] The expected behavior.
- [ ] The actual behavior.
- [ ] A **simple**, runnable reproduction!
Please make a GitHub repository that anyone can clone and run and see the
problem. Other great ways to demonstrate a reproduction are using our
CodeSandbox template (https://codesandbox.io/s/apollo-server), or re-mixing
our Glitch template (https://glitch.com/~apollo-launchpad).

Contributor guide

Open the contributing guide

Research direction

Start with the shown QueryPlan for the `me { reviews }` request and trace how the federation gateway decides whether to fetch `Accounts` before `Reviews`. Reproduce the setup with the `Accounts` and `Reviews` services described in the issue, then verify that only `Reviews` is queried and the returned reviews remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
api, backend, distributed-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.