Up until this point, the News Feed data had been served as HTML. And there was no API endpoint available.
Developers grew frustrated at the amount of queries required to get the data required for different views.
This frustration led to the development of GraphQL.
{
github {
user(username: "gaearon") {
id
login
company
}
repo(ownerUsername: "gaearon" name: "redux"){
id
name
branches(limit: 1) {
name
}
}
}
}
{
"data": {
"github": {
"user": {
"id": 810438,
"login": "gaearon",
"company": "Facebook"
},
"repo": {
"id": 36535156,
"name": "redux",
"branches": [
{
"name": "core-enhancer"
}
]
}
}
}
}
GraphQL is just a string sent from the client to be processed by the server.
There are many server implementations:
Check out the repo chentsulin/awesome-graphql for more!
Suppose we wanted to query a couple users from GitHub
{
github {
gaearon: user(username: "gaearon") {
id
login
company
}
substack: user(username: "substack") {
id
login
company
}
}
}
{
"data": {
"github": {
"gaearon": {
"id": 810438,
"login": "gaearon",
"company": "Facebook"
},
"substack": {
"id": 12631,
"login": "substack",
"company": "cyberwizard institute"
}
}
}
}
Notice how the "user" field has been renamed
It's possible to group common fields in a query
{
github {
gaearon: user(username: "gaearon") {
...GithubUserFragment
}
substack: user(username: "substack") {
...GithubUserFragment
}
}
}
fragment GithubUserFragment on GithubUser {
id
login
company
}
{
"data": {
"github": {
"gaearon": {
"id": 810438,
"login": "gaearon",
"company": "Facebook"
},
"substack": {
"id": 12631,
"login": "substack",
"company": "cyberwizard institute"
}
}
}
}