Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benawad/5c9e6a9bfaef4065629b98cf5bd7d42e to your computer and use it in GitHub Desktop.
Save benawad/5c9e6a9bfaef4065629b98cf5bd7d42e to your computer and use it in GitHub Desktop.
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}`)
);
@nicolasmendonca
Copy link

Thank you, I needed this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment