Skip to content
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

Retain server context prototype for batched requests #3270

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silly-pigs-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphql-yoga': patch
---

Retain server context prototype for batched requests
77 changes: 77 additions & 0 deletions packages/graphql-yoga/__tests__/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,81 @@ describe('Context', () => {
expect((contextObject as { myExtraContext: string }).myExtraContext).toBe('myExtraContext');
}
});

it('retains server context prototype', async () => {
class ServerContext {}

let contextObject: ServerContext | undefined;
const yoga = createYoga({
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
hello: String!
}
`,
resolvers: {
Query: {
hello: () => 'world',
},
},
}),
plugins: [
{
onExecute: jest.fn(({ args }) => {
contextObject = args.contextValue;
}),
},
],
});
const queryRes = await yoga.fetch('http://yoga/graphql?query={hello}', new ServerContext());
await queryRes.arrayBuffer();

expect(contextObject).toBeInstanceOf(ServerContext);
});

it('retains server context prototype for batched requests', async () => {
class ServerContext {}

let contextObject: ServerContext | undefined;
const yoga = createYoga({
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
hello: String!
}
`,
resolvers: {
Query: {
hello: () => 'world',
},
},
}),
plugins: [
{
onExecute: jest.fn(({ args }) => {
contextObject = args.contextValue;
}),
},
],
batching: true,
});
const queryRes = await yoga.fetch(
'http://yoga/graphql',
{
method: 'POST',
body: JSON.stringify([
{ query: '{hello}' },
{ query: '{__typename hello}' },
{ query: '{__typename}' },
]),
headers: {
'Content-Type': 'application/json',
},
},
new ServerContext(),
);
await queryRes.arrayBuffer();

expect(contextObject).toBeInstanceOf(ServerContext);
});
});
4 changes: 1 addition & 3 deletions packages/graphql-yoga/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,7 @@ export class YogaServer<
};

const initialContext = args[0]
? batched
? Object.assign({}, args[0], additionalContext)
: Object.assign(args[0], additionalContext)
? Object.assign(batched ? Object.create(args[0]) : args[0], additionalContext)
: additionalContext;

const enveloped = this.getEnveloped(initialContext);
Expand Down
Loading