Javatpoint Logo
Javatpoint Logo

GraphQL Architecture

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

  • The GraphQL query is not written in JSON. When a client makes a 'POST' request to send a GraphQL query to the server, this query is sent as a string.
  • The server receives and extracts the query string. After that, the server processes and validates the GraphQL query according to the GraphQL syntax and the graph data model (GraphQL schema).
  • Like the other API servers, the GraphQL API server also makes calls to a database or other services and retrieves the data requested by the client.
  • After that, the server takes the data and returns it to the client in a JSON object.

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
  • GraphQL server integrated with the existing system.
  • A hybrid approach with a connected database and integration of the existing system

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.

GraphQL Architecture

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

  • This architecture setup is used for simple and new projects.
  • It uses a single web server that implements GraphQL.
  • This model uses resolving the query.
  • In this model, the server resolves the queries and constructs responses with data that it fetches from the 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.

GraphQL Architecture

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.

GraphQL Architecture

The above diagram is the representation of the hybrid approach model.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA