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

Dataloader executing in HttpRequest with empty context in NestJSv7 #22

Closed
dantehemerson opened this issue Apr 29, 2020 · 2 comments · Fixed by dantehemerson/nestjs-dataloader#1 · May be fixed by #26
Closed

Dataloader executing in HttpRequest with empty context in NestJSv7 #22

dantehemerson opened this issue Apr 29, 2020 · 2 comments · Fixed by dantehemerson/nestjs-dataloader#1 · May be fixed by #26

Comments

@dantehemerson
Copy link
Contributor

I migrated to new Nestjsv7
I'm using REST controller for auth and GraphQL for the other modules.
When I execute a request with REST API, it executes the DataloaderInterceptor and throws with this error:

2020-04-29T04:25:47.261Z]  INFO: HANGMAN-API/23932 on x: GraphQL Playground listening on port http://localhost:8087/graphql []
(node:23932) Warning: The route option `beforeHandler` has been deprecated, use `preHandler` instead
[2020-04-29T04:25:49.245Z] ERROR: HANGMAN-API/23932 on x: 
    Cannot read property 'NEST_LOADER_CONTEXT_KEY' of undefined TypeError: Cannot read property 'NEST_LOADER_CONTEXT_KEY' of undefined
        at DataLoaderInterceptor.intercept (/home/dantehemerson/Documents/repos/hangman-api/node_modules/nestjs-dataloader/dist/index.js:32:16)
        at /home/dantehemerson/Documents/repos/hangman-api/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:22:36
        at InterceptorsConsumer.intercept (/home/dantehemerson/Documents/repos/hangman-api/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:24:24)
        at /home/dantehemerson/Documents/repos/hangman-api/node_modules/@nestjs/core/router/router-execution-context.js:45:60
        at Object.<anonymous> (/home/dantehemerson/Documents/repos/hangman-api/node_modules/@nestjs/core/router/router-proxy.js:8:23)
        at preHandlerCallback (/home/dantehemerson/Documents/repos/hangman-api/node_modules/fastify/lib/handleRequest.js:111:30)
        at preValidationCallback (/home/dantehemerson/Documents/repos/hangman-api/node_modules/fastify/lib/handleRequest.js:100:5)
        at handler (/home/dantehemerson/Documents/repos/hangman-api/node_modules/fastify/lib/handleRequest.js:69:5)
        at handleRequest (/home/dantehemerson/Documents/repos/hangman-api/node_modules/fastify/lib/handleRequest.js:30:9)
        at onRunMiddlewares (/home/dantehemerson/Documents/repos/hangman-api/node_modules/fastify/lib/middleware.js:22:5)
        at Holder.done (/home/dantehemerson/Documents/repos/hangman-api/node_modules/middie/middie.js:90:9)
        at SessionStrategy.strategy.pass (/home/dantehemerson/Documents/repos/hangman-api/node_modules/passport/lib/middleware/authenticate.js:343:9)
        at SessionStrategy.authenticate (/home/dantehemerson/Documents/repos/hangman-api/node_modules/passport/lib/strategies/session.js:75:10)
        at attempt (/home/dantehemerson/Documents/repos/hangman-api/node_modules/passport/lib/middleware/authenticate.js:366:16)
        at authenticate (/home/dantehemerson/Documents/repos/hangman-api/node_modules/passport/lib/middleware/authenticate.js:367:7)
        at Holder.done (/home/dantehemerson/Documents/repos/hangman-api/node_modules/middie/middie.js:112:11)
        at initialize (/home/dantehemerson/Documents/repos/hangman-api/node_modules/passport/lib/middleware/initialize.js:53:5)
        at Holder.done (/home/dantehemerson/Documents/repos/hangman-api/node_modules/middie/middie.js:112:11)
        at internalNext (/home/dantehemerson/Documents/repos/hangman-api/node_modules/helmet/index.js:47:33)
        at xXssProtection (/home/dantehemerson/Documents/repos/hangman-api/node_modules/x-xss-protection/dist/index.js:47:13)
        at internalNext (/home/dantehemerson/Documents/repos/hangman-api/node_modules/helmet/index.js:51:7)
        at nosniff (/home/dantehemerson/Documents/repos/hangman-api/node_modules/dont-sniff-mimetype/dist/index.js:5:9)
[2020-04-29T04:25:49.246Z] ERROR: HANGMAN-API/23932 on x: Cannot read property 'NEST_LOADER_CONTEXT_KEY' of undefined

That is because here the context is undefined:

if (ctx[NEST_LOADER_CONTEXT_KEY] === undefined) {
ctx[NEST_LOADER_CONTEXT_KEY] = async (type: string) : Promise<NestDataLoader<any, any>> => {
if (ctx[type] === undefined) {

If I add extra validation like this

if (ctx && ctx[NEST_LOADER_CONTEXT_KEY] === undefined) {

this works. Could You help me with this error please.

Here is my repository for reproduction:
https://github.com/dantehemerson/hangman-api

@krislefeber
Copy link
Owner

Hi @dantehemerson, fixes have been released around the ctx in 7.0.1, please let me know if you are still running into issues. Thanks

@dantehemerson
Copy link
Contributor Author

@krislefeber not yet, it keeps breaking. But I was testing and this happens only when I use fastify adapter.

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