disable graphiql on production
Asked Answered
G

3

6

How can I disable graphiql on production but still able to access it on development?

With express-graphql we can do something like

app.use('/graphql', graphqlHTTP({
  schema: MySessionAwareGraphQLSchema,
  graphiql: process.env.NODE_ENV === 'development',
}));

With apollo server, my setup is

import {graphqlExpress, graphiqlExpress} from 'graphql-server-express'

const app = new Express()

app
  .all('/graphql', bodyParser.json())
  .all('/graphql', graphqlExpress({
      schema
  )
  .all('/graphiql', graphiqlExpress({
      endpointURL: 'http://localhost/graphql'
    })
  )

and I can't find a way to pass to NODE_ENV to enable/disable graphiql.

Gabriellegabrielli answered 23/7, 2017 at 12:4 Comment(0)
B
2

Do you mean to enable graphiql on development only and disable it on production. If so just exclude the /graphiql handler

if (process.env.NODE_ENV === 'development') {
  app.all(
    '/graphiql',
    graphiqlExpress({
      endpointURL: '/graphql',
    }),
  );
}
Brann answered 25/7, 2017 at 7:51 Comment(1)
Hey that looks one of the way. I thought we were able to set a boolean value to disable or enable it.Gabriellegabrielli
P
2

Here's what I have in a koa setup

export default () => (
  convert(graphqlHTTP((req, res, ctx) => ({
    schema: require('app/graphql/schema'),
    context: {
      ...ctx.app.context,
      ...ctx.state,
    },

    // Enable graphql for development environments only
    graphiql: config.environment === 'development',


    formatError: error => ({
      message: error.message,
      stack: error.stack,
      locations: error.locations,
    }),
  })))
)

Note graphiql: config.environment === 'development', from here you could pass a custom environment variable and start your app with it.

ENABLE_GRAPHIQL=true NODE_ENV=production npm start_my_server

Depending on how you manage your environment variables, you could change the expression to

graphiql: myEnv.ENABLE_GRAPHIQL || myEnv.environment === 'development', 

FWIW you should not be enabling graphiql in production

Proprietor answered 21/9, 2017 at 16:14 Comment(0)
P
0

When you start the app you need to define the environment variables. Do it using your package.json scripts.

"start:dev": "cross-env NODE_ENV=development **<your application start script>**"
"start:prod": "cross-env NODE_ENV=production **<your application start script>**"

If you develop on Windows install and use cross-env package to support environment variables.

Priscilapriscilla answered 10/10, 2022 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.