REST (REpresentational State Transfer) has dominated the data retrieval process over HTTP for quite some years now and has been widely used to build APIs. Despite their popularity, REST APIs have apparent limitations for developers, such as the lack of control over the size and structure of the response payload. It just dumps everything into a response.
Enter GraphQL, a query language that lets you fetch just the pieces of information that you want, nothing more, nothing less!
We are glad to announce that Contentstack has added support for GraphQL. This means that you can create syntaxes to customize the responses of your API calls, to get precisely the data you want. The support for GraphQL is currently in Alpha.
Imagine the new power this gives to developers. They can fetch only the data that is required, and do away with the rest (or REST!).
Let us look at some of the powerful capabilities of GraphQL that enhances the way APIs work.
Steer clear of over-fetching
REST endpoints send back fixed content in response. A REST response may sometimes cause non-relevant data to show up, and add unnecessary bandwidth load.
GraphQL allows you to request data that is specific to your needs. You can decide what your response body should look like by specifying parameters within your request body.
Consider a scenario where you want to fetch the values of only the Title and Price fields of all entries of a content type.
The GraphQL query for this request will appear as follows:
query { all_product { items { title price } } }
You need to specify the content type UID and the fields that you want to fetch. The following shows the response of this query.
{ "data": { "all_product" { "items": [{ "title": "iPhone X", "price": "80,000" }] } } }
You can see that the response contains only the data you requested, nothing else. This is an example of the beauty of GraphQL.
Fetch related content in a single request
Consider a scenario where you want to fetch the values of the Title field of all products in which the value of the Category reference field is Mobiles.
The GraphQL query for this request will look as follows:
query { all_product(where: {categories: {title: "Mobiles"}}) { items { title categories { title } } } }
Here’s the response that you’ll get:
{ "data": { "all_product": { "items": [ { "title": "Galaxy Note", "categories": [ { "title": "Mobiles" }, { "title": "Bestseller" } ] }, { "title": "Galaxy J1", "categories": [ { "title": "Mobiles" } ] }, { "title": "iPhone 7 128GB", "categories": [ { "title": "Mobiles" }, { "title": "Bestseller" } ] }, { "title": "Redmi Note 3", "categories": [ { "title": "Mobiles" } ] } ] } } }
Build apps faster
Mobile apps have very concise views, and corresponding data speed, bandwidth and computing power has obvious limitations. One-trip relational queries is why mobile app developers love GraphQL. It helps reduce the number of API requests and delivers select data of only the required fields. Ultimately, this contributes to faster development, better app performance, and fewer developer headaches.
Mocking up of databases for simplified testing
GraphQL supports all the queries that Contentstack’s Content Delivery API provides. Through clear and intuitive schema definitions, GraphQL facilitates beneficial API development. Due to its static typed schema, GraphQL allows you to run tests on your backend with ease.
Consider a scenario where an application launches a new feature. Here, GraphQL’s strongly typed schema equips your backend and frontend with all the data needed to facilitate mocking of the backend for testing purposes. With an easy mocking up of the backend available, the quality, testing, and development teams can perform their development and testing activities smoothly.
Try it out
Check out our Contentstack GraphQL API Reference doc to see the complete list of supported queries. You can also try out the GraphQL queries with our embedded GraphiQL interface. Or check how you can use GraphQL with our SDKs.