Skip to content
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
10 changes: 10 additions & 0 deletions .changeset/link-handling-fixes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@apollo/subgraph': patch
---

Align `@link` handling with composition:

- `@link(as: ...)` is now honoured for the federation link, so elements that aren't imported are namespaced under the alias instead of `federation__`.
- `@link(url: "https://specs.apollo.dev/link/v1.0", import: [...])` now names `Import` and `Purpose` after its imports instead of always using `link__Import` / `link__Purpose`.
- A `@link` URL with a trailing slash after the version (`.../federation/v2.9/`) is now rejected, matching composition.
- A `@link` URL with extra path segments under a known specification (`https://specs.apollo.dev/federation/extra/v1.0`) now links a custom specification (`extra`), as in composition, instead of being read as a federation link.
125 changes: 125 additions & 0 deletions src/__tests__/buildSubgraphSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,30 @@ describe('@link', () => {
).toThrow(UnsupportedFederationVersionError);
});

it('rejects a federation @link url with a trailing slash', () => {
expect(() =>
buildSubgraphSchema(
parse(`${fed2('v2.9/')} type Query { hello: String }`),
),
).toThrow(UnsupportedFederationVersionError);
});

it('treats a url with extra path segments under federation as a custom specification', () => {
const schema = buildSubgraphSchema(
parse(`
${fed2('v2.9', '"@key"')}
extend schema @link(url: "https://specs.apollo.dev/federation/extra/v1.0", import: ["@foo"])
directive @foo on FIELD_DEFINITION
type Product @key(fields: "id") { id: ID! name: String @foo }
`),
);

expect(schema.getDirective('key')).toBeDefined();
expect(
(schema.getType('_Entity') as GraphQLUnionType).getTypes(),
).toHaveLength(1);
});

