-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: Adds collections & uploads Apollo to version 3 #455
Merged
Merged
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9ef1f54
Adds collections
kierangillen 6f7b5d0
Adds collection rail
kierangillen 57e6e5b
Removes logs
kierangillen 19ba235
Creates collection scene
kierangillen 48b5158
Upgrades Apollo to @apollo/client
kierangillen 17926e1
Updates more apollo configs
kierangillen 0e7ba01
Merge branch 'master' of https://github.com/seasons/harvest into feat…
kierangillen 5f0fded
Fixes fitPicConfirmation
kierangillen c386433
Fixes localBagItems
kierangillen 9c8a753
Updates useQuery with previousData
kierangillen 51fe82c
Updates confirm photo styles
kierangillen 5e2ed7d
Uses slug instead of ID
kierangillen a74fbae
Merge branch 'master' of https://github.com/seasons/harvest into feat…
kierangillen 7cc31f6
Updates apollo
kierangillen 8df9fa5
Merge branch 'master' of https://github.com/seasons/harvest into feat…
kierangillen bcd307c
Updates to use productConnection
kierangillen 3224b24
Merge branch 'master' of https://github.com/seasons/harvest into feat…
kierangillen 5666e37
Updates yarn lock
kierangillen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,51 @@ | ||
import { InMemoryCache, IntrospectionFragmentMatcher } from "apollo-cache-inmemory" | ||
import { persistCache } from "apollo-cache-persist" | ||
import { ApolloClient } from "apollo-client" | ||
import { ApolloLink, Observable } from "apollo-link" | ||
import { ApolloLink, Observable, ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" | ||
import { setContext } from "apollo-link-context" | ||
import { onError } from "apollo-link-error" | ||
import { createUploadLink } from "apollo-upload-client" | ||
import { getAccessTokenFromSession, getNewToken } from "App/utils/auth" | ||
import { config, Env } from "App/utils/config" | ||
import { Platform } from "react-native" | ||
import unfetch from "unfetch" | ||
|
||
import AsyncStorage from "@react-native-community/async-storage" | ||
import * as Sentry from "@sentry/react-native" | ||
|
||
import introspectionQueryResultData from "../fragmentTypes.json" | ||
import { resolvers, typeDefs } from "./resolvers" | ||
import { isEmpty } from "lodash" | ||
|
||
export const setupApolloClient = async () => { | ||
const fragmentMatcher = new IntrospectionFragmentMatcher({ | ||
introspectionQueryResultData, | ||
const cache = new InMemoryCache({ | ||
typePolicies: { | ||
Query: { | ||
fields: { | ||
fitPicsCount: { | ||
read(newCount) { | ||
return newCount | ||
}, | ||
}, | ||
fitPics: { | ||
merge(existing = [], incoming = [], { args: { skipFitPics = 0 } }) { | ||
const merged = existing ? existing.slice(0) : [] | ||
for (let i = 0; i < incoming.length; ++i) { | ||
merged[skipFitPics + i] = incoming[i] | ||
} | ||
existing = merged | ||
return existing | ||
}, | ||
}, | ||
productsConnection: { | ||
merge(existing = {}, incoming = {}, { args: { skip = 0 } }) { | ||
let mergedEdges = !isEmpty(existing) ? existing?.edges?.slice(0) : [] | ||
if (incoming?.edges?.length > 0) { | ||
for (let i = 0; i < incoming?.edges?.length; ++i) { | ||
mergedEdges[skip + i] = incoming?.edges?.[i] | ||
} | ||
} | ||
return { ...existing, ...incoming, edges: mergedEdges } | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
const cache = new InMemoryCache({ fragmentMatcher }) | ||
|
||
const link = createUploadLink({ | ||
uri: config.get(Env.MONSOON_ENDPOINT) || "http://localhost:4000/", | ||
// FIXME: unfetch here is being used for this fix https://github.com/jhen0409/react-native-debugger/issues/432 | ||
fetch: unfetch, | ||
}) | ||
const httpLink = new HttpLink({ | ||
uri: process.env.MONSOON_ENDPOINT || "http://localhost:4000/", // Server URL (must be absolute) | ||
}) as any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should figure out the proper types for this |
||
|
||
const authLink = setContext(async (_, { headers: oldHeaders }) => { | ||
const headers = { ...oldHeaders, application: "harvest", platform: Platform.OS } | ||
|
@@ -49,6 +67,7 @@ export const setupApolloClient = async () => { | |
// return the headers to the context so createUploadLink can read them | ||
}) | ||
|
||
// @ts-ignore | ||
const errorLink = onError(({ graphQLErrors, networkError, operation, forward, response }) => { | ||
if (graphQLErrors) { | ||
console.log("graphQLErrors", graphQLErrors) | ||
|
@@ -78,7 +97,7 @@ export const setupApolloClient = async () => { | |
if (networkError) { | ||
console.log("networkError", JSON.stringify(networkError)) | ||
// User access token has expired | ||
if (networkError.statusCode === 401) { | ||
if ((networkError as any).statusCode === 401) { | ||
// We assume we have both tokens needed to run the async request | ||
// Let's refresh token through async request | ||
return new Observable((observer) => { | ||
|
@@ -112,25 +131,12 @@ export const setupApolloClient = async () => { | |
} | ||
}) | ||
|
||
const accessToken = await getAccessTokenFromSession() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're already storing the useSession in the context provider, no need for it in the cache layer |
||
cache.writeData({ | ||
data: { | ||
isLoggedIn: !!accessToken, | ||
localBagItems: [], | ||
}, | ||
}) | ||
|
||
await persistCache({ | ||
cache, | ||
storage: AsyncStorage, | ||
}) | ||
|
||
return new ApolloClient({ | ||
// Provide required constructor fields | ||
cache, | ||
link: ApolloLink.from([authLink, errorLink, link]), | ||
link: ApolloLink.from([authLink, errorLink, httpLink]) as any, | ||
typeDefs, | ||
resolvers, | ||
cache, | ||
// Provide some optional constructor fields | ||
name: "react-web-client", | ||
version: "1.3", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kierangillen Do we actually need to do this for every paginated query? seems a bit odd they would add that much complexity
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this is the new method for handling merging arrays now that
updateQuery
is deprecated: https://www.apollographql.com/docs/react/caching/cache-field-behavior/#merging-arraysThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here's a helpful blogpost as the Apollo docs are out of date: https://www.apollographql.com/blog/announcing-the-release-of-apollo-client-3-0/?mc_cid=e593721cc7&mc_eid=235a3fce14