Skip to content

Commit 14adabf

Browse files
authored
fix(gateway-runtime): Add subgraphName to ExecutionRequest (#1313)
1 parent 2806fc3 commit 14adabf

File tree

19 files changed

+96
-48
lines changed

19 files changed

+96
-48
lines changed

.changeset/big-dolls-invent.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@graphql-mesh/fusion-runtime': minor
3+
---
4+
5+
Breaking Change: Removed `subgraphNameByExecutionRequest` weak map. Subgraph name is now stored in the execution request itself.
6+
7+
```diff
8+
- const subgraphName = subgraphNameByExecutionRequest.get(executionRequest)
9+
+ const subgraphName = executionRequest.subgraphName
10+
```

.changeset/lovely-turtles-sing.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-tools/delegate': minor
3+
'@graphql-hive/gateway-runtime': minor
4+
---
5+
6+
Added `subgraphName` to `ExecutionRequest` for easier plugin developpment.

.changeset/red-teachers-love.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@graphql-hive/plugin-aws-sigv4': patch
3+
'@graphql-mesh/fusion-runtime': patch
4+
'@graphql-tools/batch-execute': patch
5+
'@graphql-tools/delegate': patch
6+
'@graphql-hive/gateway-runtime': patch
7+
'@graphql-tools/wrap': patch
8+
---
9+
10+
Fixed subgraph name being lost when execution requests get batched together.

e2e/header-propagation/gateway.config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { defineConfig } from '@graphql-hive/gateway';
22

33
export const gatewayConfig = defineConfig({
44
propagateHeaders: {
5-
fromClientToSubgraphs({ request }) {
5+
fromClientToSubgraphs({ request, subgraphName }) {
6+
if (subgraphName !== 'upstream') {
7+
return;
8+
}
9+
610
return {
711
authorization: request.headers.get('authorization') ?? 'default',
812
'session-cookie-id':

e2e/header-propagation/header-propagation.e2e.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,41 @@ it('propagates headers to subgraphs', async () => {
3434
});
3535
});
3636

37+
it('propagates headers to subgraphs with batching', async () => {
38+
await using gw = await gateway({
39+
supergraph: {
40+
with: 'apollo',
41+
services: [await service('upstream')],
42+
},
43+
});
44+
const result = await gw.execute({
45+
query: /* GraphQL */ `
46+
query {
47+
h1: headers {
48+
sessionCookieId
49+
}
50+
h2: headers {
51+
authorization
52+
}
53+
}
54+
`,
55+
headers: {
56+
authorization: 'Bearer token',
57+
'session-cookie-id': 'session-cookie',
58+
},
59+
});
60+
expect(result).toEqual({
61+
data: {
62+
h1: {
63+
sessionCookieId: 'session-cookie',
64+
},
65+
h2: {
66+
authorization: 'Bearer token',
67+
},
68+
},
69+
});
70+
});
71+
3772
it('sends default headers to subgraphs', async () => {
3873
await using gw = await gateway({
3974
supergraph: {

e2e/header-propagation/services/upstream.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ const server = new ApolloServer<ExpressContextFunctionArgument>({
3131
}
3232
3333
type Headers {
34-
authorization: String!
35-
sessionCookieId: String!
34+
authorization: String
35+
sessionCookieId: String
3636
}
3737
`),
3838
resolvers: resolvers as GraphQLResolverMap<unknown>,

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"@graphql-mesh/types": "0.104.6",
6767
"@graphql-mesh/utils": "0.104.6",
6868
"@graphql-tools/delegate": "workspace:^",
69+
"@graphql-tools/utils": "10.9.0-alpha-20250710200000-fde1c74a0c2fa4f651cbeed5b2091aeda7afb162",
6970
"@opentelemetry/otlp-exporter-base@npm:0.202.0": "patch:@opentelemetry/otlp-exporter-base@npm%3A0.202.0#~/.yarn/patches/@opentelemetry-otlp-exporter-base-npm-0.202.0-f6f29c2eeb.patch",
7071
"@rollup/plugin-node-resolve@npm:^15.2.3": "patch:@rollup/plugin-node-resolve@npm%3A16.0.1#~/.yarn/patches/@rollup-plugin-node-resolve-npm-16.0.1-2936474bab.patch",
7172
"@vitest/snapshot": "patch:@vitest/snapshot@npm:3.1.2#~/.yarn/patches/@vitest-snapshot-npm-3.1.1-4d18cf86dc.patch",

packages/batch-execute/src/mergeRequests.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export function mergeRequests(
6161
request: ExecutionRequest,
6262
) => Record<string, any>,
6363
): ExecutionRequest {
64+
const subgraphName = requests[0]!.subgraphName;
6465
const mergedVariables: Record<string, any> = Object.create(null);
6566
const mergedVariableDefinitions: Array<VariableDefinitionNode> = [];
6667
const mergedSelections: Array<SelectionNode> = [];
@@ -114,6 +115,7 @@ export function mergeRequests(
114115
}
115116

116117
return {
118+
subgraphName,
117119
document: {
118120
kind: Kind.DOCUMENT,
119121
definitions: [mergedOperationDefinition, ...mergedFragmentDefinitions],

packages/delegate/src/createRequest.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export function getDelegatingOperation(
3737
}
3838

3939
export function createRequest({
40+
subgraphName,
4041
sourceSchema,
4142
sourceParentType,
4243
sourceFieldName,
@@ -167,6 +168,7 @@ export function createRequest({
167168
};
168169

169170
return {
171+
subgraphName,
170172
document,
171173
variables: newVariables,
172174
rootValue: targetRootValue,

packages/delegate/src/delegateToSchema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export function delegateToSchema<
5454
} = options;
5555

5656
const request = createRequest({
57+
subgraphName: (schema as SubschemaConfig).name,
5758
sourceSchema: info.schema,
5859
sourceParentType: info.parentType,
5960
sourceFieldName: info.fieldName,

0 commit comments

Comments
 (0)