apollographql / apollographql/federation

Circular services + nested Entity keys fail to resolve

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

Description

## Issue

When two services refer to/extend *each other's types* and have *nested keys*, federation fails to validate the resulting schema.

### Package Version
@apollo/federation@0.16.9

### Reproduction

I modified Apollo's federation-demo to demonstrate the issue:

Clone: https://github.com/rickhutcheson/federation-demo.git
Follow guide in README (`npm start-services` / `npm start-gateway`)

### Expected Behavior

I would expect to be able to create a schema with two-way references & nested keys. For simplicity, I'll use the types in the `federation-demo` repository.

For example:

1. If we add a compound key `author { id } product { upc }` to our `Review` type in our `reviews` service

```
type Review @key(fields: "id") @key(fields: "author { id } product { upc }" ) {
id: ID!
body: String
author: User
product: Product
}
```

2. Extend the `Review` type in our `accounts` service (a contrived example)

```
extend type Review @key(fields: "id") @key(fields: "author { id } product { upc }" ) {
id: ID! @external
author: User @external
product: Product @external
likedBy: [User]
}
```

I would expect this to result in a valid schema.

### Actual Behavior

Federation fails to start with the following error:

```
[accounts] User -> A @key directive specifies the `id` field which has no matching @external field.
```

The issue _seems_ to be that [`keyFieldsMissingExternal`](https://github.com/apollographql/apollo-server/blob/570f548b88750a06fbf5f67a4abe78fb0f870ccd/packages/apollo-federation/src/composition/validate/preComposition/keyFieldsMissingExternal.ts) is missing the information that `accounts` **owns** the `User` type, so it should _not_ have an `@external` directive, but I'm not that familiar with federation's internals.

Thanks very much!

Contributor guide

Open the contributing guide

Research direction

Start with packages/apollo-federation/src/composition/validate/preComposition/keyFieldsMissingExternal.ts and reproduce the failure using the federation-demo README commands, npm start-services and npm start-gateway. Trace how ownership and nested compound keys are represented during validation. Done means the circular services with nested Entity keys compose and start with a valid schema.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.