【发布时间】:2018-06-04 21:28:46
【问题描述】:
我有一个使用 React 作为客户端框架和 Graphql 来运行 API 的应用程序。 计划正在获取用户帖子和帖子计数。
客户端
const Profile = () => (
<div>
<PostCount />
<Posts />
</div>
)
const PostCount = () => ...
const Posts = () => ...
我们需要在 Posts 组件中显示帖子,在 Count 组件中显示帖子数。 所以我的问题是,哪个更好。
在一个请求中获取 Profile 组件中的所有数据,并将其作为 props 发送到 Post 和 Count 组件。或获取 Count 组件中的帖子计数和 Post 组件中的帖子。
场景一包括对服务器的一个请求和更大的数据块。
场景二包括两个对服务器的请求和较小的数据块。
场景一服务器端:
const schema = `
type Query {
feeds(): Feed!
}
type Feed {
posts: [Post!]!
count: Int!
}
type Post {
...
}
`
async function feed() {
const posts: await Post.findAll();
const count = await Post.count();
return {
count
posts,
}
}
场景二服务器端:
const schema = `
type Query {
posts(): [Post!]!
count(): Int!
}
type Post {
...
}
`
async function feed() {
const posts: await Post.findAll();
return posts;
}
async function count() {
const count = await Post.count();
return count;
}
附:还要考虑更大的数据。帖子和计数是示例。例如用户帖子和用户 cmets。
【问题讨论】:
标签: javascript reactjs graphql