redwood: [Bug]: GraphiQL headers file not gitignored by default, and causes crash if you do ignore it

What’s not working?

As per the documentation:

A generateGraphiQLHeader file will be created in your api/lib folder and included in your gitignore. You can edit this file to customize your header. The function in the file is passed into your createGraphQLHandler and only called in dev.

The file is not automatically added to gitignore, but if you manually add it, because the generateGraphiQLHeader file is imported by api/functions/graphql, it causes a crash on CI deployment because it’s trying to import a file that doesn’t exist.

How do we reproduce the bug?

  • yarn rw setup graphiql dbAuth --id <user id>
  • Add the generateGraphiQLHeader file to gitignore
  • Try to deploy through CI/re-clone and try to run the app

Are you interested in working on this?

  • I’m interested in working on this

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 2
  • Comments: 18 (10 by maintainers)

Most upvoted comments

Hi, @alicelovescake @dthyresson,

I’d like to share what I did and think it might be a workaround. This is specific to Netlify though.

In graphql.ts, we can do something like this

let generateGraphiQLHeader = undefined

if (process.env.NODE_ENV === 'development') {
  try {
    const module = require('api/dist/lib/generateGraphiQLHeader')
    generateGraphiQLHeader = module.default
  } catch (err) {
    console.log('Could not find generateGraphiQLHeader')
  }
}

export const handler = createGraphQLHandler({
  getCurrentUser,
  generateGraphiQLHeader,
  loggerConfig: { logger, options: {} },
  directives,
  sdls,
  services,
  onException: () => {
    // Disconnect from your database with an unhandled exception.
    db.$disconnect()
  },
})

And also in netlify.toml. I added this to tell Netlify not to look for a specific inline dependency (in this case the non exisitance file)

[functions]
  external_node_modules = ["api/dist/lib/generateGraphiQLHeader"]

It works on both local and Netlify builds. So I guess I don’t have to manually remove generateGraphiQLHeader now.

Thanks

ps: perhaps the try-catch is unnecessary.

@alicelovescake Let’s talk about improving the deploy experience seen here. Thanks!

I had to manually remove the occurrences of generateGraphiQLHeader before pushing to remote. This seems to be a workaround.

I received some context on Discord.

I believe the issue happens if you add the generateGraphiQLHeader in the graphql function and deploy, say to Netlify.

Because the file is ignored and the function needs it, builds can fail.

Perhaps a pattern like:

// Ensures that production builds do not include the error page
let RedwoodDevFatalErrorPage = undefined
if (process.env.NODE_ENV === 'development') {
  RedwoodDevFatalErrorPage =
    require('@redwoodjs/web/dist/components/DevFatalErrorPage').DevFatalErrorPage
}

can be used.