Skip to content

Commit

Permalink
Don't apply @shareable if it's already @shareable (#2043)
Browse files Browse the repository at this point in the history
In the course of updating to fed2, it's feasible that subgraphs may want to add `@shareable` applications without `@link`ing to federation/2.0 and importing it from there.

Previously, this would throw an error because the schema upgrader will try to add a `@shareable` application that has already been manually added. Now we'll check first.
  • Loading branch information
benweatherman authored Aug 11, 2022
1 parent 6c5ed52 commit 3990af4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions composition-js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

This CHANGELOG pertains only to Apollo Federation packages in the 2.x range. The Federation v0.x equivalent for this package can be found [here](https://github.com/apollographql/federation/blob/version-0.x/federation-js/CHANGELOG.md) on the `version-0.x` branch of this repo.

- Don't apply @shareable when upgrading fed1 supergraphs if it's already @shareable [PR #2043](https://github.com/apollographql/federation/pull/2043)
- Update peer dependency `graphql` to `^16.5.0` to use `GraphQLErrorOptions` [PR #2060](https://github.com/apollographql/federation/pull/2060)

## 2.1.0-alpha.3
Expand Down
37 changes: 37 additions & 0 deletions composition-js/src/__tests__/composeFed1Subgraphs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,43 @@ describe('shareable', () => {
}
`);
});

it('supports fed1 subgraphs that define @shareable', () => {
const subgraphA = {
name: 'subgraphA',
typeDefs: gql`
type Queryf {
friendlyFruit: Fruit!
}
directive @shareable on OBJECT | FIELD_DEFINITION
type Fruit @shareable {
id: ID!
name: String!
}
`
};

const subgraphB = {
name: 'subgraphB',
typeDefs: gql`
type Query {
forbiddenFruit: Fruit!
}
directive @shareable on OBJECT | FIELD_DEFINITION
type Fruit @shareable {
id: ID!
name: String!
}
`
};

const result = composeServices([subgraphA, subgraphB]);
assertCompositionSuccess(result);
});
});

describe('override', () => {
Expand Down
4 changes: 2 additions & 2 deletions internals-js/src/schemaUpgrader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,14 +676,14 @@ class SchemaUpgrader {
continue;
}
const otherResolvingSubgraphs = this.otherSubgraphs.filter((s) => resolvesField(s, field));
if (otherResolvingSubgraphs.length > 0) {
if (otherResolvingSubgraphs.length > 0 && !field.hasAppliedDirective(shareableDirective)) {
field.applyDirective(shareableDirective);
this.addChange(new ShareableFieldAddition(field.coordinate, otherResolvingSubgraphs.map((s) => s.name)));
}
}
} else {
const otherDeclaringSubgraphs = this.otherSubgraphs.filter((s) => s.schema.type(type.name));
if (otherDeclaringSubgraphs.length > 0) {
if (otherDeclaringSubgraphs.length > 0 && !type.hasAppliedDirective(shareableDirective)) {
type.applyDirective(shareableDirective);
this.addChange(new ShareableTypeAddition(type.coordinate, otherDeclaringSubgraphs.map((s) => s.name)));
}
Expand Down

0 comments on commit 3990af4

Please sign in to comment.