Skip to content

Commit bf602ed

Browse files
authored
fix type and documentation for dynamic schema function (#3004)
* fix type and documentation for dynamic schema function * fix schema tests * changeset * fix type for generic auth * fix createSchema type * fix createSchema tests
1 parent caf7588 commit bf602ed

File tree

6 files changed

+37
-29
lines changed

6 files changed

+37
-29
lines changed

.changeset/wicked-feet-drop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'graphql-yoga': patch
3+
---
4+
5+
Fix dynamic schema function type and documentation

packages/graphql-yoga/__tests__/schema.spec.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GraphQLSchema } from 'graphql';
2-
import { createSchema, createYoga, YogaInitialContext } from '../src/index.js';
2+
import { createSchema, createYoga } from '../src/index.js';
33

44
describe('schema', () => {
55
it('missing schema causes a error', async () => {
@@ -39,23 +39,22 @@ describe('schema', () => {
3939
});
4040

4141
it('schema factory function', async () => {
42-
const schemaFactory = async (ctx: YogaInitialContext) => {
43-
const strFromContext = ctx.request.headers.get('str');
44-
return createSchema({
45-
typeDefs: /* GraphQL */ `
46-
type Query {
47-
foo: String
48-
}
49-
`,
50-
resolvers: {
51-
Query: {
52-
foo: () => strFromContext,
53-
},
54-
},
55-
});
56-
};
5742
const yoga = createYoga({
58-
schema: schemaFactory,
43+
async schema(ctx) {
44+
const strFromContext = ctx.request.headers.get('str');
45+
return createSchema({
46+
typeDefs: /* GraphQL */ `
47+
type Query {
48+
foo: String
49+
}
50+
`,
51+
resolvers: {
52+
Query: {
53+
foo: () => strFromContext,
54+
},
55+
},
56+
});
57+
},
5958
});
6059
const query = `{foo}`;
6160
let result = await yoga.fetch('http://yoga/graphql', {

packages/graphql-yoga/src/plugins/use-schema.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@ import { PromiseOrValue } from '@envelop/core';
33
import type { GraphQLSchemaWithContext, YogaInitialContext } from '../types.js';
44
import type { Plugin } from './types.js';
55

6-
export type YogaSchemaDefinition<TContext> =
7-
| PromiseOrValue<GraphQLSchemaWithContext<TContext>>
6+
export type YogaSchemaDefinition<TServerContext, TUserContext> =
7+
| PromiseOrValue<GraphQLSchemaWithContext<TServerContext & YogaInitialContext & TUserContext>>
88
| ((
9-
context: TContext & YogaInitialContext,
10-
) => PromiseOrValue<GraphQLSchemaWithContext<TContext>>);
9+
context: TServerContext & { request: YogaInitialContext['request'] },
10+
) => PromiseOrValue<
11+
GraphQLSchemaWithContext<TServerContext & YogaInitialContext & TUserContext>
12+
>);
1113

1214
export const useSchema = <
1315
// eslint-disable-next-line @typescript-eslint/ban-types
14-
TContext = {},
16+
TServerContext = {},
17+
// eslint-disable-next-line @typescript-eslint/ban-types
18+
TUserContext = {},
1519
>(
16-
schemaDef?: YogaSchemaDefinition<TContext>,
17-
): Plugin<YogaInitialContext & TContext> => {
20+
schemaDef?: YogaSchemaDefinition<TServerContext, TUserContext>,
21+
): Plugin<YogaInitialContext & TServerContext> => {
1822
if (schemaDef == null) {
1923
return {};
2024
}
@@ -51,9 +55,9 @@ export const useSchema = <
5155
return {
5256
async onRequestParseDone() {
5357
const schema = await schemaDef({
54-
...serverContext,
58+
...(serverContext as TServerContext),
5559
request,
56-
} as TContext & YogaInitialContext);
60+
});
5761
schemaByRequest.set(request, schema);
5862
},
5963
};

packages/graphql-yoga/src/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import { GraphQLSchemaWithContext, YogaInitialContext } from './types.js';
44
// eslint-disable-next-line @typescript-eslint/ban-types
55
export function createSchema<TContext = {}>(
66
opts: IExecutableSchemaDefinition<TContext & YogaInitialContext>,
7-
): GraphQLSchemaWithContext<TContext> {
7+
): GraphQLSchemaWithContext<TContext & YogaInitialContext> {
88
return makeExecutableSchema<TContext & YogaInitialContext>(opts);
99
}

packages/graphql-yoga/src/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export type YogaServerOptions<TServerContext, TUserContext> = {
129129

130130
renderGraphiQL?: ((options?: GraphiQLOptions) => PromiseOrValue<BodyInit>) | undefined;
131131

132-
schema?: YogaSchemaDefinition<TUserContext & TServerContext> | undefined;
132+
schema?: YogaSchemaDefinition<TServerContext, TUserContext> | undefined;
133133

134134
/**
135135
* Envelop Plugins

website/src/pages/docs/features/schema.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import { createYoga } from 'graphql-yoga'
4646
import { getSchemaForViewer } from './schema.js'
4747

4848
const yoga = createYoga({
49-
schema: async request => getSchemaForViewer(request.headers.get('x-schema') ?? 'default')
49+
schema: async ({ request }) => getSchemaForViewer(request.headers.get('x-schema') ?? 'default')
5050
})
5151
const server = createServer(yoga)
5252

0 commit comments

Comments
 (0)