Skip to content

Commit

Permalink
fix(examples): remove ts-ignore, fix peristenceMapper return type, ap…
Browse files Browse the repository at this point in the history
…ply codestyle
  • Loading branch information
wodCZ committed Aug 10, 2021
1 parent 996c831 commit c7619e2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
10 changes: 4 additions & 6 deletions examples/react-native/src/hooks/useApolloClient.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { useCallback, useEffect, useState } from 'react';
import {useCallback, useEffect, useState} from 'react';
import {
ApolloClient,
InMemoryCache,
NormalizedCacheObject,
createHttpLink,
} from '@apollo/client';
import { AsyncStorageWrapper, CachePersistor } from 'apollo3-cache-persist';
import {AsyncStorageWrapper, CachePersistor} from 'apollo3-cache-persist';
import AsyncStorage from '@react-native-async-storage/async-storage';

import { persistenceMapper, createPersistLink } from '../utils/persistence';
import {persistenceMapper, createPersistLink} from '../utils/persistence';

export const useApolloClient = () => {
const [client, setClient] = useState<ApolloClient<NormalizedCacheObject>>();
Expand All @@ -27,8 +27,6 @@ export const useApolloClient = () => {
const cache = new InMemoryCache();
let newPersistor = new CachePersistor({
cache,
// https://github.com/apollographql/apollo-cache-persist/issues/426
// @ts-ignore
storage: new AsyncStorageWrapper(AsyncStorage),
debug: __DEV__,
trigger: 'write',
Expand All @@ -37,7 +35,7 @@ export const useApolloClient = () => {
await newPersistor.restore();
setPersistor(newPersistor);
const persistLink = createPersistLink();
const httpLink = createHttpLink({ uri: 'https://api.spacex.land/graphql' });
const httpLink = createHttpLink({uri: 'https://api.spacex.land/graphql'});
setClient(
new ApolloClient({
link: persistLink.concat(httpLink),
Expand Down
31 changes: 17 additions & 14 deletions examples/react-native/src/utils/persistence/persistenceMapper.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
export const persistenceMapper = (data: any) => {
export const persistenceMapper = async (data: any) => {
const parsed = JSON.parse(data);

const mapped: any = {};
const persistEntities: any[] = [];
const rootQuery = parsed['ROOT_QUERY'];

mapped['ROOT_QUERY'] = Object.keys(rootQuery).reduce((obj: any, key: string) => {
if (key === '__typename') return obj;
mapped['ROOT_QUERY'] = Object.keys(rootQuery).reduce(
(obj: any, key: string) => {
if (key === '__typename') return obj;

if (/@persist$/.test(key)) {
obj[key] = rootQuery[key];
if (/@persist$/.test(key)) {
obj[key] = rootQuery[key];

if (Array.isArray(rootQuery[key])) {
const entities = rootQuery[key].map((item: any) => item.__ref);
persistEntities.push(...entities);
} else {
const entity = rootQuery[key].__ref;
persistEntities.push(entity);
if (Array.isArray(rootQuery[key])) {
const entities = rootQuery[key].map((item: any) => item.__ref);
persistEntities.push(...entities);
} else {
const entity = rootQuery[key].__ref;
persistEntities.push(entity);
}
}
}

return obj;
}, { __typename: 'Query' });
return obj;
},
{__typename: 'Query'},
);

persistEntities.reduce((obj, key) => {
obj[key] = parsed[key];
Expand Down

0 comments on commit c7619e2

Please sign in to comment.