Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency injection into the Factory doesn't seem to work #9

Open
Pikachews opened this issue Oct 31, 2024 · 0 comments
Open

Dependency injection into the Factory doesn't seem to work #9

Pikachews opened this issue Oct 31, 2024 · 0 comments

Comments

@Pikachews
Copy link

Despite this line, when attempting to inject a service into a LoaderFactory, it does not appear to be successful.

When testing against my own application, it says that it cannot find the service despite it being part of the same module.

When testing by modifying Loader.test.ts, it appears to fail and simply be undefined:

import request from "supertest";
import Dataloader from "dataloader";
import { describe } from "vitest";
import { Controller, Get, Injectable } from "@nestjs/common";
import { NestExpressApplication } from "@nestjs/platform-express";
import { Test } from "@nestjs/testing";
import { DataloaderModule, DataloaderFactory, type LoaderFrom, Loader } from "@strv/nestjs-dataloader";

describe("@Loader()", (it) => {
  it("injects the dataloader instance into the request handler", async (t) => {
    @Injectable()
    class TestService {
      getHello() {
        return "Hello World!";
      }
    }

    @Injectable()
    class SampleLoaderFactory extends DataloaderFactory<unknown, unknown> {
      private testService: TestService;
      constructor(testService: TestService) {
        super();
        this.testService = testService;
      }

      load = async (keys: unknown[]) => {
        console.log(this.testService.getHello());
        return await Promise.resolve(keys);
      };
      id = (key: unknown) => key;
    }

    @Controller()
    class TestController {
      @Get("/")
      async handle(@Loader(SampleLoaderFactory) loader: LoaderFrom<SampleLoaderFactory>) {
        t.expect(loader).toBeInstanceOf(Dataloader);
        await loader.load("test");
      }
    }

    const module = await Test.createTestingModule({
      imports: [DataloaderModule.forRoot(), DataloaderModule.forFeature([SampleLoaderFactory])],
      providers: [TestService],
      controllers: [TestController],
      exports: [DataloaderModule, TestService],
    }).compile();
    const app = await module.createNestApplication<NestExpressApplication>().init();
    t.onTestFinished(async () => await app.close());

    await request(app.getHttpServer()).get("/");
  });
});
[Nest] 64194  - 10/30/2024, 6:27:50 PM   ERROR [ExceptionsHandler] Cannot read properties of undefined (reading 'getHello')
TypeError: Cannot read properties of undefined (reading 'getHello')
    at SampleLoaderFactory.load (nestjs-dataloader/test/Loader.test.ts:27:38)
    at SampleLoaderFactory.#load (nestjs-dataloader/src/Dataloader.factory.ts:61:32)
    at DataLoader._batchLoadFn (nestjs-dataloader/src/Dataloader.factory.ts:19:23)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant