All about GraphQl:

The GraphQL library causes rest-controller with various APIs to be lost and only one reference API to address: http:// [server-url] : [port] /graphql remains on the server code side. All requests are sent to the address above by RequestBody along with the POST method and the different methods are called according to the body sent. With this library, the Response model is sent within the Body and therefore there is no need to define different Response models in the code.

There are two types of sending information in body:

1. Query: Requests that the user sends to the server to receive information (similar to GET methods).
2. Mutation : Requests the user sends to the server to create, edit and delete information (similar to POST, PUT, DELETE).

For each Domain, in fact, instead of Rest-controller, two classes are considered for query and Mutation management. In fact, for more understanding, if we want to adapt to the MVC model, the order of the layers is as follows:

  1. Repository
  2. Service
  3. Query/Mutation

After defining different methods in Query and Mutation classes, we need to introduce them to GraphQL. According to the following code, within the Resources folder inside Graphql and in the schema.graphqls file, we define different types of Domain, types of Request models, types of Query methods, and a variety of Mutation methods. Different fields and their materials and arguments required for each method and their material are also mentioned in this file. It should be noted that the presence of the front mark <!> of each field is for the purpose of notNull being that field. After doing this, the project will be executable and different methods will be used by the fields introduced.

Example of how to submit a request:

  • The following Query request is to receive the User by its ID and display his name, ID and roles:

graphOL

  • Response:

graphql

  • To send a Mutation request is to create a user and determine their role, but only display the user’s name and ID after creation in response:

graphql

  • Response:

graphql

To check out the Graphql sample project, you can connect to my GitHub via the link below:

https://github.com/arsalanmrz/Graphql-spring-boot-sample