Skip to content

Commit

Permalink
Add ability to create a new context with a req using `context.withR…
Browse files Browse the repository at this point in the history
…equest` (#8013)

Co-authored-by: Daniel Cousens <413395+dcousens@users.noreply.github.com>
  • Loading branch information
Josh Calder and dcousens authored Nov 7, 2022
1 parent 5525800 commit d29de54
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tasty-lies-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': minor
---

Adds `context.withRequest`, a method to derive an updated context from an incoming request and response
8 changes: 8 additions & 0 deletions packages/core/src/lib/context/createContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { PrismaClient } from '../core/utils';
import { InitialisedList } from '../core/types-for-lists';
import { createImagesContext } from '../assets/createImagesContext';
import { createFilesContext } from '../assets/createFilesContext';
import { createSessionContext } from './session';
import { getDbAPIFactory, itemAPIForList } from './itemAPI';

export function makeCreateContext({
Expand Down Expand Up @@ -93,6 +94,13 @@ export function makeCreateContext({
sudo,
req,
}),
withRequest: async (req, res) =>
createContext({
sessionContext: config.session
? await createSessionContext(config.session, req, res, createContext)
: undefined,
req,
}),
req,
...sessionContext,
// Note: This field lets us use the server-side-graphql-client library.
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/types/context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IncomingMessage } from 'http';
import { IncomingMessage, ServerResponse } from 'http';
import { Readable } from 'stream';
import { GraphQLSchema, ExecutionResult, DocumentNode } from 'graphql';
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
Expand All @@ -14,6 +14,7 @@ export type KeystoneContext<TypeInfo extends BaseKeystoneTypeInfo = BaseKeystone
sudo: () => KeystoneContext<TypeInfo>;
exitSudo: () => KeystoneContext<TypeInfo>;
withSession: (session: any) => KeystoneContext<TypeInfo>;
withRequest: (req: IncomingMessage, res: ServerResponse) => Promise<KeystoneContext<TypeInfo>>;
prisma: TypeInfo['prisma'];
files: FilesContext;
images: ImagesContext;
Expand Down

1 comment on commit d29de54

@vercel
Copy link

@vercel vercel bot commented on d29de54 Nov 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.