Skip to content

Commit

Permalink
feature() add external context, microservices event patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jan 4, 2019
1 parent f0e9b40 commit 4f76708
Show file tree
Hide file tree
Showing 85 changed files with 5,026 additions and 211 deletions.
3 changes: 1 addition & 2 deletions integration/graphql/e2e/graphql-async-class.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*import { INestApplication } from '@nestjs/common';
import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import * as request from 'supertest';
import { AsyncClassApplicationModule } from '../src/async-options-class.module';
Expand Down Expand Up @@ -36,4 +36,3 @@ describe('GraphQL (async class)', () => {
await app.close();
});
});
*/
3 changes: 1 addition & 2 deletions integration/graphql/e2e/graphql-async-existing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*import { INestApplication } from '@nestjs/common';
import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import * as request from 'supertest';
import { AsyncExistingApplicationModule } from '../src/async-options-existing.module';
Expand Down Expand Up @@ -36,4 +36,3 @@ describe('GraphQL (async existing)', () => {
await app.close();
});
});
*/
3 changes: 1 addition & 2 deletions integration/graphql/e2e/graphql-async.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*import { INestApplication } from '@nestjs/common';
import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import * as request from 'supertest';
import { AsyncApplicationModule } from '../src/async-options.module';
Expand Down Expand Up @@ -34,4 +34,3 @@ describe('GraphQL (async configuration)', () => {
await app.close();
});
});
*/
60 changes: 60 additions & 0 deletions integration/graphql/e2e/graphql-request-scoped.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { INestApplication } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import { join } from 'path';
import * as request from 'supertest';
import { CatsRequestScopedService } from '../src/cats/cats-request-scoped.service';
import { CatsModule } from '../src/cats/cats.module';

describe('GraphQL request scoped', () => {
let app: INestApplication;

beforeEach(async () => {
const module = await Test.createTestingModule({
imports: [
CatsModule.enableRequestScope(),
GraphQLModule.forRoot({
typePaths: [join(__dirname, '..', 'src', '**', '*.graphql')],
}),
],
}).compile();

app = module.createNestApplication();
await app.init();

const performHttpCall = end =>
request(app.getHttpServer())
.post('/graphql')
.send({
operationName: null,
variables: {},
query: '{\n getCats {\n id\n }\n}\n',
})
.expect(200, {
data: {
getCats: [
{
id: 1,
},
],
},
})
.end((err, res) => {
if (err) return end(err);
end();
});

await new Promise(resolve => performHttpCall(resolve));
await new Promise(resolve => performHttpCall(resolve));
await new Promise(resolve => performHttpCall(resolve));
});

it(`should create resolver for each incoming request`, () => {
expect(CatsRequestScopedService.COUNTER).to.be.eql(3);
});

afterEach(async () => {
await app.close();
});
});
3 changes: 1 addition & 2 deletions integration/graphql/e2e/graphql.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*import { INestApplication } from '@nestjs/common';
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { ApplicationModule } from '../src/app.module';
Expand Down Expand Up @@ -38,4 +38,3 @@ describe('GraphQL', () => {
await app.close();
});
});
*/
25 changes: 25 additions & 0 deletions integration/graphql/src/cats/cats-request-scoped.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Injectable, Scope } from '@nestjs/common';
import { Cat } from './interfaces/cat.interface';

@Injectable({ scope: Scope.REQUEST })
export class CatsRequestScopedService {
static COUNTER = 0;
private readonly cats: Cat[] = [{ id: 1, name: 'Cat', age: 5 }];

constructor() {
CatsRequestScopedService.COUNTER++;
}

create(cat: Cat): Cat {
this.cats.push(cat);
return cat;
}

findAll(): Cat[] {
return this.cats;
}

findOneById(id: number): Cat {
return this.cats.find(cat => cat.id === id);
}
}
20 changes: 17 additions & 3 deletions integration/graphql/src/cats/cats.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
import { Module } from '@nestjs/common';
import { CatsService } from './cats.service';
import { DynamicModule, Module, Scope } from '@nestjs/common';
import { CatsRequestScopedService } from './cats-request-scoped.service';
import { CatsResolvers } from './cats.resolvers';
import { CatsService } from './cats.service';

@Module({
providers: [CatsService, CatsResolvers],
})
export class CatsModule {}
export class CatsModule {
static enableRequestScope(): DynamicModule {
return {
module: CatsModule,
providers: [
{
provide: CatsService,
useClass: CatsRequestScopedService,
scope: Scope.REQUEST,
},
],
};
}
}
5 changes: 5 additions & 0 deletions integration/graphql/src/cats/cats.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import { Cat } from './interfaces/cat.interface';

@Injectable()
export class CatsService {
static COUNTER = 0;
private readonly cats: Cat[] = [{ id: 1, name: 'Cat', age: 5 }];

constructor() {
CatsService.COUNTER++;
}

create(cat: Cat): Cat {
this.cats.push(cat);
return cat;
Expand Down
13 changes: 13 additions & 0 deletions integration/microservices/e2e/sum-mqtt.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { Transport } from '@nestjs/microservices';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import * as request from 'supertest';
import { MqttController } from '../src/mqtt/mqtt.controller';

Expand Down Expand Up @@ -70,6 +71,18 @@ describe('MQTT transport', () => {
.expect(200, '15');
});

it(`/POST (event notification)`, done => {
request(server)
.post('/notify')
.send([1, 2, 3, 4, 5])
.end(() => {
setTimeout(() => {
expect(MqttController.IS_NOTIFIED).to.be.true;
done();
}, 1000);
});
});

afterEach(async () => {
await app.close();
});
Expand Down
13 changes: 13 additions & 0 deletions integration/microservices/e2e/sum-nats.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { Transport } from '@nestjs/microservices';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import * as request from 'supertest';
import { NatsController } from '../src/nats/nats.controller';

Expand Down Expand Up @@ -82,6 +83,18 @@ describe('NATS transport', () => {
});
});

it(`/POST (event notification)`, done => {
request(server)
.post('/notify')
.send([1, 2, 3, 4, 5])
.end(() => {
setTimeout(() => {
expect(NatsController.IS_NOTIFIED).to.be.true;
done();
}, 1000);
});
});

afterEach(async () => {
await app.close();
});
Expand Down
13 changes: 13 additions & 0 deletions integration/microservices/e2e/sum-redis.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { Transport } from '@nestjs/microservices';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import * as request from 'supertest';
import { RedisController } from '../src/redis/redis.controller';

Expand Down Expand Up @@ -70,6 +71,18 @@ describe('REDIS transport', () => {
.expect(200, '15');
});

it(`/POST (event notification)`, done => {
request(server)
.post('/notify')
.send([1, 2, 3, 4, 5])
.end(() => {
setTimeout(() => {
expect(RedisController.IS_NOTIFIED).to.be.true;
done();
}, 1000);
});
});

afterEach(async () => {
await app.close();
});
Expand Down
13 changes: 13 additions & 0 deletions integration/microservices/e2e/sum-rmq.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { Transport } from '@nestjs/microservices';
import { Test } from '@nestjs/testing';
import { expect } from 'chai';
import * as request from 'supertest';
import { RMQController } from '../src/rmq/rmq.controller';

Expand Down Expand Up @@ -75,6 +76,18 @@ describe('RabbitMQ transport', () => {
.expect(200, '15');
});

it(`/POST (event notification)`, done => {
request(server)
.post('/notify')
.send([1, 2, 3, 4, 5])
.end(() => {
setTimeout(() => {
expect(RMQController.IS_NOTIFIED).to.be.true;
done();
}, 1000);
});
});

afterEach(async () => {
await app.close();
});
Expand Down
13 changes: 13 additions & 0 deletions integration/microservices/src/mqtt/mqtt.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Body, Controller, HttpCode, Post, Query } from '@nestjs/common';
import {
Client,
ClientProxy,
EventPattern,
MessagePattern,
Transport,
} from '@nestjs/microservices';
Expand All @@ -10,6 +11,8 @@ import { scan } from 'rxjs/operators';

@Controller()
export class MqttController {
static IS_NOTIFIED = false;

@Client({ transport: Transport.MQTT })
client: ClientProxy;

Expand Down Expand Up @@ -47,6 +50,16 @@ export class MqttController {
.reduce(async (a, b) => (await a) && (await b));
}

@Post('notify')
async sendNotification(): Promise<any> {
return this.client.emit<number>('notification', true);
}

@EventPattern('notification')
eventHandler(data: boolean) {
MqttController.IS_NOTIFIED = true;
}

@MessagePattern({ cmd: 'sum' })
sum(data: number[]): number {
return (data || []).reduce((a, b) => a + b);
Expand Down
13 changes: 13 additions & 0 deletions integration/microservices/src/nats/nats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Body, Controller, Get, HttpCode, Post, Query } from '@nestjs/common';
import {
Client,
ClientProxy,
EventPattern,
MessagePattern,
RpcException,
Transport,
Expand All @@ -11,6 +12,8 @@ import { catchError, scan } from 'rxjs/operators';

@Controller()
export class NatsController {
static IS_NOTIFIED = false;

@Client({
transport: Transport.NATS,
options: {
Expand Down Expand Up @@ -84,4 +87,14 @@ export class NatsController {
throwError(): Observable<number> {
return throwError(new RpcException('test'));
}

@Post('notify')
async sendNotification(): Promise<any> {
return this.client.emit<number>('notification', true);
}

@EventPattern('notification')
eventHandler(data: boolean) {
NatsController.IS_NOTIFIED = data;
}
}
19 changes: 16 additions & 3 deletions integration/microservices/src/redis/redis.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Controller, Post, Body, Query, HttpCode } from '@nestjs/common';
import { Body, Controller, HttpCode, Post, Query } from '@nestjs/common';
import {
Client,
MessagePattern,
ClientProxy,
EventPattern,
MessagePattern,
Transport,
} from '@nestjs/microservices';
import { Observable, of, from } from 'rxjs';
import { from, Observable, of } from 'rxjs';
import { scan } from 'rxjs/operators';

@Controller()
export class RedisController {
static IS_NOTIFIED = false;

@Client({ transport: Transport.REDIS })
client: ClientProxy;

Expand Down Expand Up @@ -62,4 +65,14 @@ export class RedisController {
streaming(data: number[]): Observable<number> {
return from(data);
}

@Post('notify')
async sendNotification(): Promise<any> {
return this.client.emit<number>('notification', true);
}

@EventPattern('notification')
eventHandler(data: boolean) {
RedisController.IS_NOTIFIED = data;
}
}
Loading

0 comments on commit 4f76708

Please sign in to comment.