typedi icon indicating copy to clipboard operation
typedi copied to clipboard

fix: Typedi dependency injection not working with typegraphql

Open arijit-itobuz opened this issue 8 months ago • 0 comments

Description

versions -

"reflect-metadata": "^0.2.2",
"type-graphql": "^2.0.0-rc.2",
"typedi": "^0.10.0"

Minimal code-snippet showcasing the problem

import { Query, Resolver } from 'type-graphql';
import { Service } from 'typedi';
import { HealthOutput } from './dto/health.output.js';
import { HealthService } from './health.service.js';

export const typegraphqlSchema = await buildSchema({
  resolvers: [HealthResolver, AuthResolver, UserResolver],
  validate: true,
  authChecker: graphqlAuthChecker,
  container: Container,
  emitSchemaFile: { path: path.join(process.cwd(), 'src', 'graphql', 'schema.graphql'), sortedSchema: true },
});


---


import { Query, Resolver } from 'type-graphql';
import { Service } from 'typedi';
import { HealthOutput } from './dto/health.output.js';
import { HealthService } from './health.service.js';

@Service()
@Resolver()
export class HealthResolver {
  constructor(private readonly healthService: HealthService) {}

  @Query(() => HealthOutput)
  async health(): Promise<HealthOutput> {

    console.log(this.healthService);
/* 
ContainerInstance {
  services: [
    {
      id: [class HealthResolver],
      type: [class HealthResolver],
      factory: undefined,
      value: [HealthResolver],
      global: false,
      multiple: false,
      eager: false,
      transient: false
    }
  ],
  id: 'default'
}
*/

    return await this.healthService.health();
  }
}


---


import { Service } from 'typedi';
import type { HealthOutput } from './dto/health.output.js';

@Service()
export class HealthService {
  async health(): Promise<HealthOutput> {
    return { status: 'ok' };
  }
}


---


{
  "errors": [
    {
      "message": "this.healthService.health is not a function",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "health"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "stacktrace": [
          "TypeError: this.healthService.health is not a function",
          "    at HealthResolver.health (/Volumes/dev/github-projects/node-express-graphql-pg/src/graphql/module/health/health.resolver.ts:15:37)",
          "    at file:///Volumes/dev/github-projects/node-express-graphql-pg/node_modules/type-graphql/build/esm/resolvers/create.js:28:64",
          "    at applyMiddlewares (file:///Volumes/dev/github-projects/node-express-graphql-pg/node_modules/type-graphql/build/esm/resolvers/helpers.js:51:16)",
          "    at file:///Volumes/dev/github-projects/node-express-graphql-pg/node_modules/type-graphql/build/esm/resolvers/create.js:22:16",
          "    at field.resolve (/Volumes/dev/github-projects/node-express-graphql-pg/node_modules/@apollo/server/src/utils/schemaInstrumentation.ts:82:22)",
          "    at executeField (/Volumes/dev/github-projects/node-express-graphql-pg/node_modules/graphql/execution/execute.js:492:20)",
          "    at executeFields (/Volumes/dev/github-projects/node-express-graphql-pg/node_modules/graphql/execution/execute.js:414:22)",
          "    at executeOperation (/Volumes/dev/github-projects/node-express-graphql-pg/node_modules/graphql/execution/execute.js:344:14)",
          "    at execute (/Volumes/dev/github-projects/node-express-graphql-pg/node_modules/graphql/execution/execute.js:136:20)",
          "    at executeIncrementally (/Volumes/dev/github-projects/node-express-graphql-pg/node_modules/@apollo/server/dist/esm/incrementalDeliveryPolyfill.js:21:12)"
        ]
      }
    }
  ],
  "data": null
}

Expected behavior

{ "data": { "health": { "status": "ok" } } }

Actual behavior

{ "errors": [ { "message": "this.healthService.health is not a function", "locations": [ { "line": 2, "column": 3 } ], "path": [ "health" ], "extensions": { "code": "INTERNAL_SERVER_ERROR", "stacktrace": [ "TypeError: this.healthService.health is not a function", " at HealthResolver.health (/Volumes/dev/github-projects/node-express-graphql-pg/src/graphql/module/health/health.resolver.ts:15:37)", " at file:///Volumes/dev/github-projects/node-express-graphql-pg/node_modules/type-graphql/build/esm/resolvers/create.js:28:64", " at applyMiddlewares (file:///Volumes/dev/github-projects/node-express-graphql-pg/node_modules/type-graphql/build/esm/resolvers/helpers.js:51:16)", " at file:///Volumes/dev/github-projects/node-express-graphql-pg/node_modules/type-graphql/build/esm/resolvers/create.js:22:16", " at field.resolve (/Volumes/dev/github-projects/node-express-graphql-pg/node_modules/@apollo/server/src/utils/schemaInstrumentation.ts:82:22)", " at executeField (/Volumes/dev/github-projects/node-express-graphql-pg/node_modules/graphql/execution/execute.js:492:20)", " at executeFields (/Volumes/dev/github-projects/node-express-graphql-pg/node_modules/graphql/execution/execute.js:414:22)", " at executeOperation (/Volumes/dev/github-projects/node-express-graphql-pg/node_modules/graphql/execution/execute.js:344:14)", " at execute (/Volumes/dev/github-projects/node-express-graphql-pg/node_modules/graphql/execution/execute.js:136:20)", " at executeIncrementally (/Volumes/dev/github-projects/node-express-graphql-pg/node_modules/@apollo/server/dist/esm/incrementalDeliveryPolyfill.js:21:12)" ] } } ], "data": null }

arijit-itobuz avatar Apr 07 '25 06:04 arijit-itobuz