The Power of GraphQL With RESTful APIs — Meet Hypnos
Desktop app for sandboxing GraphQL-like calls and responses to RESTful API endpoints
Airbnb, Audi, CNBC, and Expedia are among the engineering teams Apollo touts as adopting its technology.
GraphQL is lauded for its speed and declarative querying; unlike REST APIs, a GraphQL query returns exactly what the client requests to a single endpoint, boosting performance for clients with poor internet connections or less powerful devices.
But to implement the querying language, an API overhaul is necessary to appropriately serve GraphQL requests. To test queries, developers have to spin up a quick application or write a GraphQL back end.
Existing playgrounds are useful but they might not have meaningful data or errors for developers. And while GraphQL is very powerful, errors are not descriptive, even with the Apollo Client layer.
That’s where Hypnos comes in.
What Is Hypnos?
Hypnos is an endpoint tester, utilizing Apollo’s link-rest package to make GraphQL-structured queries to authenticated or non-authenticated RESTful APIs.
When a proper query is made, a response object is rendered on the screen with the fields specified by the client.
How Does It Work?
With the link-rest package, the Apollo client makes a fetch request to a given API and path. Upon receiving a response, the client parses through the response to structure data akin to what would be returned from a GraphQL endpoint.
While the under-the-hood actions are via normal fetches to REST APIs, instead of a cumbersome JSON-formatted object with fields irrelevant to the client, the data is parsed according to the query specification.
How Hypnos Helps
Apollo does not inherently handle errors declaratively. It does allow developers to catch errors with the apollo-link-error package in whichever way they choose.
However, if a GraphQL error is not written correctly (e.g. in the front end), the feedback can be unclear. Hypnos catches syntax errors and plainly describes where the fault in the query construction is.
For GraphQL, if a query is structured properly to a valid endpoint, it will always run and return information and a
200 status code, even if declared query fields return null.
To help with this, Hypnos parses the response data to check for null values. As REST APIs might have intentionally null values, the entire response is still displayed, along with stack trace where null values exist.
The Hypnos team (Dillon Garrett, Sophie Nye, and Will Robinson) are very proud of their first release. If you are interested in Hypnos (v0.0.1-beta), please check out our GitHub page, and try out our application.