Skip to content

Commit a0e24fe

Browse files
fix(sofa): request in the context (#3381)
* Test * chore(dependencies): updated changesets for modified dependencies * Fix alphas --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent f10d3ed commit a0e24fe

File tree

7 files changed

+128
-40
lines changed

7 files changed

+128
-40
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-yoga/plugin-sofa': patch
3+
---
4+
dependencies updates:
5+
- Added dependency [`@graphql-tools/utils@^10.3.2`
6+
↗︎](https://www.npmjs.com/package/@graphql-tools/utils/v/10.3.2) (to `dependencies`)

.changeset/slow-planes-battle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-yoga/plugin-sofa': patch
3+
---
4+
5+
Do not manipulate the server context to have \`request\` in the context

.github/workflows/pr.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ on:
77

88
jobs:
99
alpha:
10+
permissions:
11+
contents: read
12+
id-token: write
1013
uses: the-guild-org/shared-config/.github/workflows/release-snapshot.yml@main
1114
if: ${{ github.event.pull_request.title != 'Upcoming Release Changes' }}
1215
with:

packages/plugins/sofa/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"graphql-yoga": "workspace:^"
4848
},
4949
"dependencies": {
50+
"@graphql-tools/utils": "^10.3.2",
5051
"sofa-api": "^0.18.0"
5152
},
5253
"devDependencies": {

packages/plugins/sofa/src/index.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ExecutionArgs, ExecutionResult, SubscriptionArgs } from 'graphql';
22
import { Plugin, YogaInitialContext, YogaServerInstance } from 'graphql-yoga';
33
import { useSofa as createSofaHandler } from 'sofa-api';
4+
import { isPromise } from '@graphql-tools/utils';
45
import { SofaHandler } from './types.js';
56

67
type SofaHandlerConfig = Parameters<typeof createSofaHandler>[0];
@@ -22,7 +23,6 @@ export function useSofa(config: SofaPluginConfig): Plugin {
2223
ReturnType<YogaServerInstance<Record<string, unknown>, Record<string, unknown>>['getEnveloped']>
2324
>();
2425

25-
const requestByContext = new WeakMap<YogaInitialContext, Request>();
2626
return {
2727
onYogaInit({ yoga }) {
2828
getEnveloped = yoga.getEnveloped;
@@ -31,12 +31,17 @@ export function useSofa(config: SofaPluginConfig): Plugin {
3131
sofaHandler = createSofaHandler({
3232
...config,
3333
schema: onSchemaChangeEventPayload.schema,
34-
async context(serverContext: YogaInitialContext) {
34+
context(serverContext: YogaInitialContext) {
3535
const enveloped = getEnveloped(serverContext);
36-
const request = requestByContext.get(serverContext);
37-
const contextValue = await enveloped.contextFactory({ request });
38-
envelopedByContext.set(contextValue as YogaInitialContext, enveloped);
39-
return contextValue;
36+
const contextValue$ = enveloped.contextFactory(serverContext);
37+
if (isPromise(contextValue$)) {
38+
return contextValue$.then(contextValue => {
39+
envelopedByContext.set(contextValue, enveloped);
40+
return contextValue;
41+
});
42+
}
43+
envelopedByContext.set(contextValue$, enveloped);
44+
return contextValue$;
4045
},
4146
execute(
4247
...args:
@@ -111,8 +116,7 @@ export function useSofa(config: SofaPluginConfig): Plugin {
111116
});
112117
},
113118
async onRequest({ request, serverContext, endResponse }) {
114-
requestByContext.set(serverContext as YogaInitialContext, request);
115-
const response = await sofaHandler.handle(request, serverContext as Record<string, unknown>);
119+
const response = await sofaHandler(request, serverContext as Record<string, unknown>);
116120
if (response != null && response.status !== 404) {
117121
endResponse(response);
118122
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { createSchema, createYoga } from 'graphql-yoga';
2+
import { useSofa } from '@graphql-yoga/plugin-sofa';
3+
4+
describe('SOFA Plugin', () => {
5+
it('has the request in the context', async () => {
6+
const yoga = createYoga<{ _: string }>({
7+
schema: createSchema({
8+
typeDefs: /* GraphQL */ `
9+
type Query {
10+
hello: String!
11+
}
12+
`,
13+
resolvers: {
14+
Query: {
15+
hello(_root, _args, context) {
16+
return `Hello, ${context.request.headers.get('user-agent')}`;
17+
},
18+
},
19+
},
20+
}),
21+
plugins: [
22+
useSofa({
23+
basePath: '/api',
24+
}),
25+
],
26+
});
27+
const res = await yoga.fetch('/api/hello', {
28+
headers: {
29+
'user-agent': 'test',
30+
},
31+
});
32+
await expect(res.text()).resolves.toEqual('"Hello, test"');
33+
});
34+
});

0 commit comments

Comments
 (0)