A way you can quickly and clear explain what kind of data you want
Graphql can be compared to REST API:
In REST:
By sending request to "/users" url you telling server that you asking for usersBy sending request to "/users/123" url you telling server that you asking for user with id 123
In GraphQl
the ways to "explain" to server what exactly you want are much wider:Here is example how we can achieve the total repos count of user, only instead of using v3 GitHub api (like in previous posts)
we using GitHub grpahQL v4 api:
function getReposCount(user) {
// graphQL query
const query = `query {
user(login: ${user}) {
repositories {
totalCount
}
}
}`;
const body = JSON.stringify({ query });
const uri = `https://api.github.com/graphql`;
return fetch(uri, {
method: "POST",
headers: { Authorization: `bearer ${token}` },
body
})
.then(d => d.json())
.then(data => {
// extract only count field
const {
data: {
user: {
repositories: { totalCount: total }
}
}
} = data;
return total;
});
}