Architecture of GraphQL: In previous topics, we understood all the basic concepts of GraphQL. In this topic, we will discuss the architecture of GraphQL. With the help of architecture of GraphQL we can easily understand that how GraphQL queries are used in an HTTP client, especially within a mobile or a web application.
As we have already discussed, GraphQL is not an implementation; GraphQL is just a kind of specification. So, when Facebook released GraphQL, it was released as in the form of specification. GraphQL is a specification that describes how the GraphQL server work and its behavior. It also provides some significant guidelines for executing and handling query requests from the client-side. It also handles the response to be given for these requests from the server-side like some supported protocols, in which format the server accepts data from client or user, the format of the data in which server responds to the request of client, etc.
When a client or user requests the server for retrieving some kind of data, the request is named as or known as a ‘query.’ And, when a client launches a query for communicating with the server, the query is transported to the server through some transport layer. The communication between server and client is also setup through this transport layer. You can connect this transport layer of GraphQL with any available networks such as WebSocket, TCP, or any other transport layer protocol.
We get one plus point while implementing GraphQL because the GraphQL server does not care about the kind of database you are using. GraphQL is entirely neutral to the database. You can use it from any NoSQL to relational database. You can implement GraphQL to any of the databases you want to.
The flow of Client-server communication in the GraphQL:
• First of all, we need to remember that the GraphQL query which we execute is not written in JSON. Whenever a client makes a ‘POST’ request to send the query to the GraphQL server, the query is sent as in the form of a string.
• The server receives the string and then extract the query of the client from the string. When the server extracts the string’s query, it starts processing and validating the client’s query according to the syntaxes of GraphQL and GraphQL schema (which we discuss later in this GraphQL tutorial).
• Same as the server of other APIs, the GraphQL’s API server makes a call to the database as per the query and retrieves data from the database, which the client requested in his query.
• After this, when GraphQL gets the data from the database, which the client requested in his query, the GraphQL server takes the data and returns to the client in the form of a JSON object.
That’s how communication is established between the client and the server through GraphQL queries.
How to build GraphQL server?
We have learned how communication is done between the client and the server using GraphQL, connected with the database. Let us proceed with our topic of ‘Building a GraphQL server.’
For using GraphQL in our application or in our projects, first, we need to build a GraphQL server. You can build a GraphQL server using any programming language which you like. As we have earlier mentioned, it is open-source and can be implemented with different programming languages. You can do this with any programming language.
Other than this, first we need to discuss the architecture model of the GraphQL server. There are three basic and standard architectural models for the implementation of the GraphQL server. Let’s discuss all these three architectural models of GraphQL server’s implementation one by one.
• The GraphQL server is connected with a database.
• The GraphQL server is integrated with an existing database system.
• The GraphQL server is used with a hybrid approach model where the GraphQL server is connected with a Database connected with an existing integrated system.
Let us learn more details about each of the models as mentioned above for GraphQL server architecture: –
1. GraphQL server is connected with a database:
It is the most straightforward approach for building up a GraphQL server with our database. This type of setup is usually used for building new projects with GraphQL. In this type of architecture setup, the GraphQL server is connected with a database. And in this setup, when a client executes a query, the GraphQL server reads the query and understands what kind of data client has asked for, fetches the data from the client’s database, and returns it to the client. This complete process of Executing query and returning the data is called ‘Resolving the query‘ where the GraphQL server resolves the clients’ query. After resolving the query, the query’s response is returned to the client in an official format of GraphQL specification.
The communication between the client and the GraphQL server is done by sending a query over HTTP via computer/mobile.
In the above image, the architecture you can see is a GraphQL server integrated with a database. This kind of Implementation of GraphQL in the projects is the simplest form of implementation. You can integrate your GraphQL server with the existing database, and the GraphQL server and database are integrated with a single node. As we have mentioned, the query is launched into the GraphQL server in this type of architecture over HTTP on mobile or computer. When the GraphQL server receives the client’s query over HTTP, the GraphQL starts processing the query and fetch data from the database, and retrieves it to the client in specific GraphQL format.
Important point of the GraphQL server integrated with a single database
• The architecture, as mentioned above, is the most common type of GraphQL architecture, which is used in the ‘Greenfield projects.’
• Another critical point to note that GraphQL is a transport-layer agnostic server.
• Like we have mentioned above, GraphQL doesn’t care about the type of database you will be using in your projects.
• The above architecture uses the GraphQL specification ‘resolving the query.’
• This architecture uses only one web-server while implementing GraphQL with the database.
2. The GraphQL server is integrated with an existing database system:
Another architecture of GraphQL, which is majorly used, is the GraphQL server is integrated with many existing databases of many services with a single GraphQL server. This type of architecture is mainly used with companies that are having very involved and legacy infrastructure. Many other APIs have grown over the years and are used with such a structure, but they need very high maintenance, which has created a company’s burden. Another major problem of the companies with such a legacy system is that this system makes almost practically design some new and innovative products that need to access multiple systems at once.
GraphQL server is used with these existing systems and hide the complexity of such kind of legacy infrastructure. Through this, the new client application can quickly be developed by merely communicating with the single GraphQL server. This GraphQL server will fetch the data from the multiple existing databases they needed.
Same as the previous architecture, the GraphQL server doesn’t care about what kind of database you will integrate it with. The existing databases can be of any kind like SQL database or NoSQL database. GraphQL will be integrated with both kinds.
In the above image, you can see that the architecture makes it easy for Companies with legacy and complex infrastructures to develop new communication by integrating a single GraphQL server with all the multiple existing databases. The GraphQL server works like an interface between the client-side and the company’s existing system—the client-side only deals with the GraphQL APIs to fetch data from multiple services.
Key points of this architecture
• This type of Architecture also uses a single GraphQL integrated server, but it is integrated with multiple servers this time.
• The client doesn’t need to communicate with all the existing databases. The communication is done between the client and the integrated single GraphQL web server. The GraphQL fetches data from the existing system for the client’s request and returns it to the client with a specific GraphQL format.
3. The GraphQL server is used with a hybrid approach model where GraphQL server is connected with a Database, and database is connected with an existing integrated system:
We have one more GraphQL architecture, and we can say that this form of architecture is a combination of the above two models. Yes, it’s finally possible to combine two or more approaches and build a single GraphQL web server with its database connected with it. And still, this architecture helps to communicate with a third-party APIs or existing system’s multiple databases.
When a client requests a query from the GraphQL server, the GraphQL server resolves and executes it, and fetches data from the database. It is connected with or retrieves data from the existing system’s database.
In the above image, we can clearly see that the hybrid model architecture of the GraphQL server is a combination of the first two types of architecture of GraphQL. The client only needs to communicate with the GraphQL server, and GraphQL will process the client’s query and retrieve the data to the client in the GraphQL’s specific format.
These are the three types of an architectural model of the GraphQL server integrated with our system. GraphQL server can be built on any of the architecture, as mentioned above, model based on your needs and project, which you are working on.