1.1.5 • Published 2 years ago
nextjs-apollo-server v1.1.5
nextjs-apollo-server
A small package to convert api routes in graphql using apollo server
Install
npm i nextjs-apollo-server@latest
Usage in Api routes
- params
config
is same tonew ApolloServer({ ...config })
- you can change
path
, by default is/api/graphql
- this package only added CORS to api route
- export config is needed
// /api/graphql
import { apolloServer, type ApolloServerConfig } from 'nextjs-apollo-server'
import { type PageConfig } from 'next'
const { getServer } = apolloServer({
config: {
typeDefs: `
type Query {
hello: String
}
`,
resolvers: {
Query: {
hello: () => 'Hello world!',
},
},
},
})
// https://nextjs.org/docs/api-routes/api-middlewares#custom-config
export const config: PageConfig = {
api: {
bodyParser: false,
},
}
export default getServer
Usage in client
import { ApolloClient, InMemoryCache } from '@apollo/client'
export const client = new ApolloClient({
uri: '/api/graphql', // your path from apollo server
cache: new InMemoryCache(),
})