question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Service is created for every child type when using `Scope.REQUEST`

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kamilmysliwieccommented, Mar 31, 2019

Oh thanks @iagomelanias, fixed!

1reaction
kamilmysliwieccommented, Mar 28, 2019

Thanks for reporting. It should be fixed in @nestjs/graphql@6.0.4 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found