We use Apollo-client for making graphql query from the front-end (react) and Golang to implement graphql in backend.
We are displaying time-series data in the Front-end. Our graphql query looks like this to request 6 mins worth of data
query {
fetchTimeSlots(id: 1, start: t, end: t+6) { // where t is epoch time
TS
Data
}
}
This data will be stored in Apollo cache with key fetchTimeSlots(id :"1, start: t, end: t+6).
Since user has the option to change time range, the query can change to
query {
fetchTimeSlots(id: 1, start: t+3, end: t+9) { // where t is epoch time
TS
Data
}
}
This will be a whole new query to graphql (since the query parameter changes) even though we already have data in Apollo cache for t+3 to t+6.
Currently we tackle this problem by
- manually writing the data into certain key in Apollo cache using writeQuery
- Parse the cache and find out available timestamps and only request graphql with missing timestamps
But we want to solve this problem without these hacks as our application purely made of just various time-series data and we do not want to add various hacks to tackle cache issue.
How can we design the time-series schema so that it is cached by Apollo by default.
We are trying this following solution.
query {
alias_t: fetchTimeSlots(id: 1, ts: t) {
TS
Data
}
alias_t1: fetchTimeSlots(id: 1, ts: t+1) {
TS
Data
}
alias_t2: fetchTimeSlots(id: 1, ts: t+2) {
TS
Data
}
}
In this way, Apollo cache will store each query separately ie. fetchTimeSlots(id :1, ts: t) as one query. The next time, when we request the same timestamp data, it will be served from cache.
The problem with this approach is, in server side all these queries are executed in parallel. But for optimal performance, we would want to provide Graphql server with array of times so that server can resolve in one go.
My questions would be
- Is there a standard way in representing time-series data in Graphql so that Apollo caching can work seamless and server performance is also not impacted
- Can we parse the query and modify it before sending it to resolver
- Any other better solution to solve this problem.