it('rejects an import the linked version does not define', () => {
expect(() =>
buildSubgraphSchema(
Expand All @@ -363,6 +387,107 @@ describe('@link', () => {
).toThrow(MultipleFederationLinksError);
});

it('namespaces federation definitions under the @link alias', () => {
const schema = buildSubgraphSchema(
parse(`
extend schema @link(url: "https://specs.apollo.dev/federation/v2.9", as: "fed")
type Product @fed__key(fields: "id") { id: ID! }
`),
);

expect(schema.getDirective('fed__key')).toBeDefined();
expect(schema.getDirective('federation__key')).toBeUndefined();
expect(schema.getType('fed__FieldSet')).toBeDefined();
expect(
(schema.getType('_Entity') as GraphQLUnionType).getTypes(),
).toHaveLength(1);
});

it('names link specification types after the link spec imports', () => {
const schema = buildSubgraphSchema(
parse(`
schema @link(url: "https://specs.apollo.dev/link/v1.0", import: ["Import", { name: "Purpose", as: "LinkPurpose" }]) {
query: Query
}
extend schema @link(url: "https://specs.apollo.dev/federation/v2.9", import: ["@key"])
type Query { hello: String }
`),
);

expect(schema.getType('Import')).toBeDefined();
expect(schema.getType('LinkPurpose')).toBeDefined();
expect(schema.getType('link__Import')).toBeUndefined();
expect(schema.getType('link__Purpose')).toBeUndefined();
expect(
schema
.getDirective('link')!
.args.map((arg) => `${arg.name}: ${arg.type}`),
).toEqual([
'url: String',
'as: String',
'for: LinkPurpose',
'import: [Import]',
]);
});

it('namespaces link specification types that are not imported', () => {
const schema = buildSubgraphSchema(
parse(`
schema @link(url: "https://specs.apollo.dev/link/v1.0") { query: Query }
extend schema @link(url: "https://specs.apollo.dev/federation/v2.9", import: ["@key"])
type Query { hello: String }
`),
);

expect(schema.getType('link__Import')).toBeDefined();
expect(schema.getType('link__Purpose')).toBeDefined();
});

it('rejects an import the link specification does not define', () => {
expect(() =>
buildSubgraphSchema(
parse(`
schema @link(url: "https://specs.apollo.dev/link/v1.0", import: ["FieldSet"]) { query: Query }
extend schema @link(url: "https://specs.apollo.dev/federation/v2.9")
type Query { hello: String }
`),
),
).toThrow(
'Cannot import "FieldSet": it is not part of the link specification v1.0.',
);
});

it('names link specification types after the link spec imports without a federation link', () => {
const schema = buildSubgraphSchema(
parse(`
extend schema @link(url: "https://specs.apollo.dev/link/v1.0", import: [{ name: "Purpose", as: "LinkPurpose" }])
type Query { hello: String }
`),
);

expect(schema.getType('LinkPurpose')).toBeDefined();
expect(schema.getType('link__Purpose')).toBeUndefined();
expect(
schema
.getDirective('link')!
.args.find((arg) => arg.name === 'for')!
.type.toString(),
).toBe('LinkPurpose');
});

it('rejects an import the link specification does not define without a federation link', () => {
expect(() =>
buildSubgraphSchema(
parse(`
extend schema @link(url: "https://specs.apollo.dev/link/v1.0", import: ["FieldSet"])
type Query { hello: String }
`),
),
).toThrow(
'Cannot import "FieldSet": it is not part of the link specification v1.0.',
);
});

it('defines @link for a schema that only links a custom specification', () => {
const schema = buildSubgraphSchema(
parse(`
Expand Down
33 changes: 16 additions & 17 deletions src/buildSubgraphSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import {
} from 'graphql';
import { GraphQLSchemaValidationError } from './errors';
import {
FEDERATION_NAMESPACE,
LINK_NAMESPACE,
applyLinkNaming,
findFederationLink,
findLinkSpecImports,
hasLinkDirective,
linkedDirectiveName,
} from './link';
Expand Down Expand Up @@ -310,28 +310,27 @@ function buildSubgraph(inputDocument: DocumentNode): Subgraph {
const specDefinitions: DefinitionNode[] = [];
const specNames = { directives: new Set<string>(), types: new Set<string>() };

// Any `@link` needs the link specification itself, named after the schema's
// link spec imports whether or not it also links federation.
const linkSpec = hasLinkDirective(userDocument)
? applyLinkNaming(
linkSpecDocument(),
findLinkSpecImports(userDocument),
LINK_NAMESPACE,
)
: null;
if (linkSpec) specDefinitions.push(...linkSpec.definitions);

if (link) {
const linkSpec = applyLinkNaming(
linkSpecDocument(),
new Map([['@link', '@link']]),
LINK_NAMESPACE,
);
const federationSpec = applyLinkNaming(
federationSpecDocument(link.version),
link.imports,
FEDERATION_NAMESPACE,
);
specDefinitions.push(
...linkSpec.definitions,
...federationSpec.definitions,
link.namespace,
);
specDefinitions.push(...federationSpec.definitions);
} else {
// Federation 1: no `@link`, so nothing is namespaced or renamed.
// Federation 1: no federation `@link`, so its elements are not renamed.
specDefinitions.push(...federation1SpecDocument().definitions);
if (hasLinkDirective(userDocument)) {
// A `@link` to some non-federation specification still needs `@link` itself.
specDefinitions.push(...linkSpecDocument().definitions);
}
}

collectDefinedNames(specDefinitions, specNames);
Expand All @@ -346,7 +345,7 @@ function buildSubgraph(inputDocument: DocumentNode): Subgraph {
});

const keyDirectiveName = link
? linkedDirectiveName(link.imports, FEDERATION_NAMESPACE, 'key')
? linkedDirectiveName(link.imports, link.namespace, 'key')
: 'key';

const synthesizedDefinitions = synthesizeMissingDefinitions(userDocument);
Expand Down
5 changes: 3 additions & 2 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ export class UnsupportedLinkImportError extends FederationError {
super(message);
}

/** `specification` names what was linked, e.g. `federation specification v2.5`. */
static unknownName(
name: string,
version: string,
specification: string,
): UnsupportedLinkImportError {
return new UnsupportedLinkImportError(
`Cannot import "${name}": it is not part of the federation specification ${version}.`,
`Cannot import "${name}": it is not part of the ${specification}.`,
);
}

Expand Down
Loading
Loading