graphql dataloader не может прочитать свойство 'load' неопределенной ошибки

Я хотел бы привести данные о местоположении, которые существуют под таблицей location_group. Вы пытаетесь использовать DataLoader, потому что существует проблема n + 1.

Я думал, что это сделано, но когда я действительно запускаю код, я получаю Cannot read property 'load' of undefined error. Не могли бы вы мне сказать, почему возникает эта ошибка?

Resolver код:

import {
    Args,
    Context,
    ID,
    Mutation,
    Parent,
    Query,
    ResolveField,
    Resolver
} from '@nestjs/graphql';
import { Location_Group } from 'src/entities/location_group.entity';
import { Location } from 'src/entities/location.entity';
import { LocationDataInput } from 'src/input_graphQl/LocationDataInput.input';
import { LocationGroupDataInput } from 'src/input_graphQl/LocationGroupDataInput.input';
import { LocationService } from './location.service';
import { LocationGroupWhereInput } from 'src/input_graphQl/LocationGroupWhereInput.input';
import { GeoJSONPoint } from 'src/scalar/geoJSONPoint.scalar';
import { LocationConText } from 'src/loader/LocationConText.context';

@Resolver(() => Location_Group)
export class LocationResolver2 {
    constructor(private readonly locationService: LocationService) {}
    @ResolveField(() => [Location])
    locations(
        @Parent() location_group: Location_Group,
        @Context() ctx: LocationConText
    ): Promise<Location[]> {
        console.log(location_group);
        return ctx.LocationDataLoader.load(location_group.id);
    }
}

DataLoader код:

import DataLoader from 'dataloader';
import { Location_Group } from 'src/entities/location_group.entity';
import { getRepository } from 'typeorm';

export const LocationDataLoader = () =>
    new DataLoader(async (keys: number[]) => {
        const loc = await getRepository(Location_Group)
            .createQueryBuilder('lg')
            .leftJoinAndSelect('lg.locations', 'locations')
            .where('lg.id IN (:...keys)', { keys })
            .getMany();
        return loc.map((element) => element.locations);
    });

LocationConText код:

import DataLoader from 'dataloader';
import { Request, Response } from 'express';
import { Location } from 'src/entities/location.entity';

export interface LocationConText {
    req: Request;
    res: Response;
    LocationDataLoader: DataLoader<number, Location[]>;
}

Значение результата, когда DataLoader не используется. введите описание изображения здесь

Значение результата при использовании DataLoader. введите описание изображения здесь


person momo    schedule 18.06.2021    source источник


Ответы (1)


Я нашел ответ.

импортировать DataLoader из 'dataloader';

=>

import * как DataLoader из 'dataloader';

Очень жаль, что я потратил на это два дня.

person momo    schedule 19.06.2021