Skip to content

zw-engineers/spring-graphql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 

Repository files navigation

spring-graphql

Pre-requisites:

  • Java 11+
  • Spring Boot 2.1.7+
  • GraphQL Spring Boot starter
  • GraphiQL tool to execute graphql queries

How to startup the application:

Architecture

The idea is if any UI or Service wanted specific data, it could use GraphQL as a tool to filter out unnecessary data that we might not want the client to see/have. For example in this architecture, we have our University microservice which contains all university student details. Normally you would have another service consuming this University microservice with a getAllStudents() method and then the UI/Client would have to do all the filtering of that data. However, with our GraphQL microservice here, it will query our Univeristy microservice and filter out the data for our UI/client to consume. In our example, GraphQL will only return student data given an existing student name instead of the client getting all student data and then performing the filtering.

Starting University App

  • To start the University application just navigate into the university app directory containing the build.gradle file and just start the app by running the command ./gradlew bootRun and it should start the application on port 8081.
  • You can test that you are recieving a list of Students with degrees and tutors by calling the following API: http://localhost:8081/api/university/students

and you should have an output as below:

[
  {
    "name": "artemas",
    "surname": "muzanenhamo",
    "degree": {
      "degree": "software engineering"
    },
    "tutor": {
      "name": "jessica",
      "surname": "stevens",
      "degree": {
        "degree": "software engineering"
      }
    }
  },
  {
    "name": "thomas",
    "surname": "jenkins",
    "degree": {
      "degree": "bio-chemistry"
    },
    "tutor": {
      "name": "lavern",
      "surname": "masoja",
      "degree": {
        "degree": "bio-chemistry"
      }
    }
  },
  {
    "name": "sarah",
    "surname": "smith",
    "degree": {
      "degree": "computer science"
    },
    "tutor": {
      "name": "randal",
      "surname": "macbeth",
      "degree": {
        "degree": "computer science"
      }
    }
  }
]

Starting the GraphQL application

  • Navigate to the graphql application directory containing the build.gradle file and you should start the application with the following command: ./gradlew bootRun. The application should be started on port 8080

  • You will need a GraphQl client which will enable you to run queries against the data returned from the graphql application. There are a few clients as below:

  • Download any of the clients above and you should be able to access the graphql endpoint at: http://localhost:8080/graphql.

  • I have two top queries:

    • Get Student By Name - This query returns a student given an existing valid name
        type Query {
            getAllStudents: [Student]
        }
    • Below is an example to execute this query:
        {
          getStudentByName(name: "artemas") {
            name
            surname
            degree {
              degree
            }
            tutor{
              name
              surname
              degree {
                degree
              }
            }
          }
        }
    
    • Get All Students - This query will return all the students. This data matches what is returned by the University application.
        type Query {
            getAllStudents: [Student]
        }
    
    • Below is how you execute this query:
        {
          getAllStudents {
            name
            surname
            degree {
              degree
            }
            tutor {
              name
              surname
              degree {
                degree
              }
            }
          }
        }

About

Get familiar with Spring-Boot and GraphQL

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages