Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
graphql-yoga cors setup
const { GraphQLServer } = require("graphql-yoga");
const session = require("express-session");
const { Prisma } = require("prisma-binding");
const resolvers = require("./resolvers");
const db = new Prisma({
typeDefs: "src/generated/prisma.graphql", // the auto-generated GraphQL schema of the Prisma API
endpoint: process.env.PRISMA_ENDPOINT, // the endpoint of the Prisma API (value set in `.env`)
debug: true // log all GraphQL queries & mutations sent to the Prisma API
// secret: process.env.PRISMA_SECRET, // only needed if specified in `database/prisma.yml` (value set in `.env`)
});
const server = new GraphQLServer({
typeDefs: "./src/schema.graphql",
resolvers,
context: req => ({ req: req.request, db })
});
const opts = {
port: 4000,
cors: {
credentials: true,
origin: ["http://localhost:3000"] // your frontend url.
}
};
const SESSION_SECRET = "lsdfjlkjlkewaqra";
server.express.use(
session({
name: "qid",
secret: SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
maxAge: 1000 * 60 * 60 * 24 * 7 // 7 days
}
})
);
server.start(opts =>
console.log(`Server is running on http://localhost:${opts.port}`)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment