Adding REST endpoints to Hasura

Of the many new features added to Hasura 2.0 – adding REST endpoints to stored queries and mutations has to be a standout. You can hardly finish typing "GraphQL" into a search engine before the "GraphQL vs REST", "Is GraphQL going to replace REST" punditry floods the suggested query autocomplete.

The fact is, was, and was bound to always be, REST is not going to go anywhere. There's a number of compelling use-cases where REST is still a preferred method for fetching data over the wire.

REST vs GraphQL? REST and GraphQL with Hasura

The question should always be "when" to use which API specification. If you are building for embedded systems, it's by far easier to modify the resolver than it is to modify the query. If you are building for the Jamstack, ensuring protected execution of your content is critical.

From the beginning of GraphQL's adoption in the industry, we've seen three common points of friction for adoption. Flagging vendor tooling for the existing production/deploy environments, security concerns about handing out an open API with infinite query depth, legacy systems that will require perpetual REST endpoint and so adopting GraphQL becomes an additional burden, not consolidating the pain points.

These days, and specifically with Hasura, and even more specifically with our latest release supporting REST endpoints, you get all the benefits of GraphQL and the ability to maintain and deliver REST endpoints where needed.

Fortunately, with REST support in Hasura, you can have both. To create a REST endpoint from a query or mutation, you first create a named query/mutation in the GraphiQL API explorer, then press the REST button in the GraphiQL interface. From there, provide a helpful name to identify your endpoint later, provide the suffix path for where there endpoint should be called, and choose which verb (GET, POST, PATCH, PUT, DELETE) your endpoint should respond to.

Hasura supports traditional body parsing for dynamic variables as well as dynamic URL segments. Dynamic URL segments are denoted with : as a prefix in the path. Calling /shapes/round to a URL defined as /shapes/:shape would yield in a body of {"shape": "round"} being passed to the query or mutation serving as the resolver.

Whether the project you are building is a better target for REST or you have a legacy project you are planning on migrating slowly to GraphQL, REST endpoints in Hasura allow you to support the best of both worlds with sacrificing the developer velocity you've come to expect from Hasura and Hasura Cloud.

Learn more in this deep dive where we cover REST endpoints and a quick introduction to access controls to go with it.

Learn how to create a REST endpoint in Hasura Cloud with this deep-dive guide.

17 Mar, 2021

2 MIN READ

Share
Blog
17 Mar, 2021
Email
Subscribe to stay up-to-date on all things Hasura. One newsletter, once a month.
Loading...
v3-pattern
Accelerate development and data access with radically reduced complexity.