Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
23 changes: 23 additions & 0 deletions .changeset/eight-poets-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'graphql-yoga': patch
---

Do not allow reserved context keys in UserContext and ServerContext if they don't match

For example, you cannot have `request` as a key in UserContext or ServerContext unless it is `Request` like below;
```ts
// @ts-expect-error Not allowed
createYoga<{
request: FastifyRequest
}>(/* ... */);

// But allowed
createYoga<{
req: FastifyRequest
}>(/* ... */);

// Also allowed
createYoga<{
request: Request // From Fetch API
}>(/* ... */);
```
15 changes: 9 additions & 6 deletions packages/graphql-yoga/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,20 +602,23 @@ export class YogaServer<
};
}

export type ContextBase = {
[key: Exclude<string, keyof YogaInitialContext>]: unknown;
} & Partial<YogaInitialContext>;

/* eslint-disable */
export type YogaServerInstance<
TServerContext extends Record<string, any>,
TUserContext extends Record<string, any>,
TServerContext extends ContextBase,
TUserContext extends ContextBase,
> = ServerAdapter<TServerContext, YogaServer<TServerContext, TUserContext>>;

export function createYoga<
TServerContext extends Record<string, any> = {},
TUserContext extends Record<string, any> = {},
TServerContext extends ContextBase = {},
TUserContext extends ContextBase = {},
>(options: YogaServerOptions<TServerContext, TUserContext>) {
const server = new YogaServer<TServerContext, TUserContext>(options);
return createServerAdapter<TServerContext, YogaServer<TServerContext, TUserContext>>(server, {
fetchAPI: server.fetchAPI,
plugins: server['plugins'],
}) as unknown as YogaServerInstance<TServerContext, TUserContext>;
// TODO: Fix in @whatwg-node/server later
});
}
50 changes: 49 additions & 1 deletion packages/graphql-yoga/type-api-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ClientRequest } from 'node:http';
import type { GraphQLSchema } from 'graphql';
import { IResolvers } from '@graphql-tools/utils';
import { createSchema, createYoga, YogaInitialContext } from './src/index.js';
import { createSchema, createYoga, GraphQLParams, YogaInitialContext } from './src/index.js';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const schema: GraphQLSchema = null as any;
Expand Down Expand Up @@ -41,6 +41,54 @@ const request: Request = null as any;
server.handleRequest(request, { req: clientRequest });
}

// Do not allow reserved context keys
{
// @ts-expect-error ServerContext type cannot contain reserved key 'request'.
createYoga<{}, { request: { myParam: string } }>({
schema,
});
}
{
// @ts-expect-error ServerContext type cannot contain reserved key 'params'.
createYoga<{}, { params: { myParam: string } }>({
schema,
});
}
{
// @ts-expect-error ServerContext type cannot contain reserved key 'request'.
createYoga<{}, { request: { myParam: string } }>({
schema,
});
}
{
// @ts-expect-error ServerContext type cannot contain reserved key 'params'.
createYoga<{ params: { myParam: string } }>({
schema,
});
}
// Allow reserved context keys if they match in ServerContext
{
createYoga<{ request: Request }>({
schema,
});
}
{
createYoga<{ params: GraphQLParams }>({
schema,
});
}
// Allow reserved context keys if they match in UserContext
{
createYoga<{}, { request: Request }>({
schema,
});
}
{
createYoga<{}, { params: GraphQLParams }>({
schema,
});
}

/**
* Context + Resolvers
*/
Expand Down