hasura / hasura/data-dictionary

Update docs and code with info about enable CORS requests

Open
#5 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
88
Forks
8
PR merge metrics
No merged PRs in 30d

Description

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:

```ts
// 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)
}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.