Skip to content

Instantly share code, notes, and snippets.

@jsbonso
Created October 19, 2017 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsbonso/f2584a5f74676e5b37b86ba4e79eca7b to your computer and use it in GitHub Desktop.
Save jsbonso/f2584a5f74676e5b37b86ba4e79eca7b to your computer and use it in GitHub Desktop.
Primer to GraphQL
The What, When and Why of GraphQL
In our previous lecture, we had a good introduction of what GraphQL is
by looking at the common issues of a RESTful API, and how GraphQL solves
and improves
Those certain issues.
This time, we will dive deeper on what GraphQL is,
the story on how it came to be
And its core concepts and features such as Queries, Mutations, Directives and many more.
Let’s begin.
First off,
What is GraphQL?
-
- GraphQL is basically just a query language specification made by Facebook back in 2012.
- It is a specification of a new data query language for web services.
- That is why GraphQL has a suffix “QL” , which means Query Language.
- And since it is ONLY a specification and not an actual language per se, there are a lot of GraphQL Implementations out there for almost any language, such as GraphQL for JavaScript, GraphQL for Java, GraphQL for PHP and so and so forth.
- GraphQL is a new and emerging query language that offers an alternative and better way, to fetch data and performs operations to your server and even on top of your existing web services.
GraphQL Diagram
- Application
Why should you use GraphQL?
Well, why not? With all of its benefits.
- Api version free
- Single Endpoint
-
The shape of the returned data is determined entirely by the client's query, so servers become simpler and easy to generalize. When you're adding new product features, additional fields can be added to the server, leaving existing clients unaffected. When you're sunsetting older features, the corresponding server fields can be deprecated but continue to function. This gradual, backward-compatible process removes the need for an incrementing version number. We still support three years of released Facebook applications on the same version of our GraphQL API.
Nowadays, Facebook rich web and mobile apps are lightning-fast, even though there have hundreds of
API calls running under the hood. How did they able to manage to build that kind of system?
Let me tell you a story.
Back in 2012, Facebook began an effort to rebuild its native mobile applications. This was the time when they were still using FQL, or the Facebook SQL-like API.
At the time, their iOS and Android apps were just thin wrappers around views of their mobile website. While this brought them close to an ideal of the "write once, run anywhere" mobile app, the down side is that it pushed their mobile-webview apps beyond their limits.
As Facebook's mobile apps added more and more features, rendering became more complex and hence, there mobile-webview apps suffered poor performance and frequently crashed.
http://facebook.github.io/graphql/October2016/
https://code.facebook.com/posts/1691455094417024/graphql-a-data-query-language/
The GraphQL data query language is:
* A specification. The spec determines the validity of the schema on the API server. The schema determines the validity of client calls.

* Strongly typed. The schema defines an API's type system and all object relationships.

* Introspective. A client can query the schema for details about the schema.

* Hierarchical. The shape of a GraphQL call mirrors the shape of the JSON data it returns. Nested fields let you query for and receive only the data you specify in a single round trip.

* An application layer. GraphQL is not a storage model or a database query language. The graph refers to graph structures defined in the schema, where nodes define objects and edges define relationships between objects. The API traverses and returns application data based on the schema definitions, independent of how the data is stored.

Why is GitHub using GraphQL?
GitHub chose GraphQL for our API v4 because it offers significantly more flexibility for our integrators. The ability to define precisely the data you want—and only the data you want—is a powerful advantage over the REST API v3 endpoints. GraphQL lets you replace multiple REST requests with a single call to fetch the data you specify.
For more details about why GitHub has moved to GraphQL, see the original announcement blog 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment