apollo-link-state defaults derived from query data?

893 Views Asked by At

Note: This is a followup question to my previous question about Apollo GraphQl Storing derived data

I'm using apollo-link-state to store data that's derived from query data. In this example the query data from the db includes some (x,y) points on a graph and the derived data is slope, moving average, acceleration etc.

My React graphing components need different combinations of original and derived data. Some need only the original.

I need derived data to be calculated only once and only when I query for it.

The example on the Apollo site seems to imply needing to trigger a mutation first but that seems wrong to me since each component that uses this derived data needs to trigger a mutation first to make sure it's initialized. I don't want to do a query and a mutation everywhere I need data.

So my question is: Can/should I use query resolvers in apollo-link-state or is there some better way of thinking about this?

UPDATE: I think their async example might be what I need but I need to work it through.

1

There are 1 best solutions below

0
On BEST ANSWER

Figured it out. Don't know why this wasn't obvious to me to begin with but.... hindsight.

In the end, you just need to define your resolver to return something. The resolver can even make its own queries.

export const getProjectDerived = (_obj, { ProjectId }, { cache }, info) => {

  const projQueryRes = cache.readQuery({
    query: projQuery,
    variables: {
      ProjectId
    }
  })
  const newObj = { ...something here... }

  return newObj


}

Then just include it in the 'Query' section of the resolvers.

import { getProjectDerived } from './project'

const resolvers = {
  Query: {
    ProjectDerived: getProjectDerived
  }
}
export default resolvers