I’m trying to get cache to work on a query and for the life of me can never get client side to use cache.
This is my client setup:
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
companyOverview: {
keyArgs: ["CompanyOverviewInput",["id"]]
},
},
},
},
});
const defaultOptions = {
watchQuery: {
fetchPolicy: 'cache-and-network',
errorPolicy: 'ignore',
},
query: {
fetchPolicy: "network-only", // Used for first execution
nextFetchPolicy: "cache-first",
errorPolicy: 'all',
},
}
const client = new ApolloClient({
link: authLink.concat(httpLink),
// TODO - Use local cache example
cache: cache,
defaultOptions: defaultOptions,
connectToDevTools: true
});
This is the input type used:
extend type Query {
"""
Fetches company stock overview data
"""
companyOverview(stockSymbols: [CompanyOverviewInput]): [CompanyOverview]
}
"""
Input type used for submitting a new comment
"""
input CompanyOverviewInput {
"""
Stock symbol
"""
id: String
}
This is query in client:
const { loading, error, data } = useQuery(companyOverview, {
variables: {
"stockSymbols": [
{
"id": "DIS"
},
]
},
});
This is query text:
import {gql} from "@apollo/client";
export const companyOverview = gql`
query CompanyOverview($stockSymbols: [CompanyOverviewInput]) {
companyOverview(stockSymbols: $stockSymbols) {
id
Symbol
AssetType
Name
Description
CIK
Exchange
Currency
Country
Sector
Industry
Address
FiscalYearEnd
LatestQuarter
MarketCapitalization
EBITDA
PERatio
PEGRatio
BookValue
DividendPerShare
DividendYield
EPS
RevenuePerShareTTM
ProfitMargin
OperatingMarginTTM
ReturnOnAssetsTTM
ReturnOnEquityTTM
RevenueTTM
GrossProfitTTM
DilutedEPSTTM
QuarterlyEarningsGrowthYOY
QuarterlyRevenueGrowthYOY
AnalystTargetPrice
TrailingPE
ForwardPE
PriceToSalesRatioTTM
PriceToBookRatio
EVToRevenue
EVToEBITDA
Beta
_52WeekHigh
_52WeekLow
_50DayMovingAverage
_200DayMovingAverage
SharesOutstanding
DividendDate
ExDividendDate
graphs {
PE {
x
y
color
}
Price {
x
y
color
}
}
tables {
Summary {
name
label
labelShort
value
}
All {
name
label
labelShort
value
}
}
}
}
`;
if I use dev tools and tell explore to load from cache it does without hitting server:
Here’s the cache in dev tool:
OOT_QUERY
__typename:"Query"
__ref:"CompanyOverview:DIS"
CompanyOverview:DIS
CACHE ID
CompanyOverview:DIS
id:"DIS"
__typename:"CompanyOverview"
Symbol:"DIS"
AssetType:"Common Stock"
Name:"The Walt Disney Company"
Description:"The Walt Disney Company, commonly known as Disney, is an American diversified multinational mass media and entertainment conglomerate headquartered at the Walt Disney Studios complex in Burbank, California."
CIK:"1744489"
Exchange:"NYSE"
Currency:"USD"
Country:"USA"
Sector:"TRADE & SERVICES"
Industry:"SERVICES-MISCELLANEOUS AMUSEMENT & RECREATION"
Address:"500 SOUTH BUENA VISTA STREET, BURBANK, CA, US"
FiscalYearEnd:"September"
LatestQuarter:"2021-12-31"
MarketCapitalization:243308986000
EBITDA:10560000000
PERatio:79.41
PEGRatio:1.174
BookValue:49.87
DividendPerShare:0
DividendYield:0
EPS:1.683
RevenuePerShareTTM:40.15
ProfitMargin:0.0422
OperatingMarginTTM:0.0751
ReturnOnAssetsTTM:0.0169
ReturnOnEquityTTM:0.0368
RevenueTTM:72988000000
GrossProfitTTM:22287000000
DilutedEPSTTM:1.683
QuarterlyEarningsGrowthYOY:63.36
QuarterlyRevenueGrowthYOY:0.343
AnalystTargetPrice:192.13
TrailingPE:79.41
ForwardPE:35.09
PriceToSalesRatioTTM:3.333
PriceToBookRatio:3.03
EVToRevenue:4.274
EVToEBITDA:28.91
Beta:1.175
_52WeekHigh:198.54
_52WeekLow:129.26
_50DayMovingAverage:147.68
_200DayMovingAverage:165.45
SharesOutstanding:1820630000
DividendDate:"2020-01-16"
ExDividendDate:"2019-12-13"
But no matter what I do when I refresh the page in the browser it hits the server with a request.
Can anyone see what I’m doing wrong?