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'
|
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'
|
2022-11-07 23:31:29 +00:00
|
|
|
import { ME } from '../fragments/users'
|
2023-01-27 23:20:33 +00:00
|
|
|
import { PRICE } from '../fragments/price'
|
2023-09-12 15:56:02 +00:00
|
|
|
import { BLOCK_HEIGHT } from '../fragments/blockHeight'
|
2023-07-29 19:38:20 +00:00
|
|
|
import { getServerSession } from 'next-auth/next'
|
|
|
|
import { getAuthOptions } from '../pages/api/auth/[...nextauth]'
|
2021-04-14 23:56:29 +00:00
|
|
|
|
2023-07-29 19:38:20 +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({
|
2021-04-26 21:55:15 +00:00
|
|
|
ssrMode: true,
|
|
|
|
link: new SchemaLink({
|
2022-11-06 17:28:58 +00:00
|
|
|
schema: makeExecutableSchema({
|
|
|
|
typeDefs,
|
|
|
|
resolvers
|
2021-04-26 21:55:15 +00:00
|
|
|
}),
|
|
|
|
context: {
|
2021-04-14 23:56:29 +00:00
|
|
|
models,
|
2022-04-21 22:50:02 +00:00
|
|
|
me: session
|
2022-05-02 01:01:33 +00:00
|
|
|
? session.user
|
2022-04-21 22:50:02 +00:00
|
|
|
: me,
|
2022-01-26 15:35:14 +00:00
|
|
|
lnd,
|
2023-08-17 00:48:45 +00:00
|
|
|
search
|
2021-04-14 23:56:29 +00:00
|
|
|
}
|
2021-04-26 21:55:15 +00:00
|
|
|
}),
|
2023-07-23 15:08:43 +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',
|
2023-07-23 15:08:43 +00:00
|
|
|
canonizeResults: true,
|
|
|
|
ssr: true
|
|
|
|
},
|
|
|
|
query: {
|
2023-08-05 19:46:29 +00:00
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
nextFetchPolicy: 'no-cache',
|
2023-07-23 15:08:43 +00:00
|
|
|
canonizeResults: true,
|
|
|
|
ssr: true
|
|
|
|
}
|
|
|
|
}
|
2021-04-26 21:55:15 +00:00
|
|
|
})
|
2022-11-24 19:22:58 +00:00
|
|
|
return client
|
2021-04-26 21:55:15 +00:00
|
|
|
}
|
2021-10-26 20:49:37 +00:00
|
|
|
|
2023-08-28 17:52:15 +00:00
|
|
|
/**
|
|
|
|
* Takes a query and variables and returns a getServerSideProps function
|
|
|
|
*
|
|
|
|
* @param opts Options
|
|
|
|
* @param opts.query graphql query or function that return graphql query
|
|
|
|
* @param opts.variables graphql variables or function that return graphql variables
|
|
|
|
* @param opts.notFound function that tests data to determine if 404
|
|
|
|
* @param opts.authRequired boolean that determines if auth is required
|
|
|
|
*/
|
2023-08-31 00:03:05 +00:00
|
|
|
export function getGetServerSideProps (
|
|
|
|
{ query: queryOrFunc, variables: varsOrFunc, notFound, authRequired }) {
|
2023-07-29 19:38:20 +00:00
|
|
|
return async function ({ req, res, query: params }) {
|
2022-09-06 13:01:49 +00:00
|
|
|
const { nodata, ...realParams } = params
|
2023-07-23 15:08:43 +00:00
|
|
|
// we want to use client-side cache
|
|
|
|
if (nodata) return { props: { } }
|
|
|
|
|
2023-08-31 00:03:05 +00:00
|
|
|
const variables = typeof varsOrFunc === 'function' ? varsOrFunc(realParams) : varsOrFunc
|
2022-09-06 13:01:49 +00:00
|
|
|
const vars = { ...realParams, ...variables }
|
2023-08-31 00:03:05 +00:00
|
|
|
const query = typeof queryOrFunc === 'function' ? queryOrFunc(vars) : queryOrFunc
|
2023-07-23 15:08:43 +00:00
|
|
|
|
2023-07-29 19:38:20 +00:00
|
|
|
const client = await getSSRApolloClient({ req, res })
|
2022-09-06 14:57:34 +00:00
|
|
|
|
2023-10-23 23:19:36 +00:00
|
|
|
const { data: { me } } = await client.query({ query: ME })
|
2022-09-06 14:57:34 +00:00
|
|
|
|
2023-08-28 17:52:15 +00:00
|
|
|
if (authRequired && !me) {
|
|
|
|
const callback = process.env.PUBLIC_URL + req.url
|
2022-01-27 20:15:18 +00:00
|
|
|
return {
|
2023-08-28 17:52:15 +00:00
|
|
|
redirect: {
|
|
|
|
destination: `/login?callbackUrl=${encodeURIComponent(callback)}`
|
|
|
|
}
|
2022-01-27 20:15:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 17:52:15 +00:00
|
|
|
const { data: { price } } = await client.query({
|
|
|
|
query: PRICE, variables: { fiatCurrency: me?.fiatCurrency }
|
|
|
|
})
|
|
|
|
|
2023-09-12 15:56:02 +00:00
|
|
|
const { data: { blockHeight } } = await client.query({
|
|
|
|
query: BLOCK_HEIGHT, variables: {}
|
|
|
|
})
|
|
|
|
|
2022-04-21 22:50:02 +00:00
|
|
|
let error = null; let data = null; let props = {}
|
|
|
|
if (query) {
|
2023-08-30 15:00:47 +00:00
|
|
|
try {
|
|
|
|
({ error, data } = await client.query({
|
|
|
|
query,
|
|
|
|
variables: vars
|
|
|
|
}))
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e)
|
|
|
|
}
|
2021-10-26 20:49:37 +00:00
|
|
|
|
2023-08-28 17:52:15 +00:00
|
|
|
if (error || !data || (notFound && notFound(data, vars))) {
|
2022-04-21 22:50:02 +00:00
|
|
|
return {
|
|
|
|
notFound: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
props = {
|
|
|
|
apollo: {
|
|
|
|
query: print(query),
|
2022-09-06 13:01:49 +00:00
|
|
|
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,
|
2023-09-12 15:56:02 +00:00
|
|
|
blockHeight,
|
2023-07-23 15:08:43 +00:00
|
|
|
ssrData: data
|
2021-10-26 20:49:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|