Last active
November 14, 2021 05:30
-
-
Save rajeshkrishnakumar/653300e2225748b7de4d137b1d56b2ef to your computer and use it in GitHub Desktop.
graphql-route.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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