Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve composition hints around types that are strictly @external #2951

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/weak-bottles-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/composition": patch
---

Stop emitting "inconsistent value type" hints against definitions where the type is marked `@external` or all fields are marked `@external`.
84 changes: 83 additions & 1 deletion composition-js/src/__tests__/hints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1353,4 +1353,86 @@ describe('when a directive causes an implicit federation version upgrade', () =>
assertCompositionSuccess(result);
expect(result).toNotRaiseHints();
});
})
});

describe('when a partially-defined type is marked @external or all fields are marked @external', () => {
describe('value types', () => {
it('with type marked @external', () => {
const meSubgraph = gql`
type Query {
me: Account
}

type Account @key(fields: "id") {
id: ID!
name: String
permissions: Permissions
}

type Permissions {
canView: Boolean
canEdit: Boolean
}
`;

const accountSubgraph = gql`
type Query {
account: Account
}

type Account @key(fields: "id") {
id: ID!
permissions: Permissions @external
isViewer: Boolean @requires(fields: "permissions { canView }")
}

type Permissions @external {
canView: Boolean
}
`;

const result = mergeDocuments(meSubgraph, accountSubgraph);
expect(result).toNotRaiseHints();
});

it('with all fields marked @external', () => {
const meSubgraph = gql`
type Query {
me: Account
}

type Account @key(fields: "id") {
id: ID!
name: String
permissions: Permissions
}

type Permissions {
canView: Boolean
canEdit: Boolean
canDelete: Boolean
}
`;

const accountSubgraph = gql`
type Query {
account: Account
}

type Account @key(fields: "id") {
id: ID!
permissions: Permissions @external
isViewer: Boolean @requires(fields: "permissions { canView canEdit }")
}

type Permissions {
canView: Boolean @external
canEdit: Boolean @external
}
`;

const result = mergeDocuments(meSubgraph, accountSubgraph);
expect(result).toNotRaiseHints();
});
});
});
8 changes: 6 additions & 2 deletions composition-js/src/merging/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -965,9 +965,9 @@ class Merger {
typeDescription = 'interface'
break;
}
for (const source of sources) {
for (const [index, source] of sources.entries()) {
// As soon as we find a subgraph that has the type but not the field, we hint.
if (source && !source.field(field.name)) {
if (source && !source.field(field.name) && !this.areAllFieldsExternal(index, source)) {
this.mismatchReporter.reportMismatchHint({
code: hintId,
message: `Field "${field.coordinate}" of ${typeDescription} type "${dest}" is defined in some but not all subgraphs that define "${dest}": `,
Expand Down Expand Up @@ -1081,6 +1081,10 @@ class Merger {
return this.metadata(sourceIdx).isFieldFullyExternal(field);
}

private areAllFieldsExternal(sourceIdx: number, type: ObjectType | InterfaceType): boolean {
return type.fields().every(f => this.isExternal(sourceIdx, f));
}

private validateAndFilterExternal(sources: (FieldDefinition<any> | undefined)[]): (FieldDefinition<any> | undefined)[] {
const filtered: (FieldDefinition<any> | undefined)[] = [];
for (const [i, source] of sources.entries()) {
Expand Down