从查询数据派生的apollo-link-state默认值? [英] apollo-link-state defaults derived from query data?
问题描述
注意:这是我先前关于的问题的跟进问题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.
我只需要一次并且只有当我query
时才需要计算派生数据.
I need derived data to be calculated only once and only when I query
for it.
Apollo网站上的示例似乎暗示需要先触发一个突变,但是这对我来说似乎是错的,因为使用此派生数据的每个组件都需要先触发一个突变以确保已初始化.我不想在需要数据的任何地方进行查询和更改.
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屋!