-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature() add external context, microservices event patterns
- Loading branch information
1 parent
f0e9b40
commit 4f76708
Showing
85 changed files
with
5,026 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
integration/graphql/src/cats/cats-request-scoped.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.