2022-11-07 23:31:29 +00:00
|
|
|
import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client'
|
2021-09-09 16:44:01 +00:00
|
|
|
import { decodeCursor, LIMIT } from './cursor'
|
2021-09-06 22:36:08 +00:00
|
|
|
|
2021-10-26 20:49:37 +00:00
|
|
|
function isFirstPage (cursor, existingThings) {
|
2021-09-09 16:44:01 +00:00
|
|
|
if (cursor) {
|
|
|
|
const decursor = decodeCursor(cursor)
|
|
|
|
return decursor.offset === LIMIT
|
|
|
|
} else {
|
|
|
|
// we don't have anything cached, or our existing items are less than
|
2021-09-30 15:46:58 +00:00
|
|
|
// or equal to a full page
|
2021-10-26 20:49:37 +00:00
|
|
|
return existingThings?.length < LIMIT
|
2021-09-09 16:44:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-30 15:46:58 +00:00
|
|
|
export default function getApolloClient () {
|
2022-11-24 19:22:58 +00:00
|
|
|
if (typeof window === 'undefined') {
|
2023-07-23 15:08:43 +00:00
|
|
|
return getClient(`${process.env.SELF_URL}/api/graphql`)
|
2022-11-24 19:22:58 +00:00
|
|
|
} else {
|
|
|
|
global.apolloClient ||= getClient('/api/graphql')
|
|
|
|
return global.apolloClient
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getClient (uri) {
|
|
|
|
return new ApolloClient({
|
|
|
|
link: new HttpLink({ uri }),
|
2023-07-23 15:08:43 +00:00
|
|
|
ssrMode: typeof window === 'undefined',
|
2021-09-30 15:46:58 +00:00
|
|
|
cache: new InMemoryCache({
|
2023-07-23 15:08:43 +00:00
|
|
|
freezeResults: true,
|
2021-09-30 15:46:58 +00:00
|
|
|
typePolicies: {
|
|
|
|
Query: {
|
|
|
|
fields: {
|
2021-12-17 00:39:19 +00:00
|
|
|
topUsers: {
|
2023-07-23 15:08:43 +00:00
|
|
|
keyArgs: ['when', 'by'],
|
2021-12-17 00:39:19 +00:00
|
|
|
merge (existing, incoming) {
|
|
|
|
if (isFirstPage(incoming.cursor, existing?.users)) {
|
|
|
|
return incoming
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
cursor: incoming.cursor,
|
|
|
|
users: [...(existing?.users || []), ...incoming.users]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-02-09 18:41:28 +00:00
|
|
|
topCowboys: {
|
|
|
|
keyArgs: [],
|
|
|
|
merge (existing, incoming) {
|
|
|
|
if (isFirstPage(incoming.cursor, existing?.users)) {
|
|
|
|
return incoming
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
cursor: incoming.cursor,
|
|
|
|
users: [...(existing?.users || []), ...incoming.users]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-02-17 17:23:43 +00:00
|
|
|
items: {
|
2023-07-23 15:08:43 +00:00
|
|
|
keyArgs: ['sub', 'sort', 'type', 'name', 'when', 'by'],
|
2022-10-25 21:35:32 +00:00
|
|
|
merge (existing, incoming) {
|
|
|
|
if (isFirstPage(incoming.cursor, existing?.items)) {
|
|
|
|
return incoming
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
cursor: incoming.cursor,
|
|
|
|
items: [...(existing?.items || []), ...incoming.items],
|
|
|
|
pins: existing?.pins || null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-07-23 15:08:43 +00:00
|
|
|
comments: {
|
|
|
|
keyArgs: ['id', 'sort'],
|
2022-10-25 21:35:32 +00:00
|
|
|
merge (existing, incoming) {
|
2023-07-23 15:08:43 +00:00
|
|
|
return incoming
|
2022-10-25 21:35:32 +00:00
|
|
|
}
|
|
|
|
},
|
2022-10-26 22:46:01 +00:00
|
|
|
related: {
|
2023-07-23 15:08:43 +00:00
|
|
|
keyArgs: ['id', 'title', 'minMatch', 'limit'],
|
2022-10-03 21:05:06 +00:00
|
|
|
merge (existing, incoming) {
|
|
|
|
if (isFirstPage(incoming.cursor, existing?.items)) {
|
|
|
|
return incoming
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
cursor: incoming.cursor,
|
|
|
|
items: [...(existing?.items || []), ...incoming.items]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-01-27 19:18:48 +00:00
|
|
|
search: {
|
2022-10-20 22:44:44 +00:00
|
|
|
keyArgs: ['q', 'sub', 'sort', 'what', 'when'],
|
2022-01-27 19:18:48 +00:00
|
|
|
merge (existing, incoming) {
|
|
|
|
if (isFirstPage(incoming.cursor, existing?.items)) {
|
|
|
|
return incoming
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
cursor: incoming.cursor,
|
|
|
|
items: [...(existing?.items || []), ...incoming.items]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-07-23 15:08:43 +00:00
|
|
|
searchUsers: {
|
|
|
|
keyArgs: ['q', 'limit', 'similarity'],
|
2023-02-16 22:23:59 +00:00
|
|
|
merge (existing, incoming) {
|
2023-07-23 15:08:43 +00:00
|
|
|
return [...(existing?.searchUsers || []), ...incoming]
|
2023-02-16 22:23:59 +00:00
|
|
|
}
|
|
|
|
},
|
2021-09-30 15:46:58 +00:00
|
|
|
notifications: {
|
2022-09-06 13:01:49 +00:00
|
|
|
keyArgs: ['inc'],
|
2021-09-30 15:46:58 +00:00
|
|
|
merge (existing, incoming) {
|
2021-10-26 20:49:37 +00:00
|
|
|
if (isFirstPage(incoming.cursor, existing?.notifications)) {
|
2021-09-30 15:46:58 +00:00
|
|
|
return incoming
|
|
|
|
}
|
2021-09-06 22:36:08 +00:00
|
|
|
|
2021-09-30 15:46:58 +00:00
|
|
|
return {
|
|
|
|
cursor: incoming.cursor,
|
2022-04-24 16:13:07 +00:00
|
|
|
earn: existing?.earn || incoming.earn,
|
2021-09-30 15:46:58 +00:00
|
|
|
notifications: [...(existing?.notifications || []), ...incoming.notifications],
|
2023-07-23 15:08:43 +00:00
|
|
|
lastChecked: existing?.lastChecked || incoming.lastChecked
|
2021-09-30 15:46:58 +00:00
|
|
|
}
|
2021-09-06 22:36:08 +00:00
|
|
|
}
|
2021-12-15 16:50:11 +00:00
|
|
|
},
|
|
|
|
walletHistory: {
|
2021-12-16 17:27:12 +00:00
|
|
|
keyArgs: ['inc'],
|
2021-12-15 16:50:11 +00:00
|
|
|
merge (existing, incoming) {
|
|
|
|
if (isFirstPage(incoming.cursor, existing?.facts)) {
|
|
|
|
return incoming
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
cursor: incoming.cursor,
|
|
|
|
facts: [...(existing?.facts || []), ...incoming.facts]
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 22:36:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 15:46:58 +00:00
|
|
|
}),
|
2023-07-23 15:08:43 +00:00
|
|
|
assumeImmutableResults: true,
|
2021-09-30 15:46:58 +00:00
|
|
|
defaultOptions: {
|
|
|
|
watchQuery: {
|
2023-07-23 15:08:43 +00:00
|
|
|
fetchPolicy: 'cache-first',
|
|
|
|
nextFetchPolicy: 'cache-first',
|
|
|
|
canonizeResults: true
|
2021-09-30 15:46:58 +00:00
|
|
|
},
|
|
|
|
query: {
|
2023-07-23 15:08:43 +00:00
|
|
|
fetchPolicy: 'cache-first',
|
|
|
|
nextFetchPolicy: 'cache-first',
|
|
|
|
canonizeResults: true
|
2021-09-30 15:46:58 +00:00
|
|
|
}
|
2021-09-06 22:36:08 +00:00
|
|
|
}
|
2021-09-30 15:46:58 +00:00
|
|
|
})
|
|
|
|
}
|