Introducing GraphQL Support in Choreo
- Miraj Abeysekara
- 6 Oct, 2022
We’re happy to announce the release of the GraphQL component type in Choreo, which allows you to develop, deploy, and manage GraphQL services.
GraphQL is widely used for modern mobile and web application backends as it solves many problems associated with REST APIs like over-fetching, under-fetching, and tight coupling between the frontend and the backend. With Choreo now supporting GraphQL components, users can leverage it to build better applications.
Let’s look at what GraphQL is, discuss why you should use it, and then review its capabilities in Choreo.
What is GraphQL?
GraphQL is a query language similar to SQL where you can query an API for specific data, rather than receiving an entire API payload. Like SQL, in addition to data queries, you can also manipulate data via GraphQL. HTTP is the most common way of exposing the GraphQL capabilities to the clients via a single endpoint. Once we expose these capabilities via the HTTP protocol, it becomes a GraphQL API, serving over HTTP.
GraphQL enables the client-side to control what data they require, which reduces data fetching overhead. It also allows client-side applications to evolve using existing data without changing the existing domain APIs.
Let’s discuss the benefits of using GraphQL compared to REST.
Why Should You Use GraphQL Instead of a REST API?
1. No over-fetching or under-fetching
Two common problems with REST APIs are over-fetching (getting more data than required), and under-fetching (having to make multiple requests to get all the data required). These can lead to higher bandwidth usage and power consumption, which is particularly troublesome in mobile devices. GraphQL solves these issues by taking a cue from SQL and letting developers select what data to fetch with each API call. This eliminates over and under-fetching issues and reduces client-side processing.
2. Strongly-typed schema
REST APIs lack a built-in, strongly-typed schema system. GraphQL has a built-in Schema Definition Language (SDL) to define the APIs. This empowers developers to use tools like code generators to increase productivity and reduce mistakes when developing GraphQL services.
3. Rapid application development
Frontend applications can evolve rapidly based on changing requirements and user feedback. This, in turn, can introduce different data requirements compared to previous versions. With REST APIs, developers would need to update both the frontend and backend to respond to these changes, slowing down development. Since GraphQL provides a single endpoint to query the necessary data, there is no need to wait for any backend changes. Further, GraphQL APIs can evolve without breaking existing clients, speeding up the development of both frontend and backend applications.
Using Choreo for GraphQL APIs
Choreo is a full lifecycle cloud native developer platform that enables building and running cloud native applications at scale. With the addition of GraphQL support, you can leverage all the benefits that Choreo offers including cloud development, multi-environment deployment, testing, and observability.
Let’s explore how you can use the above-mentioned capabilities to develop and manage GraphQL APIs.
Develop GraphQL APIs Using the Built-in Code Editor
Choreo offers an online VS Code development environment for you and your team to build GraphQL APIs using the Ballerina programming language. Choreo’s VS Code editor comes with pre-installed tools to develop, test, and version control your GraphQL services, making it easier for you to start coding. This is shown in Figure 1.
Multi-Environment Deployment and Management
Similar to other component types (like REST APIs and Scheduled Tasks) you can leverage the multiple environments as shown in Figure 2 to test and conduct quality assurance before making your GraphQL API available to users. This can be done via Choreo’s default environments or pre-configured private environments.
Query Generation and Testing
Once your GraphQL API is deployed to an environment, use the Test page for the built-in GraphQL IDE, which allows you to explore documentation, generate queries, and invoke the API. This is seen in Figure 3.
Operation Level Observability for GraphQL APIs
GraphQL API monitoring at the operation level is difficult compared to REST API resource level monitoring as GraphQL doesn’t use HTTP resource paths and always returns HTTP 200 (the request has succeeded), regardless of whether the query was successful or not. Usually, you need to use additional tools to monitor GraphQL APIs at an operational level.
Choreo offers GraphQL operation-level observability out-of-the-box. Therefore, you don’t need to invest in additional tooling for monitoring. Figure 4 shows the Throughput and Latency graphs for allTodos operation for a sample Todo service.
Get Started with GraphQL APIs in Choreo
To create a GraphQL component, click on the GraphQL card on the “Create a new component” page after logging into the Choreo console as shown in Figure 5.
Afterwards, see our GraphQL documentation to create your custom GraphQL APIs in Choreo.
If you are new to Choreo, visit Choreo and sign up for free!