Skip to content

Conversation

@jdolle
Copy link
Collaborator

@jdolle jdolle commented May 27, 2025

Description

extend schema syntax was ignored, causing it to not be included in the final imported schema.
And @link errors if included because of a missing directive definition.

This change fixes both these issues so that a schema such as:

extend schema @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
}

can be imported correctly so that the import result is a valid subgraph definition.

Related # (issue)

#7224

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can
reproduce. Please also list any relevant details for your test configuration

  • See unit tests

Test Environment:

  • OS:
  • @graphql-tools/...:
  • NodeJS:

Checklist:

  • I have followed the
    CONTRIBUTING doc and the
    style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests and linter rules pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Further comments

I opted for a minimal set of changes to resolve this issue. In the long run, we may consider what if federation is using a namespaced import e.g. @federation__key

@jdolle jdolle requested a review from ardatan May 27, 2025 15:33
@jdolle jdolle self-assigned this May 27, 2025
@changeset-bot
Copy link

changeset-bot bot commented May 27, 2025

🦋 Changeset detected

Latest commit: 94276ef

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@graphql-tools/import Patch
@graphql-tools/graphql-file-loader Patch
@graphql-tools/node-require Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 27, 2025

📝 Walkthrough

Summary by CodeRabbit

  • New Features

    • Added support for the 'extend schema' syntax and the use of the 'link' directive as a built-in, enabling more flexible schema extensions in federated GraphQL schemas.
  • Tests

    • Introduced a new test case to verify correct handling of the 'link' directive and schema extensions during schema import.
      """

Summary by CodeRabbit

  • New Features
    • Added support for the 'extend schema' syntax in schema imports, including proper handling of the 'link' directive as a built-in directive.
  • Tests
    • Introduced a new test case to verify correct import and handling of the 'link' directive and custom directives in schema extensions.

Walkthrough

Support for the 'extend schema' syntax and the 'link' directive was added to the GraphQL import package. The import logic was updated to recognize schema extensions, process their directives, and handle the 'link' directive as a built-in. New tests and fixtures verify correct import and traversal of these schema features.

Changes

File(s) Change Summary
.changeset/quick-chairs-cover.md Added a changeset describing support for 'extend schema' and the 'link' directive in federated GraphQL schemas.
packages/import/src/index.ts Updated import logic to handle schema extensions and the 'link' directive; added a visitor for schema extension nodes.
packages/import/tests/schema/fixtures/directive/h.graphql Added a schema fixture demonstrating schema extension with the 'link' directive and a custom directive.
packages/import/tests/schema/import-schema.spec.ts Added a test case verifying import of schema extensions and the 'link' directive.

Sequence Diagram(s)

sequenceDiagram
    participant Importer as importSchema()
    participant File as h.graphql
    participant Parser as parse()
    participant Visitor as visitDefinition()

    Importer->>File: Load schema SDL
    Importer->>Parser: Parse SDL
    Parser->>Visitor: Traverse definitions
    Visitor->>Visitor: Handle SchemaExtensionNode
    Visitor->>Visitor: Process @link directive
    Visitor->>Visitor: Import custom directive (@foo)
    Visitor->>Visitor: Process extended types and fields
    Visitor->>Importer: Return imported schema
Loading

Suggested reviewers

  • ardatan

Poem

In the warren of code, a new path unfurls,
Schema extends, as the 'link' directive twirls.
Directives imported, dependencies found,
With tests and fixtures, the changes abound.
Now federated schemas hop with delight—
The importer's journey is perfectly right!
🐇✨
"""

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/import/src/index.ts

Oops! Something went wrong! :(

ESLint: 9.27.0

ESLint couldn't find an eslint.config.(js|mjs|cjs) file.

From ESLint v9.0.0, the default configuration file is now eslint.config.js.
If you are using a .eslintrc.* file, please follow the migration guide
to update your configuration file to the new format:

https://eslint.org/docs/latest/use/configure/migration-guide

If you still have problems after following the migration guide, please stop by
https://eslint.org/chat/help to chat with the team.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e5b2926 and 94276ef.

📒 Files selected for processing (1)
  • packages/import/src/index.ts (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/import/src/index.ts
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Unit Test on Node 18 (windows-latest) and GraphQL v16
  • GitHub Check: Unit Test on Bun (ubuntu-latest) and GraphQL v15
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions
Copy link
Contributor

github-actions bot commented May 27, 2025

💻 Website Preview

The latest changes are available as preview in: https://pr-7225.graphql-tools.pages.dev

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 340baa3 and e5b2926.

📒 Files selected for processing (4)
  • .changeset/quick-chairs-cover.md (1 hunks)
  • packages/import/src/index.ts (5 hunks)
  • packages/import/tests/schema/fixtures/directive/h.graphql (1 hunks)
  • packages/import/tests/schema/import-schema.spec.ts (1 hunks)
🧰 Additional context used
🪛 GitHub Check: Unit Test on Node 18 (ubuntu-latest) and GraphQL v15
packages/import/src/index.ts

[failure] 275-275:
Argument of type '"OperationDefinition" | "SchemaDefinition" | "SchemaExtension"' is not assignable to parameter of type '"SchemaDefinition" | "SchemaExtension"'.

🪛 GitHub Check: Unit Test on Node 20 (ubuntu-latest) and GraphQL v15
packages/import/src/index.ts

[failure] 275-275:
Argument of type '"OperationDefinition" | "SchemaDefinition" | "SchemaExtension"' is not assignable to parameter of type '"SchemaDefinition" | "SchemaExtension"'.

🪛 GitHub Check: Unit Test on Node 23 (ubuntu-latest) and GraphQL v15
packages/import/src/index.ts

[failure] 275-275:
Argument of type '"OperationDefinition" | "SchemaDefinition" | "SchemaExtension"' is not assignable to parameter of type '"SchemaDefinition" | "SchemaExtension"'.

🪛 GitHub Check: Unit Test on Bun (ubuntu-latest) and GraphQL v15
packages/import/src/index.ts

[failure] 275-275:
Argument of type '"OperationDefinition" | "SchemaDefinition" | "SchemaExtension"' is not assignable to parameter of type '"SchemaDefinition" | "SchemaExtension"'.

🪛 GitHub Check: Unit Test on Node 22 (ubuntu-latest) and GraphQL v15
packages/import/src/index.ts

[failure] 275-275:
Argument of type '"OperationDefinition" | "SchemaDefinition" | "SchemaExtension"' is not assignable to parameter of type '"SchemaDefinition" | "SchemaExtension"'.

🪛 GitHub Check: Type Check on GraphQL v15
packages/import/src/index.ts

[failure] 275-275:
Argument of type '"OperationDefinition" | "SchemaDefinition" | "SchemaExtension"' is not assignable to parameter of type '"SchemaDefinition" | "SchemaExtension"'.

⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: Unit Test on Node 18 (windows-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 23 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 20 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 22 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Unit Test on Node 18 (ubuntu-latest) and GraphQL v16
  • GitHub Check: Full Check on GraphQL v16
  • GitHub Check: deployment
🔇 Additional comments (7)
.changeset/quick-chairs-cover.md (1)

1-7: LGTM! Clear and accurate changeset description.

The changeset correctly describes the patch changes for supporting 'extend schema' syntax and adding 'link' to the built-in directives.

packages/import/tests/schema/fixtures/directive/h.graphql (1)

1-8: LGTM! Comprehensive test fixture for the new functionality.

This fixture effectively demonstrates the key features being added:

  • Schema extension with @link directive
  • Custom directive definitions imported via @link
  • Federation directives working with schema extensions

The fixture provides good test coverage for the import functionality.

packages/import/tests/schema/import-schema.spec.ts (1)

470-482: LGTM! Comprehensive test case for the new functionality.

The test properly verifies that:

  • Schema extensions with @link directives are correctly imported
  • Custom directives referenced in @link imports are processed
  • Federation directives work with schema extensions

The expected SDL output correctly matches the fixture, ensuring the import logic works as intended.

packages/import/src/index.ts (4)

33-33: LGTM! Correct import for schema extension support.

Adding SchemaExtensionNode import is necessary for handling schema extensions.


57-57: LGTM! Correct addition of 'link' to built-in directives.

Adding 'link' to the builtinDirectives array ensures it doesn't require explicit directive definitions in federated schemas, which aligns with the PR objectives.


321-323: LGTM! Correct handling of schema extensions.

Adding the SCHEMA_EXTENSION case that delegates to visitSchemaExtensionDefinitionNode properly integrates schema extension processing into the visitor pattern.


819-825: LGTM! Proper implementation of schema extension visitor.

The visitSchemaExtensionDefinitionNode function correctly:

  • Adds 'schema' dependency like schema definitions
  • Visits directives to track directive dependencies
  • Uses optional chaining for operation types (since they're optional in extensions)

This mirrors the behavior of visitSchemaDefinitionNode appropriately.

@jdolle jdolle merged commit 99e6faf into master May 27, 2025
21 checks passed
@jdolle jdolle deleted the handle-schema-extension-and-links branch May 27, 2025 16:01
@coderabbitai coderabbitai bot mentioned this pull request May 28, 2025
9 tasks
@coderabbitai coderabbitai bot mentioned this pull request Jul 19, 2025
12 tasks
ardatan pushed a commit that referenced this pull request Sep 22, 2025
…#7225)

* Support 'extend schema' syntax and add 'link' to the list of builtins

* Fix typeecheck error

* Use proposed type fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants