Last active
October 4, 2021 12:10
-
-
Save ziedHamdi/167a14fa602eef377f31b41f61fe5a5b to your computer and use it in GitHub Desktop.
/server/middleware/graphqlSetupVisitor.js adds graphql to express
This file contains hidden or 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
| const graphQlServer = graphqlHTTP(async (req, res, graphQLParams) => { | |
| const infraToken = req.header('infraToken'); | |
| let user = null; | |
| if (infraToken) { | |
| try { | |
| var _jwt$verify; | |
| _logger.default.debug("infra token found "); | |
| user = (_jwt$verify = _jsonwebtoken.default.verify(infraToken, _other.TOKEN_SECRET)) === null || _jwt$verify === void 0 ? void 0 : _jwt$verify.data; //FIXME (for some reason I have to load the user from db) | |
| // load the last updated user version from db | |
| user = await _db.User.findById(user._id); | |
| } catch (err) { | |
| _logger.default.error(err); | |
| } | |
| } | |
| return { | |
| schema: GraphQlSchema, | |
| graphiql: process.env.NODE_ENV !== 'production', | |
| customFormatErrorFn: formatError, | |
| context: { | |
| user, | |
| sessionID: req.sessionID | |
| } | |
| }; | |
| }); | |
| function addGraphqlRoute(app, url) { | |
| app.use(url ? url : '/api/graphql', graphQlServer); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment