Skip to content

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

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

Closed
Pikachews opened this issue Oct 31, 2024 · 2 comments · Fixed by #12
Closed

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

Pikachews opened this issue Oct 31, 2024 · 2 comments · Fixed by #12
Assignees

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)
@m19c
Copy link

m19c commented May 14, 2025

Hmmm... looks like that there is no intention to actually move this project forward. ¯_(ツ)_/¯
I'll stick to the original Dataloader with some context creation to fulfil my needs.

@robertrossmann robertrossmann self-assigned this May 17, 2025
robertrossmann added a commit that referenced this issue May 18, 2025
fix: rework API to support injecting providers into Factory (fixes #9)
@robertrossmann
Copy link
Member

Hey, apologies for the late reaction. Thank you for reporting this, I have made a fix for the problem and released it in v0.3, please give it a try and let me know if there are other issues.

Also please have a second look at the readme as I had to change how the loader factories are registered into Nest.js DI container, unfortunately.

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

Successfully merging a pull request may close this issue.

3 participants