Skip to content

Commit

Permalink
Update apollo client so it can be used in ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorus-nathaniel committed Jan 17, 2023
1 parent 694e7b0 commit 538ddbf
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/graphql/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import config from 'src/config'

const { enableGraphQl, graphqlUrl } = config

export let apolloClient: ApolloClient<NormalizedCacheObject>
let apolloClient: ApolloClient<NormalizedCacheObject>

export function getApolloClient() {
return apolloClient || initializeApollo()
}

const createApolloClient = (graphqlUrl: string): ApolloClient<NormalizedCacheObject> => {
return new ApolloClient({
Expand Down
4 changes: 2 additions & 2 deletions src/rtk/features/posts/postsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FindPostsQuery } from '@subsocial/api/filters'
import { getFirstOrUndefined } from '@subsocial/utils'
import { getPostsData } from 'src/graphql/apis'
import { PostFragmentMapped, PostFragmentWithParent } from 'src/graphql/apis/types'
import { apolloClient } from 'src/graphql/client'
import { getApolloClient } from 'src/graphql/client'
import {
createFetchOne,
createSelectUnknownIds,
Expand Down Expand Up @@ -195,7 +195,7 @@ const getPosts = createFetchDataFn<PostState[]>([])({
},
squid: async ({ ids, publicOnly }: { ids: string[]; publicOnly?: boolean }) => {
if (ids.length === 0) return []
const posts = await getPostsData(apolloClient, {
const posts = await getPostsData(getApolloClient(), {
where: {
id_in: ids,
hidden_not_eq: publicOnly ? true : undefined,
Expand Down
4 changes: 2 additions & 2 deletions src/rtk/features/profiles/profilesSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SubsocialApi } from '@subsocial/api'
import { bnsToIds, toSubsocialAddress } from '@subsocial/utils'
import { getProfilesData } from 'src/graphql/apis'
import { ProfileFragmentMapped } from 'src/graphql/apis/types'
import { apolloClient } from 'src/graphql/client'
import { getApolloClient } from 'src/graphql/client'
import {
CommonVisibility,
createFetchOne,
Expand Down Expand Up @@ -77,7 +77,7 @@ const getProfiles = createFetchDataFn<ProfileSpaceIdByAccount[]>([])({
return spaceIdsByAccouts.filter(x => !!x.id)
},
squid: async ({ ids }: { ids: string[] }) => {
const profiles = await getProfilesData(apolloClient, { ids })
const profiles = await getProfilesData(getApolloClient(), { ids })
return profiles
},
})
Expand Down
8 changes: 6 additions & 2 deletions src/rtk/features/reactions/myPostReactionsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SubsocialApi } from '@subsocial/api'
import { getFirstOrUndefined, idToBn, isDef, isEmptyArray, isEmptyStr } from '@subsocial/utils'
import BN from 'bn.js'
import { getAddressPostsReaction } from 'src/graphql/apis'
import { apolloClient } from 'src/graphql/client'
import { getApolloClient } from 'src/graphql/client'
import {
decodePrependedIdWithAddress,
FetchManyArgsWithPrefetch,
Expand Down Expand Up @@ -117,7 +117,11 @@ const getMyReactionsByPostIds = createFetchDataFn<FetchManyResult>([])({
return Array.from(reactionByPostId.values())
},
squid: async ({ ids, myAddress }: { ids: string[]; myAddress: string }) => {
return getAddressPostsReaction(apolloClient, { address: myAddress, postIds: ids }, idSeparator)
return getAddressPostsReaction(
getApolloClient(),
{ address: myAddress, postIds: ids },
idSeparator,
)
},
})

Expand Down
4 changes: 2 additions & 2 deletions src/rtk/features/spaces/spacesSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { FindSpacesQuery } from '@subsocial/api/filters'
import { getFirstOrUndefined } from '@subsocial/utils'
import { getSpacesData } from 'src/graphql/apis'
import { SpaceFragmentMapped } from 'src/graphql/apis/types'
import { apolloClient } from 'src/graphql/client'
import { getApolloClient } from 'src/graphql/client'
import {
createFetchOne,
FetchManyArgsWithPrefetch,
Expand Down Expand Up @@ -145,7 +145,7 @@ const getSpaces = createFetchDataFn<SpaceState[]>([])({
return entities
},
squid: async ({ ids }: { ids: string[] }) => {
const spaces = await getSpacesData(apolloClient, { where: { id_in: ids } })
const spaces = await getSpacesData(getApolloClient(), { where: { id_in: ids } })
return spaces.map<SpaceState>(space => ({ ...space, isOverview: true }))
},
})
Expand Down

0 comments on commit 538ddbf

Please sign in to comment.