Wrapping a REST API in GraphQL on Client Side

TLDR: This hack allows you to use Relay and GraphQL on the Client Side even though your server has a classic REST API. It does this by intercepting the Client side GraphQL calls on the client side only and transforming them into REST Calls before the calls go out, and doing the reverse when you get a response from your REST Endpoint.

 

ref: http://graphql.org/blog/rest-api-graphql-wrapper/

In this post I will outline a rapid, low-investment method that you can use to stand up a GraphQL endpoint atop an existing REST API, using JavaScript alone. No backend developers will be harmed during the making of this blog post.

A client-side REST wrapper

We’re going to create a GraphQL schema – a type system that describes your universe of data – that wraps calls to your existing REST API. This schema will receive and resolve GraphQL queries all on the client side. This architecture features some inherent performance flaws, but is fast to implement and requires no server changes.

this architecture features some inherent performance flaws because of how GraphQL is still calling your underlying REST API which can be very network intensive. A good next step is to move the schema from the client side to the server side to minimize latency on the network and to give you more power to cache responses. See: https://discventionstech.wordpress.com/2017/01/27/wrapping-a-rest-api-in-graphql-on-server-side/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s