Created
July 25, 2021 03:45
-
-
Save devmnj/9ce06efffec89aa47bac962c7d11ef75 to your computer and use it in GitHub Desktop.
Nextjs appolo graphql api - fix
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
//apply this fix where regular method fails | |
import { gql, ApolloServer } from "apollo-server-micro"; | |
import { PrismaClient } from "@prisma/client"; | |
import { | |
ApolloServerPluginLandingPageGraphQLPlayground, | |
ApolloServerPluginLandingPageDisabled, | |
} from "apollo-server-core"; | |
const prisma = new PrismaClient(); | |
const typeDefs = gql` | |
type Contact { | |
id: String | |
name: String | |
email: String | |
} | |
type Query { | |
contacts: [Contact] | |
} | |
`; | |
const resolvers = { | |
Query: { | |
contacts(_parent, _args, _context) { | |
return prisma.contact.findMany(); | |
}, | |
}, | |
}; | |
let apolloServerHandler =(req, res) => Promise | |
async function startServer(req, res) { | |
const server = new ApolloServer({ | |
typeDefs, | |
resolvers, | |
plugins: [ | |
ApolloServerPluginLandingPageGraphQLPlayground({ | |
// options | |
}), | |
], | |
}) ; | |
await server.start(); | |
apolloServerHandler= server.createHandler({path:'/api/graphql'}); | |
return apolloServerHandler; | |
} | |
export default async (req,res)=>{ | |
const apolloServerHandler = await startServer(); | |
return apolloServerHandler(req,res) | |
} | |
export const config = { api: { bodyParser: false } }; |
but for me , this is working.
try clear browser cookies and run again
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For some reason, the cors error stil exists and i'm still unable to reach the server.