admin管理员组文章数量:1023815
I'm trying to fetch some data from my cache. In initial reload data prop returns undefined but if I fast reload the app (react native fast reload) data prop has the value I want. I can not understand why it is returning undefined in initial reload. One case might be I'm calling the query before the cache is initialised. I have consoled the local cache and it shows values but the query is retuning undefined.
My client setup in client.js
const dev = {
base_url: BASE_URL
};
const httpLink = createHttpLink({
uri: dev.base_url
});
const errorLink = onError(({ graphQLErrors, networkError, response }) => {
if (graphQLErrors) {
// do something with graphql error
console.log(graphQLErrors);
}
if (networkError) {
// do something with network error
console.log(networkError);
// console.log('network not available');
}
if (response) {
console.log(response);
}
});
const cache = new InMemoryCache();
const setupPersistedCache = async () => {
const persistor = new CachePersistor({
cache,
storage: AsyncStorage
});
// Read the current schema version from AsyncStorage.
const currentVersion = await AsyncStorage.getItem(SCHEMA_VERSION_KEY);
console.log('currentVersion', currentVersion);
if (currentVersion && currentVersion === SCHEMA_VERSION) {
// If the current version matches the latest version,
// we're good to go and can restore the cache.
console.log('not migrating 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.
console.log('migrating cache');
await persistor.purge();
await AsyncStorage.setItem(SCHEMA_VERSION_KEY, SCHEMA_VERSION);
cache.writeData({
data: {
...initialState
}
});
await persistCache({
cache,
storage: AsyncStorage,
debug: true
});
}
// console.log(cache.data);
};
setupPersistedCache();
const link = ApolloLink.from([errorLink, httpLink]);
const client = new ApolloClient({
defaults: initialState,
link,
cache,
resolvers
});
export default client;
My initialState.js
export default {
language: 'bd'
};
My index.js
const AppProvider = () => {
const [loaded, setLoaded] = useState(false);
const configureCache = async () => {
try {
const cache = new InMemoryCache();
await persistCache({
cache,
storage: AsyncStorage,
debug: true
});
console.log(cache.data);
} catch (error) {
console.error('Error restoring Apollo cache', error);
}
};
useEffect(() => {
configureCache()
.then(() => {
setLoaded(true);
})
.catch(() => {
setLoaded(false);
});
}, []);
useEffect(() => {
SplashScreen.hide();
}, []);
return (
<>
{loaded ? (
<ApolloProvider client={client}>
<Root />
</ApolloProvider>
) : (
<View style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}}
>
<TextComponent
content="Loading"
size={fonts.fs24}
family={fonts.medium}
color={colors.white}
/>
</View>
)}
</>
);
};
AppRegistry.registerComponent(appName, () => AppProvider);
My query
export const getLangQuery = gql`
query getLang {
language @client
}
`;
I'm trying to get the data like this in my root page.
const { loading, error, data } = useQuery(getLangQuery);
const [setLanguage, result] = useMutation(setLangQuery);
const language = data;
console.log(language);
I'm trying to fetch some data from my cache. In initial reload data prop returns undefined but if I fast reload the app (react native fast reload) data prop has the value I want. I can not understand why it is returning undefined in initial reload. One case might be I'm calling the query before the cache is initialised. I have consoled the local cache and it shows values but the query is retuning undefined.
My client setup in client.js
const dev = {
base_url: BASE_URL
};
const httpLink = createHttpLink({
uri: dev.base_url
});
const errorLink = onError(({ graphQLErrors, networkError, response }) => {
if (graphQLErrors) {
// do something with graphql error
console.log(graphQLErrors);
}
if (networkError) {
// do something with network error
console.log(networkError);
// console.log('network not available');
}
if (response) {
console.log(response);
}
});
const cache = new InMemoryCache();
const setupPersistedCache = async () => {
const persistor = new CachePersistor({
cache,
storage: AsyncStorage
});
// Read the current schema version from AsyncStorage.
const currentVersion = await AsyncStorage.getItem(SCHEMA_VERSION_KEY);
console.log('currentVersion', currentVersion);
if (currentVersion && currentVersion === SCHEMA_VERSION) {
// If the current version matches the latest version,
// we're good to go and can restore the cache.
console.log('not migrating 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.
console.log('migrating cache');
await persistor.purge();
await AsyncStorage.setItem(SCHEMA_VERSION_KEY, SCHEMA_VERSION);
cache.writeData({
data: {
...initialState
}
});
await persistCache({
cache,
storage: AsyncStorage,
debug: true
});
}
// console.log(cache.data);
};
setupPersistedCache();
const link = ApolloLink.from([errorLink, httpLink]);
const client = new ApolloClient({
defaults: initialState,
link,
cache,
resolvers
});
export default client;
My initialState.js
export default {
language: 'bd'
};
My index.js
const AppProvider = () => {
const [loaded, setLoaded] = useState(false);
const configureCache = async () => {
try {
const cache = new InMemoryCache();
await persistCache({
cache,
storage: AsyncStorage,
debug: true
});
console.log(cache.data);
} catch (error) {
console.error('Error restoring Apollo cache', error);
}
};
useEffect(() => {
configureCache()
.then(() => {
setLoaded(true);
})
.catch(() => {
setLoaded(false);
});
}, []);
useEffect(() => {
SplashScreen.hide();
}, []);
return (
<>
{loaded ? (
<ApolloProvider client={client}>
<Root />
</ApolloProvider>
) : (
<View style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}}
>
<TextComponent
content="Loading"
size={fonts.fs24}
family={fonts.medium}
color={colors.white}
/>
</View>
)}
</>
);
};
AppRegistry.registerComponent(appName, () => AppProvider);
My query
export const getLangQuery = gql`
query getLang {
language @client
}
`;
I'm trying to get the data like this in my root page.
const { loading, error, data } = useQuery(getLangQuery);
const [setLanguage, result] = useMutation(setLangQuery);
const language = data;
console.log(language);
Share
Improve this question
edited Jan 13, 2020 at 13:16
Hardik Virani
1,7557 gold badges24 silver badges36 bronze badges
asked Jan 13, 2020 at 8:54
Akash SarkarAkash Sarkar
6821 gold badge9 silver badges22 bronze badges
2 Answers
Reset to default 6data
is always initially undefined, even if the result is fetched from the cache instead of the server. Once the data is loaded, its persisted in ponent state, so even if the ponent rerenders, the data does not have to be fetched from the cache again. A Fast Refresh just triggers a rerender -- it does not reload your whole app -- so any ponent state, including data
in this case, is persisted.
I figure it out myself. I was initialising the persistCache
multiple time.Once in client.js and once in index.js. Thats why it was behaving weirdly. Now Everything is behaving as expected.
I'm trying to fetch some data from my cache. In initial reload data prop returns undefined but if I fast reload the app (react native fast reload) data prop has the value I want. I can not understand why it is returning undefined in initial reload. One case might be I'm calling the query before the cache is initialised. I have consoled the local cache and it shows values but the query is retuning undefined.
My client setup in client.js
const dev = {
base_url: BASE_URL
};
const httpLink = createHttpLink({
uri: dev.base_url
});
const errorLink = onError(({ graphQLErrors, networkError, response }) => {
if (graphQLErrors) {
// do something with graphql error
console.log(graphQLErrors);
}
if (networkError) {
// do something with network error
console.log(networkError);
// console.log('network not available');
}
if (response) {
console.log(response);
}
});
const cache = new InMemoryCache();
const setupPersistedCache = async () => {
const persistor = new CachePersistor({
cache,
storage: AsyncStorage
});
// Read the current schema version from AsyncStorage.
const currentVersion = await AsyncStorage.getItem(SCHEMA_VERSION_KEY);
console.log('currentVersion', currentVersion);
if (currentVersion && currentVersion === SCHEMA_VERSION) {
// If the current version matches the latest version,
// we're good to go and can restore the cache.
console.log('not migrating 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.
console.log('migrating cache');
await persistor.purge();
await AsyncStorage.setItem(SCHEMA_VERSION_KEY, SCHEMA_VERSION);
cache.writeData({
data: {
...initialState
}
});
await persistCache({
cache,
storage: AsyncStorage,
debug: true
});
}
// console.log(cache.data);
};
setupPersistedCache();
const link = ApolloLink.from([errorLink, httpLink]);
const client = new ApolloClient({
defaults: initialState,
link,
cache,
resolvers
});
export default client;
My initialState.js
export default {
language: 'bd'
};
My index.js
const AppProvider = () => {
const [loaded, setLoaded] = useState(false);
const configureCache = async () => {
try {
const cache = new InMemoryCache();
await persistCache({
cache,
storage: AsyncStorage,
debug: true
});
console.log(cache.data);
} catch (error) {
console.error('Error restoring Apollo cache', error);
}
};
useEffect(() => {
configureCache()
.then(() => {
setLoaded(true);
})
.catch(() => {
setLoaded(false);
});
}, []);
useEffect(() => {
SplashScreen.hide();
}, []);
return (
<>
{loaded ? (
<ApolloProvider client={client}>
<Root />
</ApolloProvider>
) : (
<View style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}}
>
<TextComponent
content="Loading"
size={fonts.fs24}
family={fonts.medium}
color={colors.white}
/>
</View>
)}
</>
);
};
AppRegistry.registerComponent(appName, () => AppProvider);
My query
export const getLangQuery = gql`
query getLang {
language @client
}
`;
I'm trying to get the data like this in my root page.
const { loading, error, data } = useQuery(getLangQuery);
const [setLanguage, result] = useMutation(setLangQuery);
const language = data;
console.log(language);
I'm trying to fetch some data from my cache. In initial reload data prop returns undefined but if I fast reload the app (react native fast reload) data prop has the value I want. I can not understand why it is returning undefined in initial reload. One case might be I'm calling the query before the cache is initialised. I have consoled the local cache and it shows values but the query is retuning undefined.
My client setup in client.js
const dev = {
base_url: BASE_URL
};
const httpLink = createHttpLink({
uri: dev.base_url
});
const errorLink = onError(({ graphQLErrors, networkError, response }) => {
if (graphQLErrors) {
// do something with graphql error
console.log(graphQLErrors);
}
if (networkError) {
// do something with network error
console.log(networkError);
// console.log('network not available');
}
if (response) {
console.log(response);
}
});
const cache = new InMemoryCache();
const setupPersistedCache = async () => {
const persistor = new CachePersistor({
cache,
storage: AsyncStorage
});
// Read the current schema version from AsyncStorage.
const currentVersion = await AsyncStorage.getItem(SCHEMA_VERSION_KEY);
console.log('currentVersion', currentVersion);
if (currentVersion && currentVersion === SCHEMA_VERSION) {
// If the current version matches the latest version,
// we're good to go and can restore the cache.
console.log('not migrating 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.
console.log('migrating cache');
await persistor.purge();
await AsyncStorage.setItem(SCHEMA_VERSION_KEY, SCHEMA_VERSION);
cache.writeData({
data: {
...initialState
}
});
await persistCache({
cache,
storage: AsyncStorage,
debug: true
});
}
// console.log(cache.data);
};
setupPersistedCache();
const link = ApolloLink.from([errorLink, httpLink]);
const client = new ApolloClient({
defaults: initialState,
link,
cache,
resolvers
});
export default client;
My initialState.js
export default {
language: 'bd'
};
My index.js
const AppProvider = () => {
const [loaded, setLoaded] = useState(false);
const configureCache = async () => {
try {
const cache = new InMemoryCache();
await persistCache({
cache,
storage: AsyncStorage,
debug: true
});
console.log(cache.data);
} catch (error) {
console.error('Error restoring Apollo cache', error);
}
};
useEffect(() => {
configureCache()
.then(() => {
setLoaded(true);
})
.catch(() => {
setLoaded(false);
});
}, []);
useEffect(() => {
SplashScreen.hide();
}, []);
return (
<>
{loaded ? (
<ApolloProvider client={client}>
<Root />
</ApolloProvider>
) : (
<View style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center'
}}
>
<TextComponent
content="Loading"
size={fonts.fs24}
family={fonts.medium}
color={colors.white}
/>
</View>
)}
</>
);
};
AppRegistry.registerComponent(appName, () => AppProvider);
My query
export const getLangQuery = gql`
query getLang {
language @client
}
`;
I'm trying to get the data like this in my root page.
const { loading, error, data } = useQuery(getLangQuery);
const [setLanguage, result] = useMutation(setLangQuery);
const language = data;
console.log(language);
Share
Improve this question
edited Jan 13, 2020 at 13:16
Hardik Virani
1,7557 gold badges24 silver badges36 bronze badges
asked Jan 13, 2020 at 8:54
Akash SarkarAkash Sarkar
6821 gold badge9 silver badges22 bronze badges
2 Answers
Reset to default 6data
is always initially undefined, even if the result is fetched from the cache instead of the server. Once the data is loaded, its persisted in ponent state, so even if the ponent rerenders, the data does not have to be fetched from the cache again. A Fast Refresh just triggers a rerender -- it does not reload your whole app -- so any ponent state, including data
in this case, is persisted.
I figure it out myself. I was initialising the persistCache
multiple time.Once in client.js and once in index.js. Thats why it was behaving weirdly. Now Everything is behaving as expected.
本文标签: javascriptapollo client returning undefined in initial reload in react nativeStack Overflow
版权声明:本文标题:javascript - apollo client returning undefined in initial reload in react native - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745514098a2153959.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论