GraphQL: The Future of APIs?

Last January, Facebook announced a custom query language called GraphQL. This data query language provides a common interface between the client and the server for simpler data retrieval and manipulating data using the notion of mutation queries. It has since evolved into it's own architectural style as a direct replacement for REST. Specifications for GraphQL were released in July.

GraphQL gets it's name from the idea of graphical relationships. It is a type of social graph which maps relationships based on characteristics of a network using graph theory as opposed to a defined relational representation. Instead of dealing with data as objects which are then flattened to a tabular representation and then re-converted back into objects on the client side, GraphQL is a hierarchical set of fields. The response is formed exactly like the request. This allows more flexibility to the client. Instead of the server defining structured responses, the client will request the fields and relationships it wants. 

Here is an example query provided by Nick Schrock:

{  user(id: 3500401) {    id,    name,    isViewerFriend,    profilePicture(size: 50)  {      uri,      width,      height    }  } }

 

And the response:

{  "user" : {    "id": 3500401,    "name": "Jing Chen",    "isViewerFriend": true,    "profilePicture": {      "uri": "http://someurl.cdn/pic.jpg",      "width": 50,      "height": 50    }  } }

 

As you can see, the response mirrors the request. GraphQL also allows you to define multiple queries which would otherwise require multiple REST endpoints. Another thing, GraphQL also has built-in type introspection so you can inspect the schema with GraphQL queries which is something you couldn't do with REST.

Although it seems like GraphQL is better than REST APIs in every way, it doesn't mean REST will go away. GraphQL is powerful but some API interaction is designed to be simple and may not warrant a complete overhaul of some production APIs. Many companies have already built tools to support existing APIs in REST and may not look to migrate for many years down the road. In reality, if GraphQL proves to be be effective then it will exist along side REST. 

Facebook is the only company currently using GraphQL in production. We'll have to wait to see how it will work for everyone else. It's too early to tell if GraphQL will be the next API standard, but it certainly has a lot going for it.

Want to Learn More?

This is just a sample of what we can do. We have 15 years of experience working in nearly every technology and industry. Whatever you are doing, we've done it and are prepared to tackle your project. Reach out and we will discuss it with you.