Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Durable providers does not work with Graphql resolvers #2452

Closed
2 of 4 tasks
KamalAman opened this issue Oct 16, 2022 · 2 comments
Closed
2 of 4 tasks

Durable providers does not work with Graphql resolvers #2452

KamalAman opened this issue Oct 16, 2022 · 2 comments

Comments

@KamalAman
Copy link

KamalAman commented Oct 16, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When trying to use a durable provider, the ContextIdFactory never gets call when using a Graphql resolver

Minimum reproduction code

https://stackblitz.com/edit/nestjs-typescript-starter-wrfmxw?file=src%2Fdurable.module.ts,package.json,src%2Fcontext-id-strategy.ts,src%2Fmain.ts

Steps to reproduce

Using a basic module here, the REST endpoint works properly with the durable service however the Graphql Resolver never causes the ContextIdFactory documented here to executes, thus always creates a new service each call. Durable providers are documented here: durable-providers

import { Controller, Get } from '@nestjs/common';
import { Module } from '@nestjs/common';
import { Inject, Injectable, Scope } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Field, ObjectType, Resolver, Query } from '@nestjs/graphql';

@Injectable({ scope: Scope.REQUEST, durable: true })
export class DurableService {
  public instanceCounter = 0;

  constructor(@Inject(REQUEST) public readonly requestPayload: unknown) {}

  greeting() {
    ++this.instanceCounter;
    return `Hello world! Counter: ${this.instanceCounter}`;
  }
}

@Controller('durable')
export class DurableController {
  constructor(private readonly durableService: DurableService) {}

  @Get()
  greeting(): string {
    return this.durableService.greeting();
  }

  @Get('echo')
  echo() {
    return this.durableService.requestPayload;
  }
}

@ObjectType('Durable')
class DurableDto {
    @Field(() => String, { nullable: true })
    id: string
}

@Resolver(() => DurableDto)
export class DurableResolver {
  constructor(private readonly service: DurableService) {}

  @Query(() => DurableDto)
  async durable() {
    return {
        id: this.service.greeting()
    }
  }
}

@Module({
  controllers: [DurableController],
  providers: [DurableService, DurableResolver],
})
export class DurableModule {}

Expected behavior

instanceCounter should increment each time the durable resolver is called, however it stays at 1 because the service is recreated each request. The controller works just fine incrementing the counter.

Package version

10.1.3

Graphql version

graphql: 15.6.1

NestJS version

9.1.4

Node.js version

16.15.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@thiagomini
Copy link
Contributor

I am working on that. Hopefully, this will be a quick fix.

@KamalAman
Copy link
Author

KamalAman commented Nov 25, 2022

Caution for those out there, don't use Durable resolvers if you are also using DataLoader since DataLoaders must be Request scoped

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants