Skip to content

Commit e550b67

Browse files
Merge pull request #2262 from p-mcgowan/docs/add-inquirer-example-to-provider-scopes
docs(provider-scopes): add documentation for `INQUIRER` scope
2 parents 8a2ae1a + a59db60 commit e550b67

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

content/fundamentals/provider-scopes.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,44 @@ export class CatsService {
105105

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

108+
#### Inquirer provider
109+
110+
If you want get the class where a provider was constructed, for instance in logging or metrics providers, you can inject the `INQUIRER` token.
111+
112+
```typescript
113+
import { Inject, Injectable, Scope } from '@nestjs/common';
114+
import { INQUIRER } from '@nestjs/core';
115+
116+
@Injectable({ scope: Scope.TRANSIENT })
117+
export class HelloService {
118+
constructor(@Inject(INQUIRER) private parentClass: object) {}
119+
120+
sayHello(message: string) {
121+
console.log(`${this.parentClass?.constructor?.name}: ${message}`);
122+
}
123+
}
124+
```
125+
126+
And then use it as follows:
127+
128+
```typescript
129+
import { Injectable } from '@nestjs/common';
130+
import { HelloService } from './hello.service';
131+
132+
@Injectable()
133+
export class AppService {
134+
constructor(private helloService: HelloService) {}
135+
136+
getRoot(): string {
137+
this.helloService.sayHello('My name is getRoot');
138+
139+
return 'Hello world!';
140+
}
141+
}
142+
```
143+
144+
In the example above when `AppService#getRoot` is called, `"AppService: My name is getRoot"` will be logged to the console.
145+
108146
#### Performance
109147

110148
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)