Service is created for every child type when using `Scope.REQUEST`
See original GitHub issueI’m submitting a…
[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
Injecting a service using the scope REQUEST
is creating the service for every child type of a query.
Expected behavior
The service should only be created once.
Minimal reproduction of the problem with instructions
In the following query, we try to get all the posts of a specific user.
query {
author(id: 1) {
id
email
name
posts {
id
title
tags
createdAt
}
}
}
The field tags is resolved dynamically using the feature @ResolveProperty()
.
@Resolver('Post')
export class PostResolvers {
constructor(private myService: MyService) {}
@ResolveProperty()
tags() {
// the logic here
}
}
MyService
is going to be created for every post published by the author instead of only one time. Since Scope.REQUEST
was designed to create the service for each request, it looks like a bug,
What is the motivation / use case for changing the behavior?
By using Scope.REQUEST
, we can create a DataLoader for every request.
Environment
Nest version: 6.0.4
For Tooling issues:
- Node version: 11.8.0
- Platform: Mac
Others:
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Asp.net core service per request scope in child scopes
Is there a way to register service to request scope so all child scopes created from the same request scope would have the...
Read more >4.4 Bean scopes - Spring
Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its...
Read more >How do I work with per-request lifetime scope?
Option 1: Change your InstancePerRequest() registrations to be InstancePerLifetimeScope() . Most applications don't create their own nested unit-of-work ...
Read more >Dependency injection guidelines - .NET | Microsoft Learn
Use IServiceScopeFactory.CreateScope to create a new IServiceScope. Use the scope's IServiceProvider to get required services.
Read more >A Deep Dive Into the NestJS Injection Scope
There are three modes to define the scope. We can either define the scope properties on the service level or module level. It...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Oh thanks @iagomelanias, fixed!
Thanks for reporting. It should be fixed in
@nestjs/graphql@6.0.4
😃