Author: Jochen Möller (Managing Director and CoFounder of EcholoN) - creation: 07.03.2023, last change: 28.03.2024
Welcome to our comprehensive article on GraphQL! In this article we will explain the basics of GraphQL and compare it to REST. GraphQL is a modern query language for APIs developed by Facebook. It offers many advantages over traditional REST APIs and is being used more and more by developers around the world.
GraphQL is a modern query language for APIs. It was developed by Facebook in 2012 and allows developers to access data more efficiently and retrieve only the data they need. Unlike REST, where multiple endpoints can exist, GraphQL has a single endpoint through which all queries can be retrieved.
Compared to REST APIs, GraphQL offers several advantages. While overfetching or underfetching of data often occurs with REST APIs, GraphQL allows for precise querying and return of data. With GraphQL, developers can specify exactly what data they need and receive only that data back. This leads to efficient data manipulation and optimised data retrieval.
GraphQL offers many advantages over REST APIs. It allows for efficient data retrieval as only the data that is needed is retrieved. Moreover, GraphQL is very flexible and allows developers to create complex queries with only a single request. In addition, GraphQL supports various programming languages such as Javascript, Python and Ruby. There is also an active community that supports and develops GraphQL, such as the GraphQL Foundation.
The heart of GraphQL is the schema. The schema defines the structure of the data and the possible queries that can be made. The type system of GraphQL allows developers to create clear and understandable API interfaces. The data is defined in terms of objects and fields, which in turn can be linked to each other.
The syntax of GraphQL is similar to that of JSON. Queries are made in the form of so-called "queries". A GraphQL query defines the desired data and its structure. It is also possible to use variables in queries to further increase flexibility. With GraphQL, data can also be mutated by changing, adding or deleting it via so-called "mutations". Subscriptions, like websockets, make it possible to receive real-time updates by establishing a permanent connection channel between client and server.
GraphQL Clients and Endpoints
To work with GraphQL, you need a GraphQL client. A GraphQL client is a software library that can be used to send queries to a GraphQL API. The client handles communication with the GraphQL server and processes the queries and responses. GraphQL supports multiple endpoints, which allows developers to have different functions within one application.
One of the main problems with REST APIs is overfetching or underfetching of data. Overfetching occurs when more data is retrieved than needed, while underfetching means that not enough data is retrieved. With GraphQL, developers can specify exactly what data they need and only get that data back. This eliminates overfetching and underfetching and improves query efficiency.
GraphQL not only enables efficient querying of data, but also efficient data manipulation. With GraphQL, data can be edited or added through so-called "mutations". Developers can specify exactly which changes are to be made and receive only the relevant information back.
GraphQL enables optimised data retrieval, as only the required data is retrieved. This eliminates the need for tedious searching of unused data. It also allows developers to create complex queries with just a single query, resulting in improved performance. This flexibility also allows developers to optimise data retrieval and improve response time.
GraphQL is supported by various programming languages such as JavaScript, Python and Ruby. REST is also supported by a wide range of programming languages due to its long market presence. Developers can choose the language that best suits their application.
The data query process differs greatly between GraphQL and REST. With REST, developers have to call multiple endpoints to retrieve all the data they need. With GraphQL, everything can be retrieved in a single query, improving efficiency and reducing traffic.
Both GraphQL and REST have their advantages and disadvantages in terms of developer friendliness. GraphQL offers a clear and understandable API interface, while REST offers more freedom in designing the API. The choice between GraphQL and REST depends on the requirements of the project and the preferences of the developers.
Using GraphQL for data exchange offers many benefits, but also poses some risks and challenges that should be considered during development and implementation.
Here are some of the risks associated with using GraphQL:
To use GraphQL in a JavaScript application, you need a GraphQL client such as Apollo Client or Relay. These clients allow you to communicate with the GraphQL API and send queries and mutations. With GraphQL, developers can create precise queries and retrieve only the data they need.
GraphQL can also be integrated into a cloud environment. Cloud hosting platforms such as AWS and Heroku offer support for GraphQL applications. Developers can host their GraphQL APIs in the cloud and benefit from the advantages of a scalable and reliable infrastructure.
With GraphQL, developers can specify exactly what data they need and receive only that data back. This enables efficient data retrieval and reduces data traffic. Developers can also create complex queries with just a single request, resulting in improved performance.
The EcholoN API uses GraphQL to perform queries and mutations. Queries can be used to retrieve information from the database. Specific fields can be defined to retrieve only the required data. This helps to maximise the efficiency of queries and minimise network traffic. On the other hand, mutations allow data to be changed in the database. This allows users to create new data, update existing data or even delete it. For example, process templates can then be addressed with workflows and / or work patterns in the workflow engine. Subscriptions, similar to WebSockets, allow current information to be obtained in real time by establishing a permanent connection channel between the client and the server. The use of GraphQL in the EcholoN API improves the flexibility and performance of the system.
{
views {
incidents(skip: 2, first: 2, orderBy: ["number_asc"]) {
id {
entityId
itemId
}
number
name
}
}
}
Reply
{
"data": {
"views": {
"incidents": [
{
"id": {
"entityId": "81986bf1-6e94-405d-b9c3-2a2eeb691d88",
"itemId": "39574653-eb5b-4276-a165-108953db1b08"
},
"number": "IM-000002",
"name": "Monitor flackert"
},
{
"id": {
"entityId": "81986bf1-6e94-405d-b9c3-2a2eeb691d88",
"itemId": "8956c73f-f91b-4234-9750-5785b52fda2e"
},
"number": "IM-000009",
"name": "Drucker druckt nicht "
}
]
}
}
}
mutation {
working {
enqueueChangeTargetDate {
latest(
input: {items: [
{reference: "myref1", identity: {entityId: "81986bf1-6e94-405d-b9c3-2a2eeb691d88", itemId: "9e1c3a25-1d7e-44ad-871f-f76750360fa7"}}],
title: "Change Date",
workedOn: "2023-09-06T11:00:00.000Z",
data: {targetDate: "2023-12-27T11:00:00.000Z"}}
) {
id
number
reference
statusCode
}
}
}
}
Reply
{
"data": {
"working": {
"enqueueChangeTargetDate": {
"latest": [
{
"id": "c03d5902-4456-4e6b-8bcd-06643f4df6aa",
"number": 0,
"reference": "myref1",
"statusCode": "Pending"
}
]
}
}
}
}
Perhaps also interesting: