stacker.news/lib/apollo.js

242 lines
7.4 KiB
JavaScript
Raw Normal View History

import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client'
import { decodeCursor, LIMIT } from './cursor'
// import { RetryLink } from '@apollo/client/link/retry'
2021-09-07 15:22:57 -05:00
// const additiveLink = from([
// new RetryLink(),
// new HttpLink({ uri: '/api/graphql' })
// ])
2021-10-26 15:49:37 -05:00
function isFirstPage (cursor, existingThings) {
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 10:46:58 -05:00
// or equal to a full page
2021-10-26 15:49:37 -05:00
return existingThings?.length < LIMIT
}
}
2021-09-30 10:46:58 -05:00
export default function getApolloClient () {
2022-11-24 13:22:58 -06:00
if (typeof window === 'undefined') {
const client = getClient(`${process.env.SELF_URL}/api/graphql`)
client.clearStore()
return client
} else {
global.apolloClient ||= getClient('/api/graphql')
return global.apolloClient
}
}
function getClient (uri) {
return new ApolloClient({
link: new HttpLink({ uri }),
2021-09-30 10:46:58 -05:00
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
2021-12-16 18:39:19 -06:00
topUsers: {
2022-10-25 16:35:32 -05:00
keyArgs: ['when', 'sort'],
2021-12-16 18:39:19 -06:00
merge (existing, incoming) {
if (isFirstPage(incoming.cursor, existing?.users)) {
return incoming
}
return {
cursor: incoming.cursor,
users: [...(existing?.users || []), ...incoming.users]
}
}
},
2023-02-09 12:41:28 -06: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 11:23:43 -06:00
items: {
2022-12-01 16:22:13 -06:00
keyArgs: ['sub', 'sort', 'type', 'name', 'within'],
2021-09-30 10:46:58 -05:00
merge (existing, incoming) {
2021-10-26 15:49:37 -05:00
if (isFirstPage(incoming.cursor, existing?.items)) {
2021-09-30 10:46:58 -05:00
return incoming
}
2021-09-30 10:46:58 -05:00
return {
cursor: incoming.cursor,
2022-01-07 10:32:31 -06:00
items: [...(existing?.items || []), ...incoming.items],
2022-01-30 11:43:22 -06:00
pins: existing?.pins || null
2021-09-30 10:46:58 -05:00
}
}
2021-09-30 10:46:58 -05:00
},
2022-10-25 16:35:32 -05:00
topItems: {
keyArgs: ['sort', 'when'],
merge (existing, incoming) {
if (isFirstPage(incoming.cursor, existing?.items)) {
return incoming
}
return {
cursor: incoming.cursor,
items: [...(existing?.items || []), ...incoming.items],
pins: existing?.pins || null
}
}
},
topComments: {
keyArgs: ['sort', 'when'],
merge (existing, incoming) {
if (isFirstPage(incoming.cursor, existing?.comments)) {
return incoming
}
return {
cursor: incoming.cursor,
comments: [...(existing?.comments || []), ...incoming.comments]
}
}
},
2022-10-26 17:46:01 -05:00
related: {
keyArgs: ['id', 'title', 'limit'],
merge (existing, incoming) {
if (isFirstPage(incoming.cursor, existing?.items)) {
return incoming
}
return {
cursor: incoming.cursor,
items: [...(existing?.items || []), ...incoming.items]
}
}
},
2022-09-22 13:44:50 -05:00
outlawedItems: {
keyArgs: [],
merge (existing, incoming) {
if (isFirstPage(incoming.cursor, existing?.items)) {
return incoming
}
return {
cursor: incoming.cursor,
items: [...(existing?.items || []), ...incoming.items]
}
}
},
2022-09-22 15:42:04 -05:00
borderlandItems: {
keyArgs: [],
merge (existing, incoming) {
if (isFirstPage(incoming.cursor, existing?.items)) {
return incoming
}
return {
cursor: incoming.cursor,
items: [...(existing?.items || []), ...incoming.items]
}
}
},
2022-10-03 16:05:06 -05:00
freebieItems: {
keyArgs: [],
merge (existing, incoming) {
if (isFirstPage(incoming.cursor, existing?.items)) {
return incoming
}
return {
cursor: incoming.cursor,
items: [...(existing?.items || []), ...incoming.items]
}
}
},
2022-01-27 13:18:48 -06:00
search: {
2022-10-20 17:44:44 -05:00
keyArgs: ['q', 'sub', 'sort', 'what', 'when'],
2022-01-27 13:18:48 -06:00
merge (existing, incoming) {
if (isFirstPage(incoming.cursor, existing?.items)) {
return incoming
}
return {
cursor: incoming.cursor,
items: [...(existing?.items || []), ...incoming.items]
}
}
},
2021-09-30 10:46:58 -05:00
moreFlatComments: {
2021-12-16 17:05:31 -06:00
keyArgs: ['name', 'sort', 'within'],
2021-09-30 10:46:58 -05:00
merge (existing, incoming) {
2021-10-26 15:49:37 -05:00
if (isFirstPage(incoming.cursor, existing?.comments)) {
2021-09-30 10:46:58 -05:00
return incoming
}
2021-09-30 10:46:58 -05:00
return {
cursor: incoming.cursor,
comments: [...(existing?.comments || []), ...incoming.comments]
}
}
2021-09-30 10:46:58 -05:00
},
moreBookmarks: {
keyArgs: ['name'],
merge (existing, incoming) {
if (isFirstPage(incoming.cursor, existing?.items)) {
return incoming
}
return {
cursor: incoming.cursor,
items: [...(existing?.items || []), ...incoming.items]
}
}
},
2021-09-30 10:46:58 -05:00
notifications: {
keyArgs: ['inc'],
2021-09-30 10:46:58 -05:00
merge (existing, incoming) {
2021-10-26 15:49:37 -05:00
if (isFirstPage(incoming.cursor, existing?.notifications)) {
2021-09-30 10:46:58 -05:00
return incoming
}
2021-09-30 10:46:58 -05:00
return {
cursor: incoming.cursor,
2022-04-24 11:13:07 -05:00
earn: existing?.earn || incoming.earn,
2021-09-30 10:46:58 -05:00
notifications: [...(existing?.notifications || []), ...incoming.notifications],
lastChecked: incoming.lastChecked
}
}
2021-12-15 10:50:11 -06:00
},
walletHistory: {
2021-12-16 11:27:12 -06:00
keyArgs: ['inc'],
2021-12-15 10:50:11 -06:00
merge (existing, incoming) {
if (isFirstPage(incoming.cursor, existing?.facts)) {
return incoming
}
return {
cursor: incoming.cursor,
facts: [...(existing?.facts || []), ...incoming.facts]
}
}
}
}
}
}
2021-09-30 10:46:58 -05:00
}),
defaultOptions: {
// cache-and-network allows us to refresh pages on navigation
watchQuery: {
2021-10-26 15:49:37 -05:00
fetchPolicy: 'cache-only',
2021-09-30 10:46:58 -05:00
nextFetchPolicy: 'cache-first'
},
query: {
2021-10-26 15:49:37 -05:00
fetchPolicy: 'cache-only',
2021-09-30 10:46:58 -05:00
nextFetchPolicy: 'cache-first'
}
}
2021-09-30 10:46:58 -05:00
})
}