【发布时间】:2019-02-13 00:48:05
【问题描述】:
给定一个返回多个级别响应的查询,例如Github's GraphQL API 上的此查询:
query {
viewer {
starredRepositories(first: 100) {
edges {
node {
repositoryTopics(first: 100) {
edges {
node {
id
topic {
id
name
}
}
}
}
}
}
}
}
}
如何规范化主题并使用apollo-link-state 将其存储到存储中?
{
topics: [Topic]
}
目前我的店铺设置如下:
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloClient } from 'apollo-client';
import { ApolloLink } from 'apollo-link';
import { withClientState } from 'apollo-link-state';
const cache = new InMemoryCache();
const store = withClientState({
cache,
defaults: {
topics: [],
},
resolvers: {},
typeDefs: `
type Topic {
id: String!
name: String!
}
type Query {
topics: [Topic]
}
`,
});
const client = new ApolloClient({
cache,
links: ApolloLink.from([
// Other links ... ,
store,
// Other links ... ,
]),
});
检查我的缓存显示ROOT_QUERY:
{
topics: { ... },
viewer: User
starredRepositories({"first":100}): StarredRepositoryConnection
...
}
【问题讨论】:
标签: graphql apollo react-apollo graphql-js apollo-client