从查询数据派生的 apollo-link-state 默认值? [英] apollo-link-state defaults derived from query data?

查看:18
本文介绍了从查询数据派生的 apollo-link-state 默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:这是我之前关于 的问题的后续问题Apollo GraphQl 存储派生数据

我使用 apollo-link-state 来存储从查询数据派生的数据.在这个例子中,来自数据库的查询数据包括图形上的一些 (x,y) 点,导出的数据是斜率、移动平均值、加速度等.

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.

我的 React 绘图组件需要原始数据和派生数据的不同组合.有些只需要原件.

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.

所以我的问题是:我可以/应该在 apollo-link-state 中使用 query 解析器还是有更好的思考方式?

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.

推荐答案

想通了.不知道为什么这对我来说一开始并不明显,但是......事后看来.

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

这篇关于从查询数据派生的 apollo-link-state 默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