Skip to content

Commit dc771aa

Browse files
2 parents 4bc5c8a + 4e6cf3d commit dc771aa

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

content/faq/hybrid-application.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,35 @@ await app.startAllMicroservicesAsync();
3535
await app.listen(3001);
3636
```
3737

38+
To bind `@MessagePattern()` to only one transport strategy (for example, MQTT) in a hybrid application with multiple microservices, we can pass the second argument of type `Transport` which is an enum with all the built-in transport strategies defined.
39+
40+
```typescript
41+
@@filename()
42+
@MessagePattern('time.us.*', Transport.NAST)
43+
getDate(@Payload() data: number[], @Ctx() context: NatsContext) {
44+
console.log(`Subject: ${context.getSubject()}`); // e.g. "time.us.east"
45+
return new Date().toLocaleTimeString(...);
46+
}
47+
@MessagePattern({ cmd: 'time.us' }, Transport.TCP)
48+
getTCPDate(@Payload() data: number[]) {
49+
return new Date().toLocaleTimeString(...);
50+
}
51+
@@switch
52+
@Bind(Payload(), Ctx())
53+
@MessagePattern('time.us.*', Transport.NAST)
54+
getDate(data, context) {
55+
console.log(`Subject: ${context.getSubject()}`); // e.g. "time.us.east"
56+
return new Date().toLocaleTimeString(...);
57+
}
58+
@Bind(Payload(), Ctx())
59+
@MessagePattern({ cmd: 'time.us' }, Transport.TCP)
60+
getTCPDate(data, context) {
61+
return new Date().toLocaleTimeString(...);
62+
}
63+
```
64+
65+
> info **Hint** `@Payload()`, `@Ctx()`, `Transport` and `NatsContext` are imported from `@nestjs/microservices`.
66+
3867
#### Sharing configuration
3968

4069
By default a hybrid application will not inherit global pipes, interceptors, guards and filters configured for the main (HTTP-based) application.

content/graphql/complexity.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,14 @@ Alternatively, you can define the estimator function:
8484
@Field({ complexity: (options: ComplexityEstimatorArgs) => ... })
8585
title: string;
8686
```
87+
88+
#### Query/Mutation-level complexity
89+
90+
In addition, `@Query()` and `@Mutation()` decorators may have a `complexity` property specified like so:
91+
92+
```typescript
93+
@Query({ complexity: (options: ComplexityEstimatorArgs) => options.args.count * options.childComplexity })
94+
items(@Args('count') count: number) {
95+
return this.itemsService.getItems({ count });
96+
}
97+
```

content/microservices/basics.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ The request-response message style is useful when you need to **exchange** messa
9797

9898
To enable the request-response message type, Nest creates two logical channels - one is responsible for transferring the data while the other waits for incoming responses. For some underlying transports, such as [NATS](https://nats.io/), this dual-channel support is provided out-of-the-box. For others, Nest compensates by manually creating separate channels. There can be overhead for this, so if you do not require a request-response message style, you should consider using the event-based method.
9999

100-
To create a message handler based on the request-response paradigm use the `@MessagePattern()` decorator, which is imported from the `@nestjs/microservices` package.
100+
To create a message handler based on the request-response paradigm use the `@MessagePattern()` decorator, which is imported from the `@nestjs/microservices` package. This decorator should be used only within the [controller](https://docs.nestjs.com/controllers) classes since they are the entry points for your application. Using them inside providers won't have any effect as they are simply ignored by Nest runtime.
101101

102102
```typescript
103103
@@filename(math.controller)
@@ -348,3 +348,27 @@ export interface RequestContext<T = any> {
348348
```
349349

350350
The `data` property is the message payload sent by the message producer. The `pattern` property is the pattern used to identify an appropriate handler to handle the incoming message.
351+
352+
353+
### Handling timeouts
354+
355+
In distributed systems, sometimes microservices might be down or not available. To avoid infinitely long waiting, you can use Timeouts. A timeout is an incredibly useful pattern when communicating with other services. To apply timeouts to your microservice calls, you can use the `RxJS` timeout operator. If the microservice does not respond to the request within a certain time, an exception is thrown, which can be caught and handled appropriately.
356+
357+
To solve this problem you have to use [rxjs](https://github.com/ReactiveX/rxjs) package. Just use the `timeout` operator in the pipe:
358+
359+
```typescript
360+
@@filename()
361+
this.client
362+
.send<TResult, TInput>(pattern, data)
363+
.pipe(timeout(5000))
364+
.toPromise();
365+
@@switch
366+
this.client
367+
.send(pattern, data)
368+
.pipe(timeout(5000))
369+
.toPromise();
370+
```
371+
372+
> info **Hint** The `timeout` operator is imported from the `rxjs/operators` package.
373+
374+
After 5 seconds, if the microservice isn't responding, it will throw an error.

0 commit comments

Comments
 (0)