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

Apollo federation v2 #2460

Merged
merged 14 commits into from
Nov 27, 2023
Merged

Apollo federation v2 #2460

merged 14 commits into from
Nov 27, 2023

Conversation

cappuc
Copy link
Contributor

@cappuc cappuc commented Oct 30, 2023

This PR adds support for Apollo Federation v2 (except for federated tracing).

I've tested this changes with the apollo compatibility test suite and I will make a PR with the required changes to the test app (here you can see the changes for the compatibility test).

Resolves #2160

  • Added or updated tests
  • Documented user facing changes
  • Updated CHANGELOG.md

Changes

In order to support Federation v2 the subgraph needs to extend the schema with the link to the spec version and import the used directives:

extend schema
  @link(
    url: "https://specs.apollo.dev/federation/v2.3",
    import: [
      "@composeDirective",
      "@extends",
      "@external",
      "@inaccessible",
      "@interfaceObject",
      "@key",
      "@override",
      "@provides",
      "@requires",
      "@shareable",
      "@tag"
    ]
  )

With this implementation the schema extension must be manually added by the user to the graphql schema.

I've added the new directives of v2 but right now there is no check on usage of this new directives without the schema extension.

The other required changes are on the FederationPrinter because we need to print in the service sdl the schema extension and the definition of directives passed to @composeDirective.

Possible improvements

The apollo spec allow to use federation directives without the import, adding the spec prefix:

extend schema
  @link(url: "https://specs.apollo.dev/federation/v2.3")

type Product @federation__key(fields: "id") {
 id: ID!
}

or with alias:

extend schema
  @link(url: "https://specs.apollo.dev/federation/v2.3", as: "fed")

type Product @fed__key(fields: "id") {
 id: ID!
}

and also renaming imported directives:

extend schema
  @link(url: "https://specs.apollo.dev/federation/v2.3", import: [{ name: "@key", as: "@uniqueKey"}])

type Product @uniqueKey(fields: "id") {
 id: ID!
}

This options are not supported right now.

Breaking changes

It should be backward compatible with v1 because the schema extension required for v2 must be manually added by the user.

@spawnia spawnia added the enhancement A feature or improvement label Nov 22, 2023
Copy link
Collaborator

@spawnia spawnia left a comment

Choose a reason for hiding this comment

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

Nicely done, happy to include this.

src/Federation/FederationPrinter.php Outdated Show resolved Hide resolved
src/Federation/SchemaPrinter.php Outdated Show resolved Hide resolved
src/Federation/SchemaPrinter.php Outdated Show resolved Hide resolved
src/Federation/SchemaPrinter.php Outdated Show resolved Hide resolved
src/Schema/AST/DocumentAST.php Outdated Show resolved Hide resolved
src/Schema/AST/DocumentAST.php Show resolved Hide resolved
tests/Integration/Federation/FederationSchemaTest.php Outdated Show resolved Hide resolved
tests/Integration/Federation/FederationSchemaTest.php Outdated Show resolved Hide resolved
tests/Unit/Schema/AST/DocumentASTTest.php Outdated Show resolved Hide resolved
@cappuc cappuc requested a review from spawnia November 23, 2023 14:21
@spawnia
Copy link
Collaborator

spawnia commented Nov 26, 2023

Do the docs need additional updates? Any gotchas regarding how Lighthouse implements the spec?

@cappuc
Copy link
Contributor Author

cappuc commented Nov 27, 2023

I've update the docs with some info of how to enabled v2 support and the unsupported features regarding directive imports

@spawnia spawnia merged commit cd01eef into nuwave:master Nov 27, 2023
21 of 22 checks passed
@spawnia
Copy link
Collaborator

spawnia commented Nov 27, 2023

Thank you very much!

@stayallive
Copy link
Collaborator

Wow, massive effort, thanks for making the PR @cappuc 💪

@cappuc
Copy link
Contributor Author

cappuc commented Nov 27, 2023

I opened the PR to update the apollo compatibility

@cappuc cappuc mentioned this pull request Nov 28, 2023
3 tasks
@stayallive
Copy link
Collaborator

@cappuc I'm playing around with this trying to combine multiple Lighthouse servers into a supergraph and running into that the built-in types are colliding, like the PaginatorInfo type. They should be marked @shareable I guess (which I manually did to make it work).

Did you encounter that too or are you not using this with multiple Lighthouse servers yet?

@cappuc
Copy link
Contributor Author

cappuc commented Dec 1, 2023

I don't use built-in pagination so I didn't notice this issue.
Your guess Is correct, the PaginatorInfo type should be marked as shareable.
I will look at It to find a way to add the directive automatically (if possibile)

@stayallive
Copy link
Collaborator

I'm wondering if it makes sense to just register the @shareable directive always so that internal types can be marked @shareable which shouldn't have any user facing impact except for that one added directive (which doesn't do anything except being there). But maybe I'm thinking about this too easy and there are some downsides of this approach. This would also prevent potentially expensive and non-cacheable AST walks to add the directive after the fact.

Just my 2-cents, love the work you've been doing on the Federation support 💪

@cappuc
Copy link
Contributor Author

cappuc commented Dec 1, 2023

I think that always adding the @shareable will break federation v1.
But maybe it con works if we detect what version of federation is used and drop the v2 only directives when printing the schema.

@cappuc cappuc deleted the feat/apollo-federation-v2 branch December 1, 2023 22:36
@stayallive
Copy link
Collaborator

Ah yes. That makes sense, could also be a configuration setting to make it easier. But forgot that it's backwards compatible of course.

@cappuc
Copy link
Contributor Author

cappuc commented Dec 11, 2023

@stayallive I've opened a PR #2485 to mark pagination types as @shareable.
Did you find other types that should be marked as @shareable?

@stayallive
Copy link
Collaborator

Not yet, but very nice, I don't have a complicated graph but pagination is something I use heavily so maybe there is more, I'm sure someone will open an issue if needed 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Apollo Federation 2
3 participants