Let's see the architecture of GraphQL to understand how GraphQL is used in an HTTP client, especially in a web/mobile app.
GraphQL was released as a specification. The specification describes the behavior of the GraphQL server. It provides some guidelines to handle requests from the clients and responses from the server, such as supported protocols, the format of the data that the server accepts, the format of the server's response, etc.
The client makes a request to the GraphQL server. This request is called a query. When a client requests a query to communicate with the server, the transport layer of GraphQL can be connected with any available network protocol such as TCP, WebSocket, or any other transport layer protocol.
The GraphQL server doesn't care about the database you use. It is neutral to databases. You can use a relational or a NoSQL database.
Client-Server flow in GraphQL
How to build a GraphQL server?
To use GraphQL in our project, we have to build the GraphQL server. You can do it in any programming language of your choice.
There are three common architectural models for a GraphQL server.
GraphQL Server with a Connected Database
This architecture setup is mostly used for new projects. In this architecture setup, the GraphQL server is integrated with the database. When the client sends a query, the server reads the requested query and fetches data from the database. This process is known as resolving the query. After resolving the query, the response is returned to the client in the official GraphQL specification format.
In the above architecture, you can see that the GraphQL server and the database are integrated on a single node. The client communicates with the GraphQL server by sending a query via computer/ mobile over HTTP. After receiving the query, the GraphQL server processes the request retrieves data from the database and returns it to the client.
Key points of GraphQL Server with a Connected Database
GraphQL Server integrated with the Existing System.
This architecture model is used for companies having complicated projects which have legacy infrastructure and many different APIs. In this architecture model, GraphQL can be used to merge microservices, legacy infrastructure, and third-party APIs in the existing system and hide the complexity of data fetching logic. The server doesn't care about the database you use. It may be a relational or a NoSQL database.
In the above architecture, you can see that the GraphQL server acts as an interface between the client and the existing systems.
A Hybrid approach with a Connected Database and Integrated Systems
This architecture model is a combination of the above two approaches: the GraphQL server with a connected database and the GraphQL server integrated with the existing system. In this architecture model, when a query is received by the server, the server resolves the received query and retrieves data either from the connected database or from the integrated API's.
The above diagram is the representation of the hybrid approach model.