Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export const getApolloClient = (getIdToken: () => Promise) => {
const cache = new InMemoryCache()
const persistor = new CachePersistor({
cache,
storage: window.localStorage as any,
maxSize: 4.5 * 1024 * 1024,
})
const lastPurge = localStorage.getItem(PERSIST_LAST_PURGE_KEY)
if (!lastPurge || Number(lastPurge) < Date.now() - PERSIST_TTL) {
localStorage.setItem(PERSIST_LAST_PURGE_KEY, String(Date.now()))
persistor
.purge()
// eslint-disable-next-line no-console
.catch((err) => console.log(`Cache purge error: ${err}`))
} else {
persistor
.restore()
// eslint-disable-next-line no-console
),
);
if (networkError) console.log(`[Network error]: ${networkError}`);
}),
stateLink,
authLink,
new HttpLink({
uri: process.env.REACT_APP_GITHUB_API_URL,
credentials: 'same-origin',
}),
]),
cache: apolloCache,
});
try {
const persistor = new CachePersistor({
cache: apolloCache,
storage: window.localStorage,
debug: true,
maxSize: 2097152, // 2mb
});
const currentVersion = await window.localStorage.getItem(
SCHEMA_VERSION_KEY,
);
if (
currentVersion &&
currentVersion.toString() === SCHEMA_VERSION.toString()
) {
// If the current version matches the latest version,
// we're good to go and can restore the cache.
export const getApolloClient = (getIdToken: () => Promise) => {
const cache = new InMemoryCache()
const persistor = new CachePersistor({
cache,
storage: window.localStorage as any,
maxSize: 4.5 * 1024 * 1024,
})
const lastPurge = localStorage.getItem(PERSIST_LAST_PURGE_KEY)
if (!lastPurge || Number(lastPurge) < Date.now() - PERSIST_TTL) {
localStorage.setItem(PERSIST_LAST_PURGE_KEY, String(Date.now()))
persistor
.purge()
// eslint-disable-next-line no-console
.catch((err) => console.log(`Cache purge error: ${err}`))
} else {
persistor
.restore()
// eslint-disable-next-line no-console
kind: 'UNION',
name: 'Event',
possibleTypes: [{ name: 'Transfer' }],
},
],
},
}
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData,
})
const cache = new InMemoryCache({ fragmentMatcher })
// TODO what should happen if this fails???
persistCache({
cache,
storage: AsyncStorage,
}).catch((err) => {
console.error('Apollo.persistCache error', err)
})
export const apolloClient = new ApolloClient({
uri: BLOCKCHAIN_API_URL,
cache,
})
let client; // eslint-disable-line
const cache = new InMemoryCache({
dataIdFromObject: o => {
switch (o.__typename) {
case 'Procedure':
return o.procedureId;
default:
return o._id;
}
},
});
const persistor = new CachePersistor({
cache,
storage: AsyncStorage,
debug: false,
debounce: 1000,
// maxSize: false
});
const authLinkMiddleware = setContext(async (_, { headers }) => {
// get the authentication token from local storage if it exists
const token = await AsyncStorage.getItem('auth_token');
const refreshToken = await AsyncStorage.getItem('auth_refreshToken');
if (token && refreshToken) {
const decodedToken = jwtDecode(token);
const decodedRefreshToken = jwtDecode(refreshToken);
const persist = async () => {
const persistor = new CachePersistor({
cache: cache(),
storage: localforage as any
});
// Read the current schema version from AsyncStorage.
const currentVersion = await localforage.getItem(SCHEMA_VERSION_KEY);
if (APP_VERSION === currentVersion) {
// If the current version matches the latest version,
// we're good to go and can restore the cache.
await persistor.restore();
} else {
// Otherwise, we'll want to purge the outdated persisted cache
// and mark ourselves as having updated to the latest version.
await persistor.purge();
await localforage.setItem(SCHEMA_VERSION_KEY, APP_VERSION);
}
/* @flow */
import { AsyncStorage, Platform } from 'react-native';
import { ApolloClient } from 'apollo-client';
import { HttpLink, InMemoryCache, ApolloLink } from 'apollo-client-preset';
import { CachePersistor } from 'apollo-cache-persist';
import { generateClientStateLink, getSchemaVersion, saveSchemaVersion } from './';
import packageJson from '../../package.json';
const currentSchemaVersion = packageJson.schema_version;
const cache = new InMemoryCache();
const persistor = new CachePersistor({
cache,
storage: AsyncStorage,
trigger: Platform.OS === 'web' ? 'write' : 'background',
});
export default async () => {
const cacheSchemaVersion = (await getSchemaVersion()) || 0;
if (currentSchemaVersion === cacheSchemaVersion) {
await persistor.restore();
} else {
await persistor.purge();
await saveSchemaVersion(currentSchemaVersion);
}
const clientStateLink = await generateClientStateLink(cache);
return new ApolloClient({
return null;
}
}
},
defaults: {
leagueIds: []
}
});
export const client = new ApolloClient({
link: ApolloLink.from([errorLink, authLink, stateLink, httpLink]),
cache
});
// needs to be declared after client otherwise there might be problems with defaults
export const persistor = new CachePersistor({
storage: AsyncStorage,
cache,
debug: true
});
import AsyncStorage from '@react-native-community/async-storage'
import ApolloClient from 'apollo-boost'
import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory'
import { persistCache } from 'apollo-cache-persist'
import introspectionQueryResultData from 'src/apollo/fragmentTypes.json'
import config from 'src/geth/networkConfig'
import Logger from 'src/utils/Logger'
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData,
})
const cache = new InMemoryCache({ fragmentMatcher })
persistCache({
cache,
// @ts-ignore https://github.com/apollographql/apollo-cache-persist/pull/58
storage: AsyncStorage,
}).catch((reason: string) => Logger.error('Apollo/index', `Failure to persist cache: ${reason}`))
export const apolloClient = new ApolloClient({
uri: config.blockchainApiUrl,
cache,
})
// WebSocket connection
const websocketLink = new WebSocketLink({
uri: url,
options: {
reconnect
}
})
// Stitch different links together
const link = ApolloLink.from([
websocketLink
])
// Cache
const cache = new InMemoryCache().restore(window.__APOLLO_STATE__)
await persistCache({
cache,
storage: window.localStorage
})
// Create a new client
client = new ApolloClient({ cache, link, ssrForceFetchDelay: 100, debug: true })
return client
}