Skip to content

Type error on @Loader() decorator use (typescript 5.8.3, NestJS 11.1.3 ) #19

@GauthierPLM

Description

@GauthierPLM

When using the @loader() decorator, to inject the loader in my resolver' @query / @ResolveField function, I get the following error:

TS2345: Argument of type typeof MyExpertLoaderFactory is not assignable to parameter of type
Factory | PipeTransform<any, any> | Type<PipeTransform<any, any>>
Type typeof MyExpertLoaderFactory is not assignable to type Factory
Construct signature return types MyExpertLoaderFactory and
DataloaderFactory<unknown, unknown, null, unknown>
are incompatible.
The types of options are incompatible between these types.
Type
((context: ExecutionContext) => Options<string, MyExpert, string>) | undefined
is not assignable to type
((context: ExecutionContext) => Options<unknown, unknown, unknown>) | undefined
Type
(context: ExecutionContext) => Options<string, MyExpert, string>
is not assignable to type
(context: ExecutionContext) => Options<unknown, unknown, unknown>
Type Options<string, MyExpert, string> is not assignable to type Options<unknown, unknown, unknown>
Type unknown is not assignable to type string

Unfortunately, I was not able to find the root cause of this.
In the example below, the error is reported in TestResolver.experts().

import { ExecutionContext, Injectable } from "@nestjs/common";
import { Query, Resolver } from "@nestjs/graphql";
import {
  DataloaderFactory,
  Loader,
  type LoaderFrom,
} from "@strv/nestjs-dataloader";

import { ExpertService } from "@api/users/expert.service";

type MyExpertId = string;

interface MyExpert {
  id: MyExpertId;
  name: string;
}

@Injectable()
export class MyExpertLoaderFactory extends DataloaderFactory<
  MyExpertId,
  MyExpert
> {
  constructor(private readonly expertService: ExpertService) {
    super();
  }

  async load(ids: readonly MyExpertId[], _: ExecutionContext) {
    return (await this.expertService.findByIds(
      ids as MyExpertId[],
    )) as MyExpert[];
  }

  id(entity: MyExpert) {
    return entity.id;
  }
}

export type MyExpertLoader = LoaderFrom<MyExpertLoaderFactory>;

@Resolver()
export class TestResolver {
  @Query()
  experts(@Loader(MyExpertLoaderFactory) expertsLoader: MyExpertLoader) {
    return expertsLoader.loadMany(["1", "2"]);
  }
}

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions