Skip to content
Closed
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
23 changes: 10 additions & 13 deletions example/__tests__/app.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Test, TestingModule } from "@nestjs/testing";
import { INestApplication } from "@nestjs/common";
import { GraphQLModule } from "@nestjs/graphql";
import { createTestClient } from "apollo-server-testing";
import gql from "graphql-tag";
import { AppModule } from "./../src/app.module";
import { Factory } from 'typeorm-factory'
import { Factory } from "typeorm-factory";
import { Account } from "../src/account/account.entity";

describe("AppModule", () => {
let app: INestApplication;
let apolloClient: ReturnType<typeof createTestClient>;
let server: GraphQLModule["apolloServer"];

beforeAll(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
Expand All @@ -19,22 +18,20 @@ 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 module: GraphQLModule =
moduleFixture.get<GraphQLModule>(GraphQLModule);

server = module.apolloServer;
});

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 Factory(Account).attr("name", "name");
const account = await f.create();
const result = await server.executeOperation({
query: gql`
query q($ids: [ID!]!) {
getAccounts(ids: $ids) {
Expand All @@ -46,6 +43,6 @@ describe("AppModule", () => {
ids: [account.id],
},
});
expect(result.errors).toBeUndefined()
expect(result.errors).toBeUndefined();
});
});
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
3 changes: 2 additions & 1 deletion example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"outDir": "./dist"
"outDir": "./dist",
"skipLibCheck": 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 = require("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
44 changes: 23 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,37 @@
"graphql"
],
"peerDependencies": {
"@nestjs/common": "^7.0.0",
"@nestjs/core": "^7.0.0",
"@nestjs/graphql": "^7.0.0",
"graphql": "^14.1.1",
"@apollo/gateway": "^0.41.0",
"@nestjs/common": "^8.0.6",
"@nestjs/core": "^8.0.6",
"@nestjs/graphql": "^9.0.4",
"graphql": "^15.5.3",
"reflect-metadata": "^0.1.12"
},
"dependencies": {
"dataloader": "^2.0.0",
"rxjs": "^6.5.4"
"dataloader": "^2.0.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",
"@apollo/gateway": "^0.41.0",
"@nestjs/cli": "^8.1.1",
"@nestjs/common": "^8.0.6",
"@nestjs/core": "^8.0.6",
"@nestjs/graphql": "^9.0.4",
"@nestjs/platform-express": "^8.0.6",
"@nestjs/testing": "^8.0.6",
"@nestjs/typeorm": "^8.0.2",
"@types/jest": "^27.0.1",
"apollo-server-express": "^3.3.0",
"graphql": "^15.5.3",
"jest": "^27.2.0",
"reflect-metadata": "^0.1.12",
"sqlite3": "^4.1.1",
"ts-jest": "^25.3.1",
"rxjs": "^7.3.0",
"sqlite3": "^5.0.2",
"ts-jest": "^27.0.5",
"ts-morph": "^12.0.0",
"typeorm": "^0.2.24",
"typeorm-factory": "^0.0.14",
"typescript": "^3.7.4"
"typescript": "^4.4.3"
},
"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