It's not just a GraghQL client
but also a framework for declarative data-fetching
.
Using REST API:
// Fetch the list of story IDs but not their details:rest.get('/stories').then(stories =>// This resolves to a list of items with linked resources:// `[ { href: "http://.../story/1" }, ... ]`Promise.all(stories.map(story =>rest.get(story.href) // Follow the links))).then(stories => {// This resolves to a list of story items:// `[ { id: "...", text: "..." } ]`console.log(stories);});
请求接口次数为 n + 1
;
graphql.get(`query { stories { id, text } }`).then(stories => {// A list of story items:// `[ { id: "...", text: "..." } ]`console.log(stories);});