Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions example/__tests__/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { Test, TestingModule } from "@nestjs/testing";
import { INestApplication } from "@nestjs/common";
import { GraphQLModule } from "@nestjs/graphql";
import { createTestClient } from "apollo-server-testing";
import { ApolloServerBase } from 'apollo-server-core';
import gql from "graphql-tag";
import { AppModule } from "./../src/app.module";
import { Factory } from 'typeorm-factory'
import { Factory } from '@linnify/typeorm-factory';
import { Account } from "../src/account/account.entity";
import { ApolloDriver } from "@nestjs/apollo";

class AccountFactory extends Factory<Account> {
entity = Account;
name = 'name'
}

describe("AppModule", () => {
let app: INestApplication;
let apolloClient: ReturnType<typeof createTestClient>;
let apolloClient: ApolloServerBase;

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
Expand All @@ -19,22 +25,18 @@ describe("AppModule", () => {
app = moduleFixture.createNestApplication();
await app.init();

const module: GraphQLModule = moduleFixture.get<GraphQLModule>(
GraphQLModule
);
// apolloServer is protected, we need to cast module to any to get it
apolloClient = createTestClient((module as any).apolloServer);
const graphqlModule = app.get<GraphQLModule<ApolloDriver>>(GraphQLModule);
apolloClient = graphqlModule.graphQlAdapter?.instance;
});

afterAll(() => app.close());

it("defined", () => expect(app).toBeDefined());

it("/graphql(POST) getAccounts", async () => {
const f = new Factory(Account).attr('name', 'name')
const account = await f.create()
const { query } = apolloClient;
const result = await query({
const f = new AccountFactory();
const account = await f.create();
const result = await apolloClient.executeOperation({
query: gql`
query q($ids: [ID!]!) {
getAccounts(ids: $ids) {
Expand All @@ -46,6 +48,6 @@ describe("AppModule", () => {
ids: [account.id],
},
});
expect(result.errors).toBeUndefined()
expect(result.errors).toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion example/src/account/account.loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import DataLoader = require("dataloader");
import DataLoader from "dataloader";
import { Injectable } from "@nestjs/common";
import { NestDataLoader } from "../../..";
import { AccountService } from "./account.service";
Expand Down
2 changes: 1 addition & 1 deletion example/src/account/account.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class AccountResolver {
@Query(() => [Account])
public getAccounts(
@Args({ name: "ids", type: () => [ID] }) ids: string[],
@Loader(AccountLoader.name)
@Loader(AccountLoader)
accountLoader: DataLoader<Account["id"], Account>
): Promise<(Account | Error)[]> {
return accountLoader.loadMany(ids);
Expand Down
4 changes: 3 additions & 1 deletion example/src/account/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export class AccountService {

async findByIds(ids: readonly string[]) {
return this.accounts.find({
id: In(ids as string[]),
where: {
id: In(ids as string[]),
},
});
}
}
4 changes: 3 additions & 1 deletion example/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Module } from "@nestjs/common";
import { GraphQLModule } from "@nestjs/graphql";
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { TypeOrmModule } from "@nestjs/typeorm";
import { join } from "path";
import { AccountModule } from "./account/account.module";

@Module({
imports: [
GraphQLModule.forRoot({
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
autoSchemaFile: true,
debug: true,
}),
Expand Down
4 changes: 3 additions & 1 deletion example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"outDir": "./dist"
"outDir": "./dist",
"skipLibCheck": true,
"esModuleInterop": true
},
"include": ["src"],
"exclude": ["node_modules", "*.spec.ts"]
Expand Down
12 changes: 5 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import {
} from '@nestjs/common';
import { APP_INTERCEPTOR, ModuleRef, ContextIdFactory } from '@nestjs/core';
import { GqlExecutionContext } from '@nestjs/graphql';
import * as DataLoader from 'dataloader';
import { Observable } from 'rxjs';
import { idText } from 'typescript';
import DataLoader from "dataloader";

/**
* This interface will be used to generate the initial data loader.      
* This interface will be used to generate the initial data loader.      
* The concrete implementation should be added as a provider to your module.
*/
export interface NestDataLoader<ID, Type> {
Expand All @@ -39,7 +37,7 @@ export class DataLoaderInterceptor implements NestInterceptor {
/**
* @inheritdoc
*/
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
intercept(context: ExecutionContext, next: CallHandler) {
const graphqlExecutionContext = GqlExecutionContext.create(context);
const ctx = graphqlExecutionContext.getContext();

Expand All @@ -48,8 +46,8 @@ export class DataLoaderInterceptor implements NestInterceptor {
contextId: ContextIdFactory.create(),
getLoader: (type: string) : Promise<NestDataLoader<any, any>> => {
if (ctx[type] === undefined) {
try {
ctx[type] = (async () => {
try {
ctx[type] = (async () => {
return (await this.moduleRef.resolve<NestDataLoader<any, any>>(type, ctx[NEST_LOADER_CONTEXT_KEY].contextId, { strict: false }))
.generateDataLoader();
})();
Expand Down
53 changes: 28 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,38 @@
"graphql"
],
"peerDependencies": {
"@nestjs/common": "^7.0.0",
"@nestjs/core": "^7.0.0",
"@nestjs/graphql": "^7.0.0",
"graphql": "^14.1.1",
"reflect-metadata": "^0.1.12"
"@apollo/gateway": "^2.1.0",
"@nestjs/common": "^9.0.11",
"@nestjs/core": "^9.0.11",
"@nestjs/graphql": "^10.0.22",
"graphql": "^16.6.0",
"reflect-metadata": "^0.1.13"
},
"dependencies": {
"dataloader": "^2.0.0",
"rxjs": "^6.5.4"
"dataloader": "^2.1.0"
},
"devDependencies": {
"@nestjs/cli": "^7.1.2",
"@nestjs/common": "^7.0.5",
"@nestjs/core": "^7.0.5",
"@nestjs/graphql": "^7.0.15",
"@nestjs/platform-express": "^7.0.7",
"@nestjs/testing": "^7.0.7",
"@nestjs/typeorm": "^7.0.0",
"@types/jest": "^25.2.1",
"apollo-server-express": "^2.9.16",
"apollo-server-testing": "^2.11.0",
"graphql": "^14.1.1",
"jest": "^25.2.7",
"reflect-metadata": "^0.1.12",
"sqlite3": "^4.1.1",
"ts-jest": "^25.3.1",
"typeorm": "^0.2.24",
"typeorm-factory": "^0.0.14",
"typescript": "^3.7.4"
"@apollo/gateway": "^2.1.0",
"@linnify/typeorm-factory": "^1.0.12",
"@nestjs/apollo": "^10.0.22",
"@nestjs/cli": "^9.1.1",
"@nestjs/common": "^9.0.11",
"@nestjs/core": "^9.0.11",
"@nestjs/graphql": "^10.0.22",
"@nestjs/platform-express": "^9.0.11",
"@nestjs/testing": "^9.0.11",
"@nestjs/typeorm": "^9.0.1",
"@types/jest": "^28.1.8",
"apollo-server-express": "^3.10.2",
"graphql": "^16.6.0",
"jest": "^29.0.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.5.6",
"sqlite3": "^5.0.11",
"ts-jest": "^28.0.8",
"ts-morph": "^15.1.0",
"typeorm": "^0.3.9",
"typescript": "^4.8.2"
},
"types": "index.d.ts",
"jest": {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"outDir": "./dist",
"baseUrl": "./",
"esModuleInterop": true,
"noUnusedLocals": true
},
"exclude": ["node_modules", "dist", "example"]
}
Loading