apollo-link-state defaults derived from query data?

可紊 提交于 2019-12-11 12:59:23

问题


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:


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


来源:https://stackoverflow.com/questions/52087275/apollo-link-state-defaults-derived-from-query-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!