Open In App

Serving GraphQL over HTTP

HTTP or Hypertext Transfer Protocol, is the foundation of Web communication. Being a stateless protocol, each request from a client to the server is independent and doesn’t retain information about past requests. GraphQL is a query language for APIs and a runtime for executing those queries.

Unlike traditional REST APIs with fixed endpoints and predefined structures, GraphQL allows clients to request the specific data they need on the same endpoint.



One common way to serve GraphQL is over HTTP, providing an efficient and flexible means of communication between client and server. In this article, we’ll explore how we can use the HTTP protocol to serve GraphQL and how it differs from traditional RESTful approaches.

Web Request Pipeline

URL and Routes

HTTP Methods

HTTP Methods are also known as HTTP which signifies the action to be performed on a resource.



HTTP GET Request

When we are using a HTTP GET request to execute a GraphQL query,

GET method will be used when we are using Query in graphql to fetch data. Below is the GraphQL query format for “Query” or to retrieve data.

{
"[links] {
"[url]" , [description]"
}
}

The above request in GET will look the following,

http://[apiendpoint]/graphql?query={[links]{[url] , [description}}

HTTP POST Request

GraphQL POST request use the application/json content type. A standard GraphQL POST request have the structure where the request body should be a JSON encoded object with the following fields,

GraphQL format for POST:

{ 
"query": "{ Links { url, description } }",
"variables": null,
"operationName": null
}

HTTP Response

Regardless of the request method, the response should be returned in the body of the response in JSON format. The payload contains,

Response:

{
"data": { },
"errors": [ ]
}

Implementation Using GraphQL in a Java Spring Boot Application for Book Management

Let’s consider the following Java Spring Boot application, where we are using a relational database to store book details and we are using GraphQL dependency to interact with the database.

Folder Structure

Following is the database structure we are using here,

public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id:
private String title;
private String desc;
private String author;
private double price:
private int pages;
}

Now all GraphQL configurations are done in the schema.graphqls file where we have defined the table structure and query and mutation methods to use when we will use the API endpoint.

Response:

The application is running in port no 9000, so the API for GraphQL is localhost:9000/graphql.

HTTP GET:

Let’s use GET Method to get details of all books in the database. As we are using GraphQL, so we can choose the attribute we need to see. For example let’s check the book’s Id, name, author name, and price.

GET

localhost:9000/graphql?query={allBooks{id title author price}}

Response:

HTTP POST:

We can use POST method to manipulate data and retrieve data also. Let’s try a GraphQL mutation where we are going to send a book details to store in the database using POST Method.

Response:

Mutation:

mutation{
createBook(book:{
title: "GraphQL in Action"
desc: "Book to learn GraphQL"
author: "Poul E."
price: 470
pages: 512
}){
id
}
}


Response:

Conclusion

Using GraphQL provides developers the privilege to communicate with database using a single HTTP endpoint. HTTP methods like GET and POST is used to interact where GET is used to fetch data and POST is to send data. Responses follow a consistent JSON structure containing “data” and “error”. Following conventions and using best practices, developers ensure consistent and better communication between client and servers, integrating GraphQL into web applications.


Article Tags :