Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { ApolloClient } from "@wora/apollo-offline";
import ApolloCache from "@wora/apollo-cache";
import { HttpLink } from "apollo-link-http";
// import ApolloClientIDB from '@wora/apollo-offline/lib/ApolloClientIDB';
const httpLink = new HttpLink({
uri: "http://localhost:4000/graphql"
});
const cacheOptions = {
dataIdFromObject: o => o.id
};
const client = new ApolloClient({
link: httpLink,
cache: new ApolloCache(cacheOptions)
});
client.setOfflineOptions({
manualExecution: false, //optional
link: httpLink, //optional
start: async mutations => {
//optional
console.log("start offline", mutations);
return mutations;
},
finish: async (mutations, error) => {
//optional
console.log("finish offline", error, mutations);
},
const Home = props => {
const {loading, error, data, ...others} = useQuery(USER_TODOS, {
variables: {
// Mock authenticated ID that matches database
userId: 'me',
},
});
if (loading) return <div>;
if (error) return `Error! ${error.message}`;
return ;
};
</div>
const Home = props => {
const {loading, error, data, ...others} = useQuery(USER_TODOS, {
variables: {
// Mock authenticated ID that matches database
userId: 'you',
},
});
if (loading) return <div>;
if (error) return `Error! ${error.message}`;
return ;
};
</div>
return true;
},
onPublish: (offlinePayload) => { //optional
//const rand = Math.floor(Math.random() * 4) + 1
//offlinePayload.serial = rand===1;
//console.log("offlinePayload", offlinePayload.serial)
console.log("onPublish offline", offlinePayload)
return offlinePayload
}
};
const cacheOptions = {
dataIdFromObject: o => o.id
};
const client = new ApolloClient({
link: httpLink,
cache: new ApolloCache(cacheOptions)
}, offlineOptions);
export default client;
export function createApolloClient(initialState = {}) {
const httpLink = new HttpLink({
uri: 'http://localhost:3000/graphql',
});
const cacheOptions = {
dataIdFromObject: o => o.id,
};
const persistOptions = {
initialState,
};
const client = ApolloClientIDB.create(
{
link: httpLink,
},
{
cacheOptions,
persistOptions,
},
);
/*
const client = new ApolloClient({
link: httpLink,
cache: new ApolloCache(cacheOptions, cachePersistOption),
});*/
client.setOfflineOptions({
manualExecution: false, //optional