-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(graphql): Exclude auxiliary types for @extends in Apollo Federation _service queries #9663
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
Open
matthewmcneely
wants to merge
13
commits into
main
Choose a base branch
from
matthewmcneely/graphql-federation-issues
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
13c154a
Restrict generation of extended auxiliary types for apollo federation…
matthewmcneely 5718c83
Fix expected output
matthewmcneely 0e960d6
Add unit test for handling external types in federation
matthewmcneely 5cf816c
Add federation integration test
matthewmcneely 6422a7a
Clean up http response body
matthewmcneely aec648e
Add a nolint directive
matthewmcneely b6272fe
Fix order of has filter generation
matthewmcneely 1ccabfa
Update Apollo service query golden files for extended types
matthewmcneely 7914239
Appease the linter
matthewmcneely 649ab14
Use modern node image
matthewmcneely 5f85586
Remove nolint directives
matthewmcneely 50746d0
Update trunk for new go version
matthewmcneely bdeb5d8
Disable 'De Morgan' checks altogether
matthewmcneely 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
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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # Federation test setup with 2 alpha instances | ||
| # alpha1: user-service subgraph | ||
| # alpha2: reviews-service subgraph | ||
| services: | ||
| alpha1: | ||
| image: dgraph/dgraph:local | ||
| working_dir: /data/alpha1 | ||
| labels: | ||
| cluster: test | ||
| service: alpha1 | ||
| ports: | ||
| - 8080 | ||
| - 9080 | ||
| volumes: | ||
| - type: bind | ||
| source: ${LINUX_GOBIN:-$GOPATH/bin} | ||
| target: /gobin | ||
| read_only: true | ||
| command: | ||
| /gobin/dgraph ${COVERAGE_OUTPUT} alpha --my=alpha1:7080 --zero=zero1:5080 --logtostderr -v=2 | ||
| --raft="idx=1;" --security "whitelist=0.0.0.0/0;" | ||
| alpha2: | ||
| image: dgraph/dgraph:local | ||
| working_dir: /data/alpha2 | ||
| depends_on: | ||
| - alpha1 | ||
| labels: | ||
| cluster: test | ||
| service: alpha2 | ||
| ports: | ||
| - 8080 | ||
| - 9080 | ||
| volumes: | ||
| - type: bind | ||
| source: ${LINUX_GOBIN:-$GOPATH/bin} | ||
| target: /gobin | ||
| read_only: true | ||
| command: | ||
| /gobin/dgraph ${COVERAGE_OUTPUT} alpha --my=alpha2:7080 --zero=zero1:5080 --logtostderr -v=2 | ||
| --raft="idx=2;" --security "whitelist=0.0.0.0/0;" | ||
| zero1: | ||
| image: dgraph/dgraph:local | ||
| working_dir: /data/zero1 | ||
| labels: | ||
| cluster: test | ||
| ports: | ||
| - 5080 | ||
| - 6080 | ||
| volumes: | ||
| - type: bind | ||
| source: ${LINUX_GOBIN:-$GOPATH/bin} | ||
| target: /gobin | ||
| read_only: true | ||
| command: | ||
| /gobin/dgraph ${COVERAGE_OUTPUT} zero --telemetry "reports=false;" --raft="idx=1;" | ||
| --my=zero1:5080 --replicas=1 --logtostderr -v=2 --bindall | ||
| volumes: {} |
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,185 @@ | ||
| //go:build integration | ||
|
|
||
| /* | ||
| * SPDX-FileCopyrightText: © 2017-2026 Istari Digital, Inc. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package federation | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/dgraph-io/dgraph/v25/graphql/e2e/common" | ||
| "github.com/dgraph-io/dgraph/v25/testutil" | ||
| "github.com/dgraph-io/dgraph/v25/x" | ||
| ) | ||
|
|
||
| var ( | ||
| // Alpha1 serves the user-service subgraph | ||
| Alpha1HTTP = testutil.ContainerAddr("alpha1", 8080) | ||
| Alpha1GraphqlURL = "http://" + Alpha1HTTP + "/graphql" | ||
| Alpha1GraphqlAdminURL = "http://" + Alpha1HTTP + "/admin" | ||
|
|
||
| // Alpha2 serves the reviews-service subgraph | ||
| Alpha2HTTP = testutil.ContainerAddr("alpha2", 8080) | ||
| Alpha2GraphqlURL = "http://" + Alpha2HTTP + "/graphql" | ||
| Alpha2GraphqlAdminURL = "http://" + Alpha2HTTP + "/admin" | ||
| ) | ||
|
|
||
| // TestMain sets up the test environment with two Dgraph alpha instances | ||
| func TestMain(m *testing.M) { | ||
| userSchema, err := os.ReadFile("testdata/user-service.graphql") | ||
| x.Panic(err) | ||
|
|
||
| reviewsSchema, err := os.ReadFile("testdata/reviews-service.graphql") | ||
| x.Panic(err) | ||
|
|
||
| // Wait for both alphas to be ready | ||
| err = common.CheckGraphQLStarted(Alpha1GraphqlAdminURL) | ||
| x.Panic(err) | ||
| err = common.CheckGraphQLStarted(Alpha2GraphqlAdminURL) | ||
| x.Panic(err) | ||
|
|
||
| // Bootstrap alpha1 with user-service schema | ||
| common.BootstrapServer(userSchema, nil) | ||
|
|
||
| // Deploy reviews-service schema to alpha2 | ||
| // Retry until schema is successfully deployed | ||
| maxRetries := 30 | ||
| for i := 0; i < maxRetries; i++ { | ||
| resp, updateErr := http.Post("http://"+Alpha2HTTP+"/admin/schema", | ||
| "application/graphql", strings.NewReader(string(reviewsSchema))) | ||
| if updateErr == nil { | ||
| resp.Body.Close() | ||
| if resp.StatusCode == 200 { | ||
| // Wait a bit for schema to be loaded | ||
| time.Sleep(500 * time.Millisecond) | ||
| break | ||
| } | ||
| } | ||
| if resp != nil && resp.Body != nil { | ||
| resp.Body.Close() | ||
| } | ||
| if i == maxRetries-1 { | ||
| x.Panic(fmt.Errorf("failed to deploy schema to alpha2 after %d retries", maxRetries)) | ||
| } | ||
| time.Sleep(200 * time.Millisecond) | ||
| } | ||
|
|
||
| os.Exit(m.Run()) | ||
| } | ||
|
|
||
| // TestRoverFederationComposition tests Apollo Federation supergraph composition | ||
| // with two Dgraph instances serving different federated subgraphs. | ||
| // | ||
| // This test: | ||
| // 1. Fetches SDL via _service query from alpha1 (user-service) and alpha2 (reviews-service) | ||
| // 2. Runs Apollo Rover CLI to compose a supergraph | ||
| // 3. Verifies composition succeeds (auxiliary types should be excluded for @extends types) | ||
| func TestRoverFederationComposition(t *testing.T) { | ||
|
|
||
| // Test that we can run docker | ||
| testCmd := exec.Command("docker", "version") | ||
| if err := testCmd.Run(); err != nil { | ||
| t.Skip("Skipping test: docker is not running or accessible") | ||
| } | ||
|
|
||
| testdataDir := "./testdata" | ||
| absTestdataDir, err := filepath.Abs(testdataDir) | ||
| require.NoError(t, err) | ||
|
|
||
| // Fetch SDL from alpha1 (user-service) | ||
| serviceQuery := &common.GraphQLParams{ | ||
| Query: `query { _service { sdl } }`, | ||
| } | ||
|
|
||
| userServiceResp := serviceQuery.ExecuteAsPost(t, Alpha1GraphqlURL) | ||
| common.RequireNoGQLErrors(t, userServiceResp) | ||
|
|
||
| var userServiceResult struct { | ||
| Service struct { | ||
| SDL string `json:"sdl"` | ||
| } `json:"_service"` | ||
| } | ||
| require.NoError(t, json.Unmarshal(userServiceResp.Data, &userServiceResult)) | ||
|
|
||
| // Fetch SDL from alpha2 (reviews-service) | ||
| reviewsServiceResp := serviceQuery.ExecuteAsPost(t, Alpha2GraphqlURL) | ||
| common.RequireNoGQLErrors(t, reviewsServiceResp) | ||
|
|
||
| var reviewsServiceResult struct { | ||
| Service struct { | ||
| SDL string `json:"sdl"` | ||
| } `json:"_service"` | ||
| } | ||
| require.NoError(t, json.Unmarshal(reviewsServiceResp.Data, &reviewsServiceResult)) | ||
|
|
||
| userSDL := userServiceResult.Service.SDL | ||
| reviewsSDL := reviewsServiceResult.Service.SDL | ||
|
|
||
| // Write the SDL files that Rover will use | ||
| userSDLFile := filepath.Join(testdataDir, "user-service-generated.graphql") | ||
| require.NoError(t, os.WriteFile(userSDLFile, []byte(userSDL), 0644)) | ||
| defer os.Remove(userSDLFile) | ||
|
|
||
| reviewsSDLFile := filepath.Join(testdataDir, "reviews-service-generated.graphql") | ||
| require.NoError(t, os.WriteFile(reviewsSDLFile, []byte(reviewsSDL), 0644)) | ||
| defer os.Remove(reviewsSDLFile) | ||
|
|
||
| // Create supergraph config that points to the generated SDL files | ||
| supergraphConfig := fmt.Sprintf(`federation_version: =2.7.1 | ||
| subgraphs: | ||
| users: | ||
| routing_url: %s | ||
| schema: | ||
| file: ./user-service-generated.graphql | ||
| reviews: | ||
| routing_url: %s | ||
| schema: | ||
| file: ./reviews-service-generated.graphql | ||
| `, Alpha1GraphqlURL, Alpha2GraphqlURL) | ||
| supergraphFile := filepath.Join(testdataDir, "supergraph-generated.yml") | ||
| require.NoError(t, os.WriteFile(supergraphFile, []byte(supergraphConfig), 0644)) | ||
| defer os.Remove(supergraphFile) | ||
|
|
||
| // Run rover supergraph compose using Docker | ||
| ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) | ||
| defer cancel() | ||
|
|
||
| cmd := exec.CommandContext(ctx, "docker", "run", "--rm", | ||
| "-e", "APOLLO_ELV2_LICENSE=accept", | ||
| "-v", absTestdataDir+":/workspace", | ||
| "-w", "/workspace", | ||
| "node:24-slim", | ||
| "sh", "-c", | ||
| `apt-get update -qq && apt-get install -y -qq curl > /dev/null 2>&1 && \ | ||
| curl -sSL https://rover.apollo.dev/nix/latest | sh > /dev/null 2>&1 && \ | ||
| $HOME/.rover/bin/rover supergraph compose --config supergraph-generated.yml`) | ||
|
|
||
| output, err := cmd.CombinedOutput() | ||
| outputStr := string(output) | ||
|
|
||
| // The auxiliary types for extended User should NOT be generated from alpha2 (reviews-service) | ||
| if err != nil { | ||
| if strings.Contains(outputStr, "UserPatch") || | ||
| strings.Contains(outputStr, "UserOrderable") || | ||
| strings.Contains(outputStr, "UserHasFilter") { | ||
| t.Errorf("Rover composition failed with auxiliary type conflicts. "+ | ||
| "The fix should exclude these types for @extends. Error: %v", err) | ||
| } else { | ||
| t.Errorf("Rover composition failed with unexpected error: %v", err) | ||
| } | ||
| } | ||
| } | ||
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,10 @@ | ||
| type Review { | ||
| id: ID! | ||
| rating: Int! @search | ||
| comment: String @search(by: [fulltext]) | ||
| } | ||
|
|
||
| extend type User @key(fields: "userId") { | ||
| userId: ID! @external | ||
| reviews: [Review] | ||
| } |
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 @@ | ||
| type User @key(fields: "userId") { | ||
| userId: ID! | ||
| username: String! @search(by: [hash]) | ||
| email: String | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.