-
Notifications
You must be signed in to change notification settings - Fork 332
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
244 additions
and
155 deletions.
There are no files selected for viewing
This file contains 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,6 @@ | ||
--- | ||
'@graphql-mesh/fusion-runtime': patch | ||
'@graphql-mesh/serve-runtime': patch | ||
--- | ||
|
||
Header Propagation |
This file contains 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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,81 @@ | ||
import { mapMaybePromise } from '@envelop/core'; | ||
import { subgraphNameByExecutionRequest } from '@graphql-mesh/fusion-runtime'; | ||
import type { TransportEntry } from '@graphql-mesh/transport-common'; | ||
import type { OnFetchHookDone } from '@graphql-mesh/types'; | ||
import type { MaybePromise } from '@graphql-tools/utils'; | ||
import type { GatewayPlugin } from '../types'; | ||
|
||
interface FromClientToSubgraphsPayload { | ||
request: Request; | ||
subgraphName: string; | ||
} | ||
|
||
interface FromSubgraphsToClientPayload { | ||
response: Response; | ||
subgraphName: string; | ||
} | ||
|
||
export interface PropagateHeadersOpts { | ||
fromClientToSubgraphs?: ( | ||
payload: FromClientToSubgraphsPayload, | ||
) => Record<string, string> | void | Promise<Record<string, string | null | undefined> | void>; | ||
fromSubgraphsToClient?: ( | ||
payload: FromSubgraphsToClientPayload, | ||
) => Record<string, string> | void | Promise<Record<string, string | null | undefined> | void>; | ||
} | ||
|
||
export function usePropagateHeaders<TContext>(opts: PropagateHeadersOpts): GatewayPlugin<TContext> { | ||
const resHeadersByRequest = new WeakMap<Request, Record<string, string>>(); | ||
return { | ||
onFetch({ executionRequest, context, options, setOptions }) { | ||
const subgraphName = subgraphNameByExecutionRequest.get(executionRequest); | ||
if (subgraphName != null) { | ||
let job: Promise<void> | void; | ||
if (opts.fromClientToSubgraphs) { | ||
job = mapMaybePromise( | ||
opts.fromClientToSubgraphs({ | ||
request: context.request, | ||
subgraphName, | ||
}), | ||
headers => | ||
setOptions({ | ||
...options, | ||
headers: { | ||
...headers, | ||
...options.headers, | ||
}, | ||
}), | ||
); | ||
} | ||
return mapMaybePromise(job, (): OnFetchHookDone => { | ||
if (opts.fromSubgraphsToClient) { | ||
return function onFetchDone({ response }) { | ||
return mapMaybePromise( | ||
opts.fromSubgraphsToClient({ | ||
response, | ||
subgraphName, | ||
}), | ||
headers => { | ||
if (headers) { | ||
resHeadersByRequest.set(context.request, headers); | ||
} | ||
}, | ||
); | ||
}; | ||
} | ||
}); | ||
} | ||
}, | ||
onResponse({ response, request }) { | ||
const headers = resHeadersByRequest.get(request); | ||
if (headers) { | ||
for (const key in headers) { | ||
const value = headers[key]; | ||
if (value) { | ||
response.headers.set(key, value); | ||
} | ||
} | ||
} | ||
}, | ||
}; | ||
} |
This file contains 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 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,142 @@ | ||
import { createSchema, createYoga, type Plugin } from 'graphql-yoga'; | ||
import { useCustomFetch } from '@graphql-mesh/serve-runtime'; | ||
import { createGatewayRuntime } from '../src/createGatewayRuntime'; | ||
|
||
describe('usePropagateHeaders', () => { | ||
describe('From Client to the Subgraphs', () => { | ||
const requestTrackerPlugin = { | ||
onParams: jest.fn((() => {}) as Plugin['onParams']), | ||
}; | ||
const upstream = createYoga({ | ||
schema: createSchema({ | ||
typeDefs: /* GraphQL */ ` | ||
type Query { | ||
hello: String | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
hello: () => 'world', | ||
}, | ||
}, | ||
}), | ||
plugins: [requestTrackerPlugin], | ||
logging: !!process.env.DEBUG, | ||
}); | ||
beforeEach(() => { | ||
requestTrackerPlugin.onParams.mockClear(); | ||
}); | ||
it('forwards specified headers', async () => { | ||
await using serveRuntime = createGatewayRuntime({ | ||
proxy: { | ||
endpoint: 'http://localhost:4001/graphql', | ||
}, | ||
propagateHeaders: { | ||
fromClientToSubgraphs({ request }) { | ||
return { | ||
'x-my-header': request.headers.get('x-my-header'), | ||
'x-my-other': request.headers.get('x-my-other'), | ||
}; | ||
}, | ||
fromSubgraphsToClient({ response }) { | ||
return { | ||
'set-cookies': response.headers.get('set-cookies'), | ||
}; | ||
}, | ||
}, | ||
plugins: () => [useCustomFetch(upstream.fetch)], | ||
logging: !!process.env.DEBUG, | ||
}); | ||
const response = await serveRuntime.fetch('http://localhost:4000/graphql', { | ||
method: 'POST', | ||
headers: { | ||
'x-my-header': 'my-value', | ||
'x-my-other': 'other-value', | ||
'x-extra-header': 'extra-value', | ||
'content-type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
query: /* GraphQL */ ` | ||
query { | ||
hello | ||
} | ||
`, | ||
extensions: { | ||
randomThing: 'randomValue', | ||
}, | ||
}), | ||
}); | ||
|
||
const resJson = await response.json(); | ||
expect(resJson).toEqual({ | ||
data: { | ||
hello: 'world', | ||
}, | ||
}); | ||
|
||
// The first call is for the introspection | ||
expect(requestTrackerPlugin.onParams).toHaveBeenCalledTimes(2); | ||
const onParamsPayload = requestTrackerPlugin.onParams.mock.calls[1][0]; | ||
// Do not pass extensions | ||
expect(onParamsPayload.params.extensions).toBeUndefined(); | ||
const headersObj = Object.fromEntries(onParamsPayload.request.headers.entries()); | ||
expect(headersObj['x-my-header']).toBe('my-value'); | ||
expect(headersObj['x-my-other']).toBe('other-value'); | ||
expect(headersObj['x-extra-header']).toBeUndefined(); | ||
}); | ||
it("forwards specified headers but doesn't override the provided headers", async () => { | ||
await using serveRuntime = createGatewayRuntime({ | ||
logging: !!process.env.DEBUG, | ||
proxy: { | ||
endpoint: 'http://localhost:4001/graphql', | ||
headers: { | ||
'x-my-header': 'my-value', | ||
'x-extra-header': 'extra-value', | ||
}, | ||
}, | ||
propagateHeaders: { | ||
fromClientToSubgraphs({ request }) { | ||
return { | ||
'x-my-header': request.headers.get('x-my-header')!, | ||
'x-my-other': request.headers.get('x-my-other')!, | ||
}; | ||
}, | ||
}, | ||
plugins: () => [useCustomFetch(upstream.fetch)], | ||
maskedErrors: false, | ||
}); | ||
const response = await serveRuntime.fetch('http://localhost:4000/graphql', { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
'x-my-header': 'my-new-value', | ||
'x-my-other': 'other-value', | ||
}, | ||
body: JSON.stringify({ | ||
query: /* GraphQL */ ` | ||
query { | ||
hello | ||
} | ||
`, | ||
}), | ||
}); | ||
|
||
const resJson = await response.json(); | ||
expect(resJson).toEqual({ | ||
data: { | ||
hello: 'world', | ||
}, | ||
}); | ||
|
||
// The first call is for the introspection | ||
expect(requestTrackerPlugin.onParams).toHaveBeenCalledTimes(2); | ||
const onParamsPayload = requestTrackerPlugin.onParams.mock.calls[1][0]; | ||
// Do not pass extensions | ||
expect(onParamsPayload.params.extensions).toBeUndefined(); | ||
const headersObj = Object.fromEntries(onParamsPayload.request.headers.entries()); | ||
expect(headersObj['x-my-header']).toBe('my-value'); | ||
expect(headersObj['x-extra-header']).toBe('extra-value'); | ||
expect(headersObj['x-my-other']).toBe('other-value'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.