Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update docs and code with info about enable CORS requests #5

Open
GavinRay97 opened this issue Dec 18, 2020 · 0 comments
Open

Update docs and code with info about enable CORS requests #5

GavinRay97 opened this issue Dec 18, 2020 · 0 comments

Comments

@GavinRay97
Copy link
Member

From conversation with someone trying to integrate the app, realized that the Next.js API doesn't have CORS enabled.

To fix this, /pages/api/graphql.ts needs to updated with:

// Make sure to "yarn add cors" or "npm i cors"
import CORS from 'cors'

const apolloServer = new ApolloServer({ typeDefs, resolvers })
const cors = CORS({ origin: "*" })function runMiddleware(req, res, fn) {
  return new Promise((resolve, reject) => {
    fn(req, res, (result) => {
      if (result instanceof Error) return reject(result)
      return resolve(result)
    })
  })
}

export default async function handler(req, res) {
  // Set CORS headers to allow all origins
  await runMiddleware(req, res, cors)
  // Create and run GQL handler
  const graphql = apolloServer.createHandler({ path: "/api/graphql" })
  return graphql(req, res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant