Javascript 从Graphql服务器获取数据的哪种方法更好?
我有一个带有React as客户端框架和Graphql的应用程序来运行API。 计划正在获取用户帖子和帖子计数 客户端Javascript 从Graphql服务器获取数据的哪种方法更好?,javascript,reactjs,graphql,Javascript,Reactjs,Graphql,我有一个带有React as客户端框架和Graphql的应用程序来运行API。 计划正在获取用户帖子和帖子计数 客户端 const Profile = () => ( <div> <PostCount /> <Posts /> </div> ) const PostCount = () => ... const Posts = () => ... 场景二服务器端: const schema = ` type
const Profile = () => (
<div>
<PostCount />
<Posts />
</div>
)
const PostCount = () => ...
const Posts = () => ...
场景二服务器端:
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;
}
P.S.也考虑更大的数据。员额和计数就是一个例子。例如用户帖子和用户评论。这两种方法都是正确的!选择更好的方法取决于您的应用程序
count
通常比提取数据快(有人可能会把它弄糟!:D),因此如果单独提取数据,则可以在文章仍在加载时更快地显示它
顺便说一句,GQL自己处理
Promise
!所以不需要异步等待 我知道两者都是正确的。但哪种方法的性能更好?关于异步等待。我从代码中复制了代码片段。原来的解析器有更多的功能。忘记删除asyinc WAIT。单独调用它们具有更好的性能,因为您可以更快地获取可用数据!