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

Accessing graphql context from loader #28

Open
andreialecu opened this issue May 27, 2020 · 6 comments
Open

Accessing graphql context from loader #28

andreialecu opened this issue May 27, 2020 · 6 comments

Comments

@andreialecu
Copy link

andreialecu commented May 27, 2020

In loaders it is usually necessary to access the graphql execution context.

As per NestJS documentation I attempted to do it like this:

import { CONTEXT } from "@nestjs/graphql";

@Injectable({ scope: Scope.REQUEST })
export class CanEditMatchdayLoader implements NestDataLoader<string, boolean> {
  constructor(
    private readonly matchdayService: MatchdayService,
    @Inject(CONTEXT) private context: any,
  ) {
    console.log(context); // prints undefined
  }

  generateDataLoader(): DataLoader<string, boolean> {
     ...
  }
}

But context appears to be undefined no matter what I do. I assume this is caused by nestjs-dataloader somehow, because the nestjs documentation specifically mentions this use case.

Alternatively, this would also seem to be fixed by #11

@some-user123
Copy link

I also require this functionality and would love to see the PR merged.

It seems there is not too much activity in the repo lately 😢

Any ideas for alternatives?

@krislefeber
Copy link
Owner

krislefeber commented Mar 1, 2021 via email

@dimatillck
Copy link

for everyone who still wants to fix it: you need to call this.moduleRef.registerRequestByContextId(ctx, contextId); before calling this.moduleRef.resolve().

https://docs.nestjs.com/fundamentals/module-ref#registering-request-provider

@kommander
Copy link

It's undefined in both constructor and generateDataLoader. How can we create a DataLoader based on the context? This seems like a common use case.

@bneigher
Copy link

+1 looking to use values decoded from request headers into the actual data loader service call

@bneigher
Copy link

looks like this PR does the trick.. any way we can get this merged in?
#11

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

No branches or pull requests

6 participants