Skip to content

Dataloader executing in HttpRequest with empty context in NestJSv7 #22

@dantehemerson

Description

@dantehemerson

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions