Skip to content

Instantly share code, notes, and snippets.

@rajeshkrishnakumar
Last active November 14, 2021 05:30
Show Gist options
  • Save rajeshkrishnakumar/653300e2225748b7de4d137b1d56b2ef to your computer and use it in GitHub Desktop.
Save rajeshkrishnakumar/653300e2225748b7de4d137b1d56b2ef to your computer and use it in GitHub Desktop.
graphql-route.js
import express from "express";
import graphqlHTTP from "express-graphql";
import { expressPlayground } from "graphql-playground-middleware";
import { applyMiddleware } from "graphql-middleware";
//import file of the above code snippet
import graphqlMiddleware from "../utils/middlewares/graphqlMiddleware.js";
import Schema from "../schema/index.js";
// Apply the middleware
const schemaWithMiddleware = applyMiddleware(Schema, graphqlMiddleware);
const router = express.Router();
router.use(
"/graphql",
graphqlHTTP((req) => ({
context: req.headers.context ? req.headers.context : "{}",
schema: schemaWithMiddleware, -> Registering the SchemaWithMiddlware
rootValue: global,
graphiql: process.env.NODE_ENV !== "production",
formatError(err) {
return {
code: false,
message:
"Oh no! Looks like we're facing an issue. :( Please try again in a few moments!",
exactMessage: err.message,
};
},
}))
);
export default router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment