An Introduction to GraphQL #1

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data

What is GraphQL?

GraphQL is a new API standard that provides a more efficient, powerful and flexible alternative to REST. It was developed and open-sourced by Facebook and is now maintained by a large community of companies and individuals from all over the world. At its core, GraphQL enables declarative data fetching where a client can specify exactly what data it needs from an API. Instead of multiple endpoints that return fixed data structures, a GraphQL server only exposes a single endpoint and responds with precisely the data a client asked for.

. . .

GraphQL - A Query Language for APIs

GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data. GraphQL isn't tied to any specific database or storage engine and is instead backed by your existing code and data. Most applications today have the need to fetch data from a server where that data is stored in a database. It’s the responsibility of the API to provide an interface to the stored data that fits an application’s needs.
. . .

Why GraphQL?


1. Efficient Data Loading
With GraphQL, you can send a query to your API and get exactly what you need, nothing more and nothing less. It’s really that simple. GraphQL minimizes the amount of data that needs to be transferred over the network and thus majorly improves applications operating under these conditions. If you compare this feature with the conventional intuitive nature of REST, you’ll understand that this is a major improvement to the way we initially do things.
2. One request, many resources
Another useful feature of GraphQL is that it makes it simple to fetch all required data with one single request. The structure of GraphQL servers makes it possible to declaratively fetch data as it only exposes a single endpoint.
3. Modern & Compatibility with frontend frameworks and platforms
Modern applications are now built in comprehensive ways where a single backend application supplies the data that is needed to run multiple clients. Web applications, mobile apps, smart screens, watches etc can now depend only on a single backend application for data to function efficiently. With GraphQL, each client can access precisely the data it needs.
. . .

How does it work?

A GraphQL service is created by defining types and fields on those types, then providing functions for each field on each type. For example, a GraphQL service that tells us who the logged in user is (me) as well as that user's name might look something like this:
type Query { me: User } type User { id: ID name: String }

Along with functions for each field on each type:
function Query_me(request) { return request.auth.user; } function User_name(user) { return user.getName(); }

Once a GraphQL service is running (typically at a URL on a web service), it can receive GraphQL queries to validate and execute. A received query is first checked to ensure it only refers to the types and fields defined, then runs the provided functions to produce a result.

For example the query:
{ me { name } }

Could produce the JSON result:
{ "me": { "name": "Luke Skywalker" } }
. . .

History of GraphQL

The first time Facebook publicly spoke about GraphQL was at React.js Conf 2015 and shortly after announced their plans to open source it. Because Facebook always used to speak about GraphQL in the context of React, it took a while for non-React developers to understand that GraphQL was by no means a technology that was limited to usage with React.


. . .

GraphQL Adoption & Community

In fact, GraphQL is a technology that can be used everywhere a client communicates with an API. Interestingly, other companies like Netflix or Coursera were working on comparable ideas to make API interactions more efficient. Coursera envisioned a similar technology to let a client specify its data requirements and Netflix even open-sourced their solution called Falcor. After GraphQL was open-sourced, Coursera completely canceled their own efforts and hopped on the GraphQL train. Today, GraphQL is used in production by lots of different companies such as GitHub, Twitter, Yelp and Shopify - to name only a few.

Despite being such a young technology, GraphQL has already been widely adopted. Learn who else is using GraphQL in production.
. . .


Never miss a post from Gufran Mirza, when you sign up for Ednsquare.