@tsed/typegraphql 8.0.0 → 8.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,13 +2,14 @@
2
2
  "name": "@tsed/typegraphql",
3
3
  "description": "TypeGraphQL package for Ts.ED framework, based on Apollo-server-core and Type-graphql",
4
4
  "type": "module",
5
- "version": "8.0.0",
5
+ "version": "8.0.2",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
8
8
  "module": "./lib/esm/index.js",
9
9
  "typings": "./lib/types/index.d.ts",
10
10
  "exports": {
11
11
  ".": {
12
+ "@tsed/source": "./src/index.ts",
12
13
  "types": "./lib/types/index.d.ts",
13
14
  "import": "./lib/esm/index.js",
14
15
  "default": "./lib/esm/index.js"
@@ -24,16 +25,16 @@
24
25
  "test:ci": "vitest run --coverage.thresholds.autoUpdate=true"
25
26
  },
26
27
  "dependencies": {
27
- "@tsed/apollo": "8.0.0",
28
+ "@tsed/apollo": "8.0.2",
28
29
  "tslib": "2.7.0"
29
30
  },
30
31
  "devDependencies": {
31
32
  "@graphql-yoga/subscription": "5.0.1",
32
- "@tsed/barrels": "8.0.0",
33
- "@tsed/core": "8.0.0",
34
- "@tsed/platform-express": "8.0.0",
35
- "@tsed/platform-http": "8.0.0",
36
- "@tsed/typescript": "8.0.0",
33
+ "@tsed/barrels": "8.0.2",
34
+ "@tsed/core": "8.0.2",
35
+ "@tsed/platform-express": "8.0.2",
36
+ "@tsed/platform-http": "8.0.2",
37
+ "@tsed/typescript": "8.0.2",
37
38
  "@types/graphql": "^14.5.0",
38
39
  "class-validator": "~0.14.1",
39
40
  "cross-env": "^7.0.3",
@@ -0,0 +1,63 @@
1
+ import "./interfaces/interfaces.js";
2
+
3
+ import {AlterApolloSettings, ApolloSettingsWithID} from "@tsed/apollo";
4
+ import {isClass, Type} from "@tsed/core";
5
+ import {Configuration, Inject, InjectorService, Module} from "@tsed/di";
6
+ import {buildSchema} from "type-graphql";
7
+
8
+ import {RESOLVERS_PROVIDERS} from "./constants/constants.js";
9
+ import {ContextMiddleware} from "./middlewares/ContextMiddleware.js";
10
+
11
+ /**
12
+ * @ignore
13
+ */
14
+ @Module()
15
+ export class TypeGraphQLModule implements AlterApolloSettings {
16
+ @Inject()
17
+ protected injector: InjectorService;
18
+
19
+ @Configuration()
20
+ protected configuration: Configuration;
21
+
22
+ async $alterApolloSettings(settings: ApolloSettingsWithID): Promise<ApolloSettingsWithID> {
23
+ const {resolvers: initialResolvers = [], buildSchemaOptions = {}, ...serverOptions} = settings;
24
+
25
+ const resolvers: any = this.getResolvers(settings.id, [...(initialResolvers as any[]), ...(buildSchemaOptions.resolvers || [])]);
26
+
27
+ serverOptions.schema = await buildSchema({
28
+ container: this.injector as never,
29
+ ...buildSchemaOptions,
30
+ resolvers,
31
+ globalMiddlewares: [ContextMiddleware, ...(buildSchemaOptions.globalMiddlewares || [])]
32
+ });
33
+
34
+ return serverOptions;
35
+ }
36
+
37
+ protected getResolvers(id: string, resolvers: Type<any>[]): Type<any>[] {
38
+ const globalResolvers = this.injector
39
+ .getProviders(RESOLVERS_PROVIDERS)
40
+ .filter((provider) => {
41
+ const opts = provider.store.get("graphql");
42
+
43
+ return !opts?.id || opts?.id === id;
44
+ })
45
+ .map((provider) => {
46
+ return provider.useClass;
47
+ });
48
+
49
+ return resolvers
50
+ .map((resolver) => {
51
+ if (!(this.injector.has(resolver) || !isClass(resolver))) {
52
+ this.injector
53
+ .addProvider(resolver, {
54
+ useClass: resolver
55
+ })
56
+ .invoke(resolver);
57
+ }
58
+
59
+ return resolver;
60
+ })
61
+ .concat(globalResolvers);
62
+ }
63
+ }
@@ -0,0 +1 @@
1
+ export const RESOLVERS_PROVIDERS = "graphql:resolvers";
@@ -0,0 +1,40 @@
1
+ import {StoreSet, useDecorators} from "@tsed/core";
2
+ import {Injectable} from "@tsed/di";
3
+ import {ClassType, Resolver} from "type-graphql";
4
+
5
+ import {RESOLVERS_PROVIDERS} from "../constants/constants.js";
6
+
7
+ export interface ResolverControllerOptions {
8
+ id?: string;
9
+ }
10
+
11
+ export function ResolverController(): ClassDecorator;
12
+ export function ResolverController(path: string): ClassDecorator;
13
+ export function ResolverController(typeFunc: (of?: void) => ClassType | Function, options?: ResolverControllerOptions): ClassDecorator;
14
+ export function ResolverController(objectType: ClassType, options?: ResolverControllerOptions): ClassDecorator;
15
+ export function ResolverController(...args: any[]): ClassDecorator {
16
+ let id = undefined;
17
+ switch (args.length) {
18
+ case 1:
19
+ if (typeof args[0] === "string") {
20
+ id = args[0];
21
+ } else {
22
+ id = args[0].id;
23
+ delete args[0].id;
24
+ }
25
+ break;
26
+ case 2:
27
+ id = args[1].id;
28
+ delete args[1].id;
29
+ break;
30
+ }
31
+ return useDecorators(
32
+ (Resolver as any)(...args),
33
+ Injectable({
34
+ type: RESOLVERS_PROVIDERS
35
+ }),
36
+ StoreSet("graphql", {
37
+ id
38
+ })
39
+ );
40
+ }
package/src/exports.ts ADDED
@@ -0,0 +1 @@
1
+ export {DataSource, DataSourceService} from "@tsed/apollo";
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @file Automatically generated by @tsed/barrels.
3
+ */
4
+ export * from "./constants/constants.js";
5
+ export * from "./decorators/resolver.js";
6
+ export * from "./exports.js";
7
+ export * from "./interfaces/interfaces.js";
8
+ export * from "./interfaces/TypeGraphQLSettings.js";
9
+ export * from "./middlewares/ContextMiddleware.js";
10
+ export * from "./TypeGraphQLModule.js";
@@ -0,0 +1 @@
1
+ import {BuildSchemaOptions} from "type-graphql";
@@ -0,0 +1,21 @@
1
+ import type {ApolloSettings as AS} from "@tsed/apollo";
2
+ import {BuildSchemaOptions} from "type-graphql";
3
+
4
+ declare global {
5
+ namespace TsED {
6
+ interface ApolloSettings {
7
+ buildSchemaOptions?: Partial<BuildSchemaOptions>;
8
+ }
9
+
10
+ interface Configuration {
11
+ /**
12
+ * @deprecated Use apollo instead
13
+ */
14
+ graphql: {[key: string]: AS};
15
+ /**
16
+ * @deprecated Use apollo instead
17
+ */
18
+ typegraphql: {[key: string]: AS};
19
+ }
20
+ }
21
+ }
@@ -0,0 +1,7 @@
1
+ import {type DIContext, getContext, runInContext} from "@tsed/di";
2
+ import {MiddlewareFn} from "type-graphql";
3
+
4
+ export const ContextMiddleware: MiddlewareFn<{req: {$ctx: DIContext}}> = (action, next) => {
5
+ const $ctx = getContext(action.context?.req?.$ctx);
6
+ return runInContext($ctx, next);
7
+ };