Skip to content

Commit c7619e2

Browse files
committed
fix(examples): remove ts-ignore, fix peristenceMapper return type, apply codestyle
1 parent 996c831 commit c7619e2

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

examples/react-native/src/hooks/useApolloClient.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { useCallback, useEffect, useState } from 'react';
1+
import {useCallback, useEffect, useState} from 'react';
22
import {
33
ApolloClient,
44
InMemoryCache,
55
NormalizedCacheObject,
66
createHttpLink,
77
} from '@apollo/client';
8-
import { AsyncStorageWrapper, CachePersistor } from 'apollo3-cache-persist';
8+
import {AsyncStorageWrapper, CachePersistor} from 'apollo3-cache-persist';
99
import AsyncStorage from '@react-native-async-storage/async-storage';
1010

11-
import { persistenceMapper, createPersistLink } from '../utils/persistence';
11+
import {persistenceMapper, createPersistLink} from '../utils/persistence';
1212

1313
export const useApolloClient = () => {
1414
const [client, setClient] = useState<ApolloClient<NormalizedCacheObject>>();
@@ -27,8 +27,6 @@ export const useApolloClient = () => {
2727
const cache = new InMemoryCache();
2828
let newPersistor = new CachePersistor({
2929
cache,
30-
// https://github.com/apollographql/apollo-cache-persist/issues/426
31-
// @ts-ignore
3230
storage: new AsyncStorageWrapper(AsyncStorage),
3331
debug: __DEV__,
3432
trigger: 'write',
@@ -37,7 +35,7 @@ export const useApolloClient = () => {
3735
await newPersistor.restore();
3836
setPersistor(newPersistor);
3937
const persistLink = createPersistLink();
40-
const httpLink = createHttpLink({ uri: 'https://api.spacex.land/graphql' });
38+
const httpLink = createHttpLink({uri: 'https://api.spacex.land/graphql'});
4139
setClient(
4240
new ApolloClient({
4341
link: persistLink.concat(httpLink),

examples/react-native/src/utils/persistence/persistenceMapper.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
export const persistenceMapper = (data: any) => {
1+
export const persistenceMapper = async (data: any) => {
22
const parsed = JSON.parse(data);
33

44
const mapped: any = {};
55
const persistEntities: any[] = [];
66
const rootQuery = parsed['ROOT_QUERY'];
77

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

11-
if (/@persist$/.test(key)) {
12-
obj[key] = rootQuery[key];
12+
if (/@persist$/.test(key)) {
13+
obj[key] = rootQuery[key];
1314

14-
if (Array.isArray(rootQuery[key])) {
15-
const entities = rootQuery[key].map((item: any) => item.__ref);
16-
persistEntities.push(...entities);
17-
} else {
18-
const entity = rootQuery[key].__ref;
19-
persistEntities.push(entity);
15+
if (Array.isArray(rootQuery[key])) {
16+
const entities = rootQuery[key].map((item: any) => item.__ref);
17+
persistEntities.push(...entities);
18+
} else {
19+
const entity = rootQuery[key].__ref;
20+
persistEntities.push(entity);
21+
}
2022
}
21-
}
2223

23-
return obj;
24-
}, { __typename: 'Query' });
24+
return obj;
25+
},
26+
{__typename: 'Query'},
27+
);
2528

2629
persistEntities.reduce((obj, key) => {
2730
obj[key] = parsed[key];

0 commit comments

Comments
 (0)