Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ yarn-error.log*

# editors/ide
.vscode
.idea
.idea

# build files
build
25 changes: 19 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import mongoose from "mongoose";
import morgan from "morgan";
import schema from "./src/schema.js";
import logger from "./config/winston.js";
import jsonwebtoken from "jsonwebtoken";
import permissions from "./src/permissions.js"
import { applyMiddleware } from "graphql-middleware";

// if server is started using `npm start`, it will use .dev.env file to populate variables.
if (process.env.ENV === "dev") dotenv.config({ path: ".dev.env" });
Expand All @@ -20,17 +23,30 @@ const options = {
mongoose
.connect(process.env.DB_URI, options)
.then(() => {
console.log("Database connected");
logger.info("Database connected");
})
.catch((err) => {
console.log(err);
logger.error(err);
});

// Mongoose models are provided in context.
const server = new ApolloServer({
schema,
schema: applyMiddleware(schema, permissions),
tracing: true,
logger,
context: ({ req }) => {
const { headers } = req;
if (headers.authorization) {
let token = headers.authorization.split(" ")[1];
try {
let user = jsonwebtoken.verify(token, "mIAb!r@5r@pA7ZK19x4Wl0Y83T5!@$De");
return { jwt: user };
} catch (e) {
return null;
}
}
return null;
},
plugins: [
{
requestDidStart() {
Expand All @@ -52,7 +68,4 @@ app.listen(5000, () => {
logger.info(
`Apollo server listening at http://localhost:5000${server.graphqlPath}`
);
console.log(
`Apollo server listening at http://localhost:5000${server.graphqlPath}`
);
});
Loading