Skip to content

Instantly share code, notes, and snippets.

@devmnj
Created July 25, 2021 03:45
Show Gist options
  • Save devmnj/9ce06efffec89aa47bac962c7d11ef75 to your computer and use it in GitHub Desktop.
Save devmnj/9ce06efffec89aa47bac962c7d11ef75 to your computer and use it in GitHub Desktop.
Nextjs appolo graphql api - fix
//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 } };
@turex04
Copy link

turex04 commented Jul 26, 2021

For some reason, the cors error stil exists and i'm still unable to reach the server.

@devmnj
Copy link
Author

devmnj commented Jul 26, 2021

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