Forked from kensmash/gist:14c413b1574311e4486e556d0e075024
Last active
March 30, 2021 12:32
-
-
Save benawad/5c9e6a9bfaef4065629b98cf5bd7d42e to your computer and use it in GitHub Desktop.
graphql-yoga cors setup
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 } = 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
Thank you, I needed this!