Skip to content

Commit d63c2f6

Browse files
2 parents a1736f2 + 21279e4 commit d63c2f6

File tree

21 files changed

+272
-142
lines changed

21 files changed

+272
-142
lines changed

content/cli/usages.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ $ nest g <schematic> <name> [options]
5555

5656
| Name | Alias | Description |
5757
| ------------- | ----- | --------------------------------------------------------------------------------------------------- |
58-
| `application` | | Generate a new application within a monorepo (converting to monorepo if it's a standard structure). |
58+
| `app` | | Generate a new application within a monorepo (converting to monorepo if it's a standard structure). |
5959
| `library` | `lib` | Generate a new library within a monorepo (converting to monorepo if it's a standard structure). |
6060
| `class` | `cl` | Generate a new class. |
6161
| `controller` | `co` | Generate a controller declaration. |

content/cli/workspaces.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ The `generate app` schematic has reorganized the code - moving each **applicatio
9393
9494
#### Workspace projects
9595

96-
A mono repo uses the concept of a workspace to manage its member entities. Workspaces are composed of **projects**. A project may be either:
96+
A monorepo uses the concept of a workspace to manage its member entities. Workspaces are composed of **projects**. A project may be either:
9797

9898
- an **application**: a full Nest application including a `main.ts` file to bootstrap the application. Aside from compile and build considerations, an application-type project within a workspace is functionally identical to an application within a _standard mode_ structure.
9999
- a **library**: a library is a way of packaging a general purpose set of features (modules, providers, controllers, etc.) that can be used within other projects. A library cannot run on its own, and has no `main.ts` file. Read more about libraries [here](/cli/libraries).

content/components.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,11 @@ This is how our directory structure should look now:
203203
<div class="item">main.ts</div>
204204
</div>
205205
</div>
206+
207+
#### Manual instantiation
208+
209+
Thus far, we've discussed how Nest automatically handles most of the details of resolving dependencies. In certain circumstances, you may need to step outside of the built-in Dependency Injection system and manually retrieve or instantiate providers. We briefly discuss two such topics below.
210+
211+
To get existing instances, or instantiate providers dynamically, you can use [Module reference](https://docs.nestjs.com/fundamentals/module-ref).
212+
213+
To get providers within the `bootstrap()` function (for example for standalone applications without controllers, or to utilize a configuration service during bootstrapping) see [Standalone applications](https://docs.nestjs.com/standalone-applications).

content/discover/who-uses.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@
141141
"logo": "/assets/logo/zeoagency.svg",
142142
"url": "https://zeo.org/tr/",
143143
"width": "80px"
144+
},
145+
{
146+
"logo": "/assets/logo/valueadd.png",
147+
"url": "https://valueadd.pl/",
148+
"width": "120px"
144149
}
145150
],
146151
"Body": [

content/faq/hybrid-application.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
### Hybrid application
22

3-
A hybrid application is one that both listens for HTTP requests, as well as makes use of connected microservices. The `INestApplication` instance can be connected with `INestMicroservice` instances through the `connectMicroservice()` method. To connect multiple microservice instances, simply pass additional microservice configuration objects as arguments in a comma-separated list.
3+
A hybrid application is one that both listens for HTTP requests, as well as makes use of connected microservices. The `INestApplication` instance can be connected with `INestMicroservice` instances through the `connectMicroservice()` method.
44

55
```typescript
66
const app = await NestFactory.create(AppModule);
@@ -11,3 +11,26 @@ const microservice = app.connectMicroservice({
1111
await app.startAllMicroservicesAsync();
1212
await app.listen(3001);
1313
```
14+
15+
To connect multiple microservice instances, issue the call to `connectMicroservice()` for each microservice:
16+
17+
```typescript
18+
const app = await NestFactory.create(AppModule);
19+
// microservice #1
20+
const microserviceTcp = app.connectMicroservice<MicroserviceOptions>({
21+
transport: Transport.TCP,
22+
options: {
23+
port: 3001,
24+
},
25+
});
26+
// microservice #2
27+
const microserviceRedis = app.connectMicroservice<MicroserviceOptions>({
28+
transport: Transport.REDIS,
29+
options: {
30+
url: 'redis://localhost:6379',
31+
},
32+
});
33+
34+
await app.startAllMicroservicesAsync();
35+
await app.listen(3001);
36+
```

content/fundamentals/dependency-injection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export class AppModule {}
272272

273273
#### Alias providers: `useExisting`
274274

275-
The `useExisting` syntax allows you to create aliases for existing providers. This creates two ways to access the same provider. In the example below, the (string-based) token `'AliasedLoggerService'` is an alias for the (class-based) token `LoggerService`. Assume we have two different dependencies, one for `'AlilasedLoggerService'` and one for `LoggerService`. If both dependencies are specified with `SINGLETON` scope, they'll both resolve to the same instance.
275+
The `useExisting` syntax allows you to create aliases for existing providers. This creates two ways to access the same provider. In the example below, the (string-based) token `'AliasedLoggerService'` is an alias for the (class-based) token `LoggerService`. Assume we have two different dependencies, one for `'AliasedLoggerService'` and one for `LoggerService`. If both dependencies are specified with `SINGLETON` scope, they'll both resolve to the same instance.
276276

277277
```typescript
278278
@Injectable()

content/fundamentals/execution-context.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ The `ArgumentsHost` class provides methods for retrieving the arguments being pa
1515
When building generic [guards](/guards), [filters](/exception-filters), and [interceptors](/interceptors) which are meant to run across multiple application contexts, we need a way to determine the type of application that our method is currently running in. Do this with the `getType()` method of `ArgumentsHost`:
1616

1717
```typescript
18-
const type = host.getType();
19-
if (type === 'http') {
20-
// HTTP application
21-
} else if (type === 'rpc') {
22-
// Microservice
18+
if (host.getType() === 'http') {
19+
// do something that is only important in the context of regular HTTP requests (REST)
20+
} else if (host.getType() === 'rpc') {
21+
// do something that is only important in the context of Microservice requests
22+
} else if (host.getType<GqlContextType>() === 'graphql') {
23+
// do something that is only important in the context of GraphQL requests
2324
}
2425
```
2526

27+
> info **Hint** The `GqlContextType` is imported from the `@nestjs/graphql` package.
28+
2629
With the application type available, we can write more generic components, as shown below.
2730

2831
#### Host handler arguments

content/fundamentals/lifecycle-events.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The following diagram depicts the sequence of key application lifecycle events,
1515
Lifecycle events happen during application bootstrapping and shutdown. Nest calls registered lifecycle hook methods on `modules`, `injectables` and `controllers` at each of the following lifecycle events (**shutdown hooks** need to be enabled first, as described [below](https://docs.nestjs.com/fundamentals/lifecycle-events#application-shutdown)). As shown in the diagram above, Nest also calls the appropriate underlying methods to begin listening for connections, and to stop listening for connections.
1616

1717
| Lifecycle hook method | Lifecycle event triggering the hook method call |
18-
| ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
18+
|-------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
1919
| `onModuleInit()` | Called once the host module's dependencies have been resolved. |
2020
| `onApplicationBootstrap()` | Called once all modules have been initialized, but before listening for connections. |
2121
| `onModuleDestroy()` | Called after a termination signal (e.g., `SIGTERM`) has been received. |
@@ -66,6 +66,8 @@ async onModuleInit() {
6666

6767
The `beforeApplicationShutdown()` and `onApplicationShutdown()` hooks are called in the **terminating** phase (in response to system signals such as `SIGTERM`). This feature is often used with [Kubernetes](https://kubernetes.io/), [Heroku](https://www.heroku.com/) or similar services.
6868

69+
> warning **warning** Due to inherent platform limitations, NestJS has limited support for application shutdown hooks on Windows. You can expect `SIGINT` to work, as well as `SIGBREAK` and to some extent `SIGHUP` - [read more](https://nodejs.org/api/process.html#process_signal_events). However `SIGTERM` will never work on Windows because killing a process in the task manager is unconditional, "i.e., there's no way for an application to detect or prevent it". Here's some [relevant documentation](http://docs.libuv.org/en/v1.x/signal.html) from libuv to learn more about how `SIGINT`, `SIGBREAK` and others are handled on Windows. Also, see Node.js documentation of [Process Signal Events](https://nodejs.org/api/process.html#process_signal_events)
70+
6971
To use these hooks you must activate a listener which listens to shutdown signals.
7072

7173
```typescript
@@ -81,6 +83,8 @@ async function bootstrap() {
8183
bootstrap();
8284
```
8385

86+
> info **Info** `enableShutdownHooks` consumes memory by starting listeners. In cases where you are running multiple Nest apps in a single Node process (e.g., when running parallel tests with Jest), Node may complain about excessive listener processes. For this reason, `enableShutdownHooks` is not enabled by default. Be aware of this condition when you are running multiple instances in a single Node process.
87+
8488
When the application receives a termination signal it will call any registered `beforeApplicationShutdown()`, then `onApplicationShutdown()` methods (in the sequence described above) with the corresponding signal as the first parameter. If a registered function awaits an asynchronous call (returns a promise), Nest will not continue in the sequence until the promise is resolved or rejected.
8589

8690
```typescript

content/graphql/enums.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,22 @@ export enum AllowedColor {
6464
}
6565
```
6666

67-
Sometimes a backend forces a different value for an enum internally than in the public API. In this example the API contains `RED`, however in resolvers we may use `#f00` instead (read more [here](https://www.apollographql.com/docs/apollo-server/schema/scalars-enums/#internal-values)). To accomplish this, declare a resolver class for the `AllowedColor` enum:
67+
Sometimes a backend forces a different value for an enum internally than in the public API. In this example the API contains `RED`, however in resolvers we may use `#f00` instead (read more [here](https://www.apollographql.com/docs/apollo-server/schema/scalars-enums/#internal-values)). To accomplish this, declare a resolver object for the `AllowedColor` enum:
6868

6969
```typescript
70-
@Resolver('AllowedColor')
71-
export class AllowedColorResolver {
72-
@ResolveField('RED')
73-
getRedColor() {
74-
return '#f00';
75-
}
70+
export const allowedColorResolver: Record<keyof typeof AllowedColor, any> = {
71+
RED: '#f00',
7672
}
7773
```
7874

7975
> info **Hint** All decorators are exported from the `@nestjs/graphql` package.
76+
77+
Then use this resolver object together with the `resolvers` property of the `GraphQLModule#forRoot()` method, as follows:
78+
79+
```typescript
80+
GraphQLModule.forRoot({
81+
resolvers: {
82+
AllowedColor: allowedColorResolver,
83+
},
84+
})
85+
```

content/graphql/resolvers-map.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export class AuthorsResolver {
203203
) {}
204204

205205
@Query(returns => Author, { name: 'author' })
206-
async getAuthor(@Args('id', {type: () => Int }) id: number) {
206+
async getAuthor(@Args('id', { type: () => Int }) id: number) {
207207
return this.authorsService.findOneById(id);
208208
}
209209

@@ -236,7 +236,7 @@ The `@Query()` decorator's options object (where we pass `{{ '{' }}name: 'author
236236

237237
Use the `@Args()` decorator to extract arguments from a request for use in the method handler. This works in a very similar fashion to [REST route parameter argument extraction](/controllers#route-parameters).
238238

239-
Usually your `@Args()` decorator will be simple, and not require an object argument as seen with the `getAuthor()` method above. For example, if an identifier's type is string, the following construction is sufficient, and simply plucks the named field from the inbound GraphQL request for use as a method argument.
239+
Usually your `@Args()` decorator will be simple, and not require an object argument as seen with the `getAuthor()` method above. For example, if the type of an identifier is string, the following construction is sufficient, and simply plucks the named field from the inbound GraphQL request for use as a method argument.
240240

241241
```typescript
242242
@Args('id') id: string
@@ -245,10 +245,10 @@ Usually your `@Args()` decorator will be simple, and not require an object argum
245245
In the `getAuthor()` case, the `number` type is used, which presents a challenge. The `number` TypeScript type doesn't give us enough information about the expected GraphQL representation (e.g., `Int` vs. `Float`). Thus we have to **explicitly** pass the type reference. We do that by passing a second argument to the `Args()` decorator, containing argument options, as shown below:
246246

247247
```typescript
248-
@Query(returns => Author, { name: 'author' })
249-
async getAuthor(@Args('id', { type: () => Int }) id: number) {
250-
return this.authorsService.findOneById(id);
251-
}
248+
@Query(returns => Author, { name: 'author' })
249+
async getAuthor(@Args('id', { type: () => Int }) id: number) {
250+
return this.authorsService.findOneById(id);
251+
}
252252
```
253253

254254
The options object allows us to specify the following optional key value pairs:

0 commit comments

Comments
 (0)