stacker.news/api/ssrApollo.js

127 lines
3.2 KiB
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'
2023-07-24 22:50:12 +00:00
import { makeExecutableSchema } from '@graphql-tools/schema'
2021-04-14 23:56:29 +00:00
import resolvers from './resolvers'
import typeDefs from './typeDefs'
import models from './models'
2023-01-18 18:49:20 +00:00
import slashtags from './slashtags'
2021-10-26 20:49:37 +00:00
import { print } from 'graphql'
2021-10-28 19:59:53 +00:00
import lnd from './lnd'
2022-01-26 15:35:14 +00:00
import search from './search'
import { ME } from '../fragments/users'
import { PRICE } from '../fragments/price'
import { getServerSession } from 'next-auth/next'
import { getAuthOptions } from '../pages/api/auth/[...nextauth]'
2021-04-14 23:56:29 +00:00
export default async function getSSRApolloClient ({ req, res, me = null }) {
const session = req && await getServerSession(req, res, getAuthOptions(req))
2022-11-24 19:22:58 +00:00
const client = new ApolloClient({
ssrMode: true,
link: new SchemaLink({
2022-11-06 17:28:58 +00:00
schema: makeExecutableSchema({
typeDefs,
resolvers
}),
context: {
2021-04-14 23:56:29 +00:00
models,
2022-04-21 22:50:02 +00:00
me: session
? session.user
2022-04-21 22:50:02 +00:00
: me,
2022-01-26 15:35:14 +00:00
lnd,
2023-01-18 18:49:20 +00:00
search,
slashtags
2021-04-14 23:56:29 +00:00
}
}),
cache: new InMemoryCache({
freezeResults: true
}),
assumeImmutableResults: true,
defaultOptions: {
watchQuery: {
2023-08-05 19:46:29 +00:00
fetchPolicy: 'no-cache',
nextFetchPolicy: 'no-cache',
canonizeResults: true,
ssr: true
},
query: {
2023-08-05 19:46:29 +00:00
fetchPolicy: 'no-cache',
nextFetchPolicy: 'no-cache',
canonizeResults: true,
ssr: true
}
}
})
2022-11-24 19:22:58 +00:00
return client
}
2021-10-26 20:49:37 +00:00
export function getGetServerSideProps (queryOrFunc, variablesOrFunc = null, notFoundFunc, requireVar) {
return async function ({ req, res, query: params }) {
const { nodata, ...realParams } = params
// we want to use client-side cache
if (nodata) return { props: { } }
const variables = typeof variablesOrFunc === 'function' ? variablesOrFunc(realParams) : variablesOrFunc
const vars = { ...realParams, ...variables }
const query = typeof queryOrFunc === 'function' ? queryOrFunc(vars) : queryOrFunc
const client = await getSSRApolloClient({ req, res })
2022-09-06 14:57:34 +00:00
const { data: { me } } = await client.query({
2023-05-07 15:44:57 +00:00
query: ME,
variables: { skipUpdate: true }
2022-09-06 14:57:34 +00:00
})
const { data: { price } } = await client.query({
query: PRICE, variables: { fiatCurrency: me?.fiatCurrency }
})
2022-01-27 20:15:18 +00:00
if (requireVar && !vars[requireVar]) {
return {
notFound: true
}
}
2022-04-21 22:50:02 +00:00
let error = null; let data = null; let props = {}
if (query) {
try {
({ error, data } = await client.query({
query,
variables: vars
}))
} catch (err) {
if (err.message === 'you must be logged in') {
const callback = process.env.PUBLIC_URL + req.url
return {
redirect: {
destination: `/login?callbackUrl=${encodeURIComponent(callback)}`
}
}
}
throw err
}
2021-10-26 20:49:37 +00:00
if (error || !data || (notFoundFunc && notFoundFunc(data, vars))) {
2022-04-21 22:50:02 +00:00
return {
notFound: true
}
}
props = {
apollo: {
query: print(query),
variables: vars
2022-04-21 22:50:02 +00:00
}
2021-10-26 20:49:37 +00:00
}
}
return {
props: {
2022-04-21 22:50:02 +00:00
...props,
2021-11-28 17:29:17 +00:00
me,
price,
ssrData: data
2021-10-26 20:49:37 +00:00
}
}
}
}