Skip to content

Instantly share code, notes, and snippets.

@mfellner
Created July 8, 2019 20:42
Show Gist options
  • Save mfellner/2119db3584023092d70118a8dabd146e to your computer and use it in GitHub Desktop.
Save mfellner/2119db3584023092d70118a8dabd146e to your computer and use it in GitHub Desktop.
Using Apollo Server in Next.js 9 with API route in pages/api/graphql.ts
import { ApolloServer, gql } from 'apollo-server-micro';
const typeDefs = gql`
type Query {
sayHello: String
}
`;
const resolvers = {
Query: {
sayHello(parent, args, context) {
return 'Hello World!';
}
}
};
const apolloServer = new ApolloServer({ typeDefs, resolvers });
export const config = {
api: {
bodyParser: false
}
};
export default apolloServer.createHandler({ path: '/api/graphql' });
@kheaganeckley
Copy link

Thank you!!!!!!!!!

@fromi
Copy link

fromi commented Nov 23, 2019

Thank You 👍

@rmacy
Copy link

rmacy commented Dec 26, 2019

you rock 🤘

@pnavarrc
Copy link

👏 thanks for sharing!

@wzulfikar
Copy link

Anyone can point me to documentation for export const config? The snippet won't work if I remove it. Since it's not being referenced anywhere in the snippet, I wonder where it's being used.

@fromi
Copy link

fromi commented Jan 12, 2020

Anyone can point me to documentation for export const config? The snippet won't work if I remove it. Since it's not being referenced anywhere in the snippet, I wonder where it's being used.

https://nextjs.org/docs/api-routes/api-middlewares#custom-config
It is used by next.js if declared, same as the default export of each API route.

@wzulfikar
Copy link

Nice. Thanks!

@hk86
Copy link

hk86 commented Jul 11, 2020

How would you use this in getStaticProps?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment