-
-
Notifications
You must be signed in to change notification settings - Fork 834
Handle federation v2 links #7229
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@graphql-tools/import": patch | ||
| --- | ||
| dependencies updates: | ||
| - Added dependency [`@theguild/federation-composition@^0.18.3` ↗︎](https://www.npmjs.com/package/@theguild/federation-composition/v/0.18.3) (to `dependencies`) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@graphql-tools/import': patch | ||
| --- | ||
|
|
||
| Adjust dependency and definition logic to include federated links |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,22 +39,21 @@ import { | |
| } from 'graphql'; | ||
| import resolveFrom from 'resolve-from'; | ||
| import { parseGraphQLSDL } from '@graphql-tools/utils'; | ||
| import { extractLinkImplementations } from '@theguild/federation-composition'; | ||
|
|
||
| const builtinTypes = ['String', 'Float', 'Int', 'Boolean', 'ID', 'Upload']; | ||
|
|
||
| const federationV1Directives = ['key', 'provides', 'requires', 'external']; | ||
|
|
||
| const builtinDirectives = [ | ||
| 'deprecated', | ||
| 'skip', | ||
| 'include', | ||
| 'cacheControl', | ||
| 'key', | ||
| 'external', | ||
| 'requires', | ||
| 'provides', | ||
| 'connection', | ||
| 'client', | ||
| 'specifiedBy', | ||
| 'link', | ||
| ...federationV1Directives, | ||
| ]; | ||
|
|
||
| const IMPORT_FROM_REGEX = /^import\s+(\*|(.*))\s+from\s+('|")(.*)('|");?$/; | ||
|
|
@@ -264,6 +263,66 @@ export function extractDependencies( | |
| }; | ||
| } | ||
|
|
||
| function importFederatedSchemaLinks( | ||
| definition: DefinitionNode, | ||
| definitionsByName: Map<string, Set<DefinitionNode>>, | ||
| ) { | ||
| const addDefinition = (name: string) => { | ||
| definitionsByName.set(name.replace(/^@/g, ''), new Set()); | ||
| }; | ||
|
|
||
| // extract links from this definition | ||
| const { links, matchesImplementation, resolveImportName } = extractLinkImplementations({ | ||
| kind: Kind.DOCUMENT, | ||
| definitions: [definition], | ||
| }); | ||
|
|
||
| if (links.length) { | ||
| const federationUrl = 'https://specs.apollo.dev/federation'; | ||
| const linkUrl = 'https://specs.apollo.dev/link'; | ||
|
|
||
| /** | ||
| * Official Federated imports are special because they can be referenced without specifyin the import. | ||
| * To handle this case, we must prepare a list of all the possible valid usages to check against. | ||
| * Note that this versioning is not technically correct, since some definitions are after v2.0. | ||
| * But this is enough information to be comfortable not blocking the imports at this phase. It's | ||
| * the job of the composer to validate the versions. | ||
| * */ | ||
| if (matchesImplementation(federationUrl, 'v2.0')) { | ||
| const federationImports = [ | ||
| '@composeDirective', | ||
| '@extends', | ||
| '@external', | ||
| '@inaccessible', | ||
| '@interfaceObject', | ||
| '@key', | ||
| '@override', | ||
| '@provides', | ||
| '@requires', | ||
| '@shareable', | ||
| '@tag', | ||
| 'FieldSet', | ||
| ]; | ||
| for (const i of federationImports) { | ||
| addDefinition(resolveImportName(federationUrl, i)); | ||
| } | ||
| } | ||
| if (matchesImplementation(linkUrl, 'v1.0')) { | ||
| const linkImports = ['Purpose', 'Import', '@link']; | ||
| for (const i of linkImports) { | ||
| addDefinition(resolveImportName(linkUrl, i)); | ||
| } | ||
| } | ||
|
|
||
| const imported = links | ||
| .filter(l => ![linkUrl, federationUrl].includes(l.identity)) | ||
| .flatMap(l => l.imports.map(i => i.as ?? i.name)); | ||
| for (const namedImport of imported) { | ||
| addDefinition(namedImport); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recall that, thankfully, for whatever reason, non-federation packages dont support the namespaced import syntax. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| function visitDefinition( | ||
| definition: DefinitionNode, | ||
| definitionsByName: Map<string, Set<DefinitionNode>>, | ||
|
|
@@ -288,6 +347,7 @@ function visitDefinition( | |
| dependencySet = new Set(); | ||
| dependenciesByDefinitionName.set(definitionName, dependencySet); | ||
| } | ||
|
|
||
| switch (definition.kind) { | ||
| case Kind.OPERATION_DEFINITION: | ||
| visitOperationDefinitionNode(definition, dependencySet); | ||
|
|
@@ -317,9 +377,11 @@ function visitDefinition( | |
| visitScalarDefinitionNode(definition, dependencySet); | ||
| break; | ||
| case Kind.SCHEMA_DEFINITION: | ||
| importFederatedSchemaLinks(definition, definitionsByName); | ||
| visitSchemaDefinitionNode(definition, dependencySet); | ||
| break; | ||
| case Kind.SCHEMA_EXTENSION: | ||
| importFederatedSchemaLinks(definition, definitionsByName); | ||
| visitSchemaExtensionDefinitionNode(definition, dependencySet); | ||
| break; | ||
| case Kind.OBJECT_TYPE_EXTENSION: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,12 @@ | ||
| extend schema @link(url: "https://the-guild.dev/graphql/tools", import: ["@foo"]) | ||
| extend schema | ||
| @link(url: "https://specs.apollo.dev/link/v1.0") | ||
| @link(url: "https://specs.apollo.dev/federation/v2.0", import: ["@key"]) | ||
| @link(url: "https://the-guild.dev/graphql/tools", import: ["@foo"]) | ||
|
|
||
| directive @foo on FIELD_DEFINITION | ||
|
|
||
| extend type User @key(fields: "id") { | ||
| id: ID! | ||
| email: String @foo | ||
| ssn: String @federation__tag(name: "private") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed link from this list per our discussion