Skip to content

middleware not running in production #823

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

Closed
chrisdrackett opened this issue May 7, 2020 · 11 comments · Fixed by #833
Closed

middleware not running in production #823

chrisdrackett opened this issue May 7, 2020 · 11 comments · Fixed by #833
Labels
type/bug Something is not working the way it should

Comments

@chrisdrackett
Copy link
Contributor

We've added a couple bits of middleware to our app. One for logging requests and one for capturing errors and sending them to sentry. Both of these work in development without issue, but as far as we can tell both are not running in production. Here is an example:

middleware/index.ts

import { schema, log } from 'nexus'

schema.middleware((config) => {
  return async (root, args, ctx, info, next) => {
    if (!root) {
      log.info('request', {
        type: config.parentTypeConfig.name,
        field: config.fieldConfig.name,
        args,
      })
    }

    return next(root, args, ctx, info)
  }
})

this is then just imported in app.ts

// import our middleware
import './middleware'

Are we doing something wrong here?

@chrisdrackett chrisdrackett added the type/bug Something is not working the way it should label May 7, 2020
@chrisdrackett
Copy link
Contributor Author

turns out if we move the above schema.middleware into our app.ts directly it works in production. I'm guessing that somehow on build it is getting ignored or removed? I wonder if there is a way to have nexus pay attention to a middleware folder in a similar way that it pays attention to the graphql folder?

@Weakky
Copy link
Collaborator

Weakky commented May 9, 2020

Hey @chrisdrackett, thanks for reporting that issue!

This is weird and I cannot reproduce locally. Importing your middleware file into the entrypoint is indeed the way to go here. I tried reproducing your setup and it works.

Could you maybe share a reproduction repo, or make sure you're using the latest nexus@next ?

@Weakky Weakky added the needs/clarification Unable to answer question/feature without more info label May 9, 2020
@chrisdrackett
Copy link
Contributor Author

@Weakky sure, here is a test app:

https://github.com/chrisdrackett/nexus-middleware-test

Locally I see 8971 ● app request -- type: 'Query' field: 'worlds' args: {} when making a request, after deploy to Heroku I see nothing when doing the same.

@Weakky
Copy link
Collaborator

Weakky commented May 11, 2020

Ah, I see. So this is not technically a bug, although it sucks. The reason why your middleware isn't working in dev nor prod is because your middleware/index.ts file export * from './test', but your middleware/test.ts file doesn't export anything. Therefore, middleware/index.ts does not require middleware/test.ts.

This problem should be gone once #833 is merged. In the meantime, you can solve that problem by replacing export * from './test' to import './test'

@chrisdrackett
Copy link
Contributor Author

note that this does work in dev, just not production. Thanks for the info!

@Weakky
Copy link
Collaborator

Weakky commented May 11, 2020

Weird, I made the suggested change on your repro and it works even in the build output.

image

Are you maybe using different compiler options?

@chrisdrackett
Copy link
Contributor Author

Sorry, the suggested change works for us! I was just noting that before the change things worked locally for us :)

@jasonkuhrt jasonkuhrt removed the needs/clarification Unable to answer question/feature without more info label May 18, 2020
@jasonkuhrt
Copy link
Member

jasonkuhrt commented May 18, 2020

Was this issue related to

🤔

@Corjen
Copy link

Corjen commented Jun 10, 2020

Hi @chrisdrackett, what does the middleware from Sentry look like? Can you share a snippet?

@chrisdrackett
Copy link
Contributor Author

this is what we currently have:

errorMiddleware.ts

import { schema, log } from 'nexus'

import { Sentry } from '../sentry'

schema.middleware((config) => {
  return async (root, args, context, info, next) => {
    try {
      const result = await next(root, args, context, info)

      return result
    } catch (error) {
      Sentry.withScope((scope) => {
        scope.setExtra('type', config.parentTypeConfig.name)
        scope.setExtra('field', config.fieldConfig.name)
        scope.setExtra('args', args)
        scope.setExtra('headers', context.request.headers)
        scope.setTag('api-endpoint', `${process.env.API_ENDPOINT}`)
        Sentry.captureException(error)
      })

      log.error('error', { error })
    }
  }
})

sentry.ts

import * as Sentry from '@sentry/node'

// @ts-ignore
Sentry.init({
  dsn: 'dsn here',
  environment: process.env.APP_ENVIRONMENT,
  release: process.env.GIT_SHA,
  enabled: process.env.APP_ENVIRONMENT !== 'development',
})

export { Sentry }

@Corjen
Copy link

Corjen commented Jun 10, 2020

That’s quick😃 Thanks so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something is not working the way it should
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants