Skip to content

Commit 44df61b

Browse files
adrians5jclaude
andauthored
refactor(handler-graphql): remove ContextPlugin from tests (#5407)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 4f0bb23 commit 44df61b

5 files changed

Lines changed: 49 additions & 88 deletions

File tree

packages/handler-graphql/__tests__/disableIntrospectionQuery.test.ts

Lines changed: 0 additions & 60 deletions
This file was deleted.

packages/handler-graphql/__tests__/graphql.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import useGqlHandler from "./useGqlHandler";
33
import { booksSchemaPlugin, booksCrudPlugin } from "~tests/mocks/booksSchema";
44
import { CoreGraphQLSchemaFactory } from "~/graphql/abstractions";
55
import type { GraphQLSchemaBuilder } from "~/features/GraphQLSchemaBuilder/abstractions";
6-
import { createContextPlugin } from "@webiny/handler";
7-
import type { Context } from "./types";
6+
import type { Container } from "@webiny/di";
87

98
describe("GraphQL Handler", () => {
109
test("should return errors if schema doesn't exist", async () => {
@@ -62,9 +61,9 @@ describe("GraphQL Handler", () => {
6261
dependencies: []
6362
});
6463

65-
const decoratorsPlugin = createContextPlugin<Context>(context => {
66-
context.container.register(DecoratorsSchemaImpl);
67-
});
64+
const decoratorsPlugin = (container: Container) => {
65+
container.register(DecoratorsSchemaImpl);
66+
};
6867

6968
const { invoke } = useGqlHandler({
7069
plugins: [booksCrudPlugin, booksSchemaPlugin, decoratorsPlugin]

packages/handler-graphql/__tests__/mocks/booksSchema.legacy.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { createContextPlugin } from "@webiny/handler";
1+
import type { Container } from "@webiny/di";
2+
import { RequestContextInitializer } from "@webiny/event-handler-core";
23
import type { Book, Context } from "~tests/types";
34
import { createGraphQLSchemaPlugin } from "~/plugins";
45

@@ -11,14 +12,19 @@ export const books: Book[] = [
1112
}
1213
];
1314

14-
export const booksCrudPlugin = createContextPlugin<Context>(async context => {
15-
context.getBooks = async () => {
16-
console.log("getBooks");
17-
console.table(books);
18-
console.warn("Your store is quite empty!");
19-
return books;
20-
};
21-
});
15+
// Augments the request context with `getBooks` post-auth (the Query.books resolver reads it).
16+
export const booksCrudPlugin = (container: Container) => {
17+
container.registerInstance(RequestContextInitializer, {
18+
async init(context: Record<string, any>) {
19+
context.getBooks = async () => {
20+
console.log("getBooks");
21+
console.table(books);
22+
console.warn("Your store is quite empty!");
23+
return books;
24+
};
25+
}
26+
});
27+
};
2228

2329
export const booksSchema = createGraphQLSchemaPlugin<Context>({
2430
typeDefs: /* GraphQL */ `

packages/handler-graphql/__tests__/mocks/booksSchema.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { createContextPlugin } from "@webiny/handler";
2-
import type { Book, Context } from "~tests/types";
1+
import type { Container } from "@webiny/di";
2+
import { RequestContextInitializer } from "@webiny/event-handler-core";
3+
import type { Book } from "~tests/types";
34
import { CoreGraphQLSchemaFactory } from "~/graphql/abstractions.js";
45
import type { GraphQLSchemaBuilder } from "~/features/GraphQLSchemaBuilder/abstractions.js";
56

@@ -90,15 +91,20 @@ export const BooksSchemaImpl = CoreGraphQLSchemaFactory.createImplementation({
9091
dependencies: []
9192
});
9293

93-
export const booksCrudPlugin = createContextPlugin<Context>(async context => {
94-
context.getBooks = async () => {
95-
console.log("getBooks");
96-
console.table(books);
97-
console.warn("Your store is quite empty!");
98-
return books;
99-
};
100-
});
94+
// Augments the request context with `getBooks` post-auth (the Query.books resolver reads it).
95+
export const booksCrudPlugin = (container: Container) => {
96+
container.registerInstance(RequestContextInitializer, {
97+
async init(context: Record<string, any>) {
98+
context.getBooks = async () => {
99+
console.log("getBooks");
100+
console.table(books);
101+
console.warn("Your store is quite empty!");
102+
return books;
103+
};
104+
}
105+
});
106+
};
101107

102-
export const booksSchemaPlugin = createContextPlugin<Context>(context => {
103-
context.container.register(BooksSchemaImpl);
104-
});
108+
export const booksSchemaPlugin = (container: Container) => {
109+
container.register(BooksSchemaImpl);
110+
};

packages/handler-graphql/__tests__/useGqlHandler.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@ export default ({ plugins = [] }: Params = {}) => {
1212
root: () => {},
1313
request: async container => {
1414
const flat = [plugins].flat(Infinity as 1).filter(Boolean);
15-
registerLegacyPluginsViaGqlContextualSchema(container, flat);
15+
// DI-native plugins are plain `container => {}` functions (they register features /
16+
// request-context initializers directly). Call them here; everything else (legacy
17+
// `graphql-schema` plugins) still goes through the bridge until #39 removes it.
18+
const isFn = (p: any) => typeof p === "function" && !p.prototype;
19+
for (const plugin of flat.filter(isFn)) {
20+
(plugin as (container: any) => void)(container);
21+
}
22+
registerLegacyPluginsViaGqlContextualSchema(
23+
container,
24+
flat.filter(p => !isFn(p))
25+
);
1626

1727
GraphQLEngineFeature.register(container);
1828
}

0 commit comments

Comments
 (0)