import ApolloClient from "apollo-client" ; import { ApolloLink } from "apollo-link" ; import { BatchHttpLink } from "apollo-link-batch-http" ; import { InMemoryCache } from "apollo-cache-inmemory" ; import { plugins } from "@webiny/plugins" ; import { ApolloDynamicLink } from "@webiny/app/plugins/ApolloDynamicLink" ; import { ApolloCacheObjectIdPlugin } from "@webiny/app/plugins/ApolloCacheObjectIdPlugin" ; export const createApolloClient = ( { uri } ) => { return new ApolloClient ( { link : ApolloLink . from ( [ new ApolloDynamicLink ( ) , new BatchHttpLink ( { uri } ) ] ) , cache : new InMemoryCache ( { addTypename : true , dataIdFromObject : obj => { const getters = plugins . byType < ApolloCacheObjectIdPlugin > ( ApolloCacheObjectIdPlugin . type ) ; for ( let i = 0 ; i < getters . length ; i ++ ) { const id = getters [ i ] . getObjectId ( obj ) ; if ( typeof id !== "undefined" ) { return id ; } } return obj . id || null ; } } ) } ) ; } ;