Skip to content

Commit bb0bf4a

Browse files
committed
docs(provider-scopes): add documentation for INQUIRER scope
Add example usage of INQUIRER injection token to provider-scopes section. closes #937
1 parent 7d3515a commit bb0bf4a

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

content/fundamentals/provider-scopes.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,42 @@ export class CatsService {
103103

104104
You then configure your `context` value (in the `GraphQLModule`) to contain `request` as its property.
105105

106+
#### Inquirer provider
107+
108+
If you want get the class where a provider was constructed, for instance in logging or metrics providers, you can inject the `INQUIRER` token.
109+
110+
```typescript
111+
import { Inject, Injectable, Scope } from '@nestjs/common';
112+
import { INQUIRER } from '@nestjs/core';
113+
114+
@Injectable({ scope: Scope.TRANSIENT })
115+
export class HelloService {
116+
constructor(@Inject(INQUIRER) private parentClass: object) {}
117+
118+
sayHello(message: string) {
119+
console.log(`${this.parentClass?.constructor?.name}: ${message}`);
120+
}
121+
}
122+
````
123+
124+
```typescript
125+
import { Injectable } from '@nestjs/common';
126+
import { HelloService } from './hello.service';
127+
128+
@Injectable()
129+
export class AppService {
130+
constructor(private helloService: HelloService) {}
131+
132+
getRoot(): string {
133+
this.helloService.sayHello('My name is getRoot');
134+
135+
return 'Hello world!';
136+
}
137+
}
138+
````
139+
140+
In the example above when `AppService.getRoot` is called, `'AppService: My name is getRoot'` will be logged to the console.
141+
106142
#### Performance
107143
108144
Using request-scoped providers will have an impact on application performance. While Nest tries to cache as much metadata as possible, it will still have to create an instance of your class on each request. Hence, it will slow down your average response time and overall benchmarking result. Unless a provider must be request-scoped, it is strongly recommended that you use the default singleton scope.

0 commit comments

Comments
 (0)