graphql / dataloader

DataLoader is a generic utility to be used as part of your application's data fetching layer to provide a consistent API over various backends and reduce requests to those backends via batching and caching.
MIT License
12.89k stars 515 forks source link

[QUESTION]Uncaught (in promise) Error: dataloader_1.default is not a constructor #296

Closed joeyren-cn closed 2 years ago

joeyren-cn commented 2 years ago

When I use dataloader in nestjs, always report Uncaught (in promise) Error: dataloader_1.default is not a constructor Could anyone explain why this is happening?

import { Resolver, Query } from '@nestjs/graphql';
import DataLoader from 'dataloader';
import { LRUMap } from 'lru_map';
import { Test } from './test.model';

@Resolver(Test)
export class TestResolver {
  @Query(() => [Test])
  async test(): Promise<Test[]> {
    const results: Test[] = [
      { id: '1', name: 'Chicago' },
      { id: '2', name: 'New York' },
      { id: '3', name: 'San Francisco' },
    ];
    async function batchFunction(ids: string[]): Promise<Test[]> {
      return ids.map((id) => results.find((result) => result.id === id));
    }
    const loader = new DataLoader(batchFunction, {
      cacheMap: new LRUMap(100),
    });
    await loader.load('1');
    return results;
  }
}

image

joeyren-cn commented 2 years ago

I find the answer: https://github.com/typings/typings/issues/670 , should use eg: import * as DataLoader from 'dataloader'