stacker.news/api/ssrApollo.js

26 lines
715 B
JavaScript
Raw Normal View History

2021-04-14 23:56:29 +00:00
import { ApolloClient, InMemoryCache } from '@apollo/client'
import { SchemaLink } from '@apollo/client/link/schema'
import { mergeSchemas } from 'graphql-tools'
import { getSession } from 'next-auth/client'
import resolvers from './resolvers'
import typeDefs from './typeDefs'
import models from './models'
2021-09-30 15:46:58 +00:00
export default async function getSSRApolloClient (req) {
2021-07-08 00:15:27 +00:00
const session = req && await getSession({ req })
return new ApolloClient({
ssrMode: true,
link: new SchemaLink({
schema: mergeSchemas({
schemas: typeDefs,
resolvers: resolvers
}),
context: {
2021-04-14 23:56:29 +00:00
models,
me: session ? session.user : null
}
}),
cache: new InMemoryCache()
})
}