Skip to content

Commit

Permalink
fix(federation): shared mutation field should not be batched (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan authored Dec 31, 2024
1 parent 47e99b0 commit 3571399
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/smart-wasps-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/federation': patch
---

In case of shared root field on Mutation, it was batched incorrectly across subgraphs. But instead only one mutation should be called as mutations should not be parallel
6 changes: 5 additions & 1 deletion packages/federation/src/supergraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,11 @@ export function getStitchingOptionsFromSupergraphSdl(
return candidates[0].fieldConfig;
}
}
if (candidates.some((candidate) => rootTypeMap.has(candidate.type.name))) {
if (
candidates.some(
(candidate) => rootTypeMap.get(candidate.type.name) === 'query',
)
) {
const defaultMergedField = defaultMerger(candidates);
return {
...defaultMergedField,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { buildSubgraphSchema } from '@apollo/subgraph';
import { normalizedExecutor } from '@graphql-tools/executor';
import { parse } from 'graphql';
import { describe, expect, it } from 'vitest';
import { ExecutionRequest } from '@graphql-tools/utils';
import { ExecutionResult, parse } from 'graphql';
import { describe, expect, it, vi } from 'vitest';
import { getStitchedSchemaFromLocalSchemas } from './getStitchedSchemaFromLocalSchemas';

describe('Aliased Shared Root Fields', () => {
it('issue #6613', async () => {
describe('Shared Root Fields', () => {
it('Aliased shared root fields issue #6613', async () => {
const query = /* GraphQL */ `
query {
testNestedField {
Expand Down Expand Up @@ -109,4 +110,76 @@ describe('Aliased Shared Root Fields', () => {

expect(result).toEqual(expectedResult);
});
it('Mutations should not be batched', async () => {
const SUBGRAPHA = buildSubgraphSchema({
typeDefs: parse(/* GraphQL */ `
type Query {
test: String
}
type Mutation {
testMutation: String
}
`),
resolvers: {
Query: {
test: () => 'test',
},
Mutation: {
testMutation: () => 'testMutation',
},
},
});
const SUBGRAPHB = buildSubgraphSchema({
typeDefs: parse(/* GraphQL */ `
type Query {
test: String
}
type Mutation {
testMutation: String
}
`),
resolvers: {
Query: {
test: () => 'test',
},
Mutation: {
testMutation: () => 'testMutation',
},
},
});
const onSubgraphExecuteFn =
vi.fn<
(
subgraph: string,
executionRequest: ExecutionRequest,
result: ExecutionResult | AsyncIterable<ExecutionResult>,
) => void
>();
const gatewaySchema = await getStitchedSchemaFromLocalSchemas(
{
SUBGRAPHA,
SUBGRAPHB,
},
onSubgraphExecuteFn,
);

const result = await normalizedExecutor({
schema: gatewaySchema,
document: parse(/* GraphQL */ `
mutation {
testMutation
}
`),
});

expect(result).toEqual({
data: {
testMutation: 'testMutation',
},
});

expect(onSubgraphExecuteFn).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 3571399

Please sign in to comment.