/spring-boot-graphql-example

A demo application with Spring Boot and Graphql

Primary LanguageJava

GraphQL example with Spring Boot

Introduction

This repo contains a simple Spring Boot (2.x) service that implements GraphQL API. The implementation is based on graphql-java-tools a GraphQL java library.

Install

mvn clean install

Information

Graphql Endpoint: http://localhost:8080/api

Graphiql Endpoint: http://localhost:8080/graphiql

H2 database console: http://localhost:8080/h2-console

Example Usages

Example Query

Query to retrieve all people. Also, info about the pagination is returned.

query getPersons {
  persons {
    info {
      count
      pages
      size
      total
    }
    results {
      id
      firstName
      lastName
      age
    }
  }
}

Response:

{
  "data": {
    "persons": {
      "info": {
        "count": 4,
        "pages": 1,
        "size": 1000,
        "total": 4
      },
      "results": [
        {
          "id": "1",
          "firstName": "Joe",
          "lastName": "Briggs",
          "age": 23
        },
        {
          "id": "2",
          "firstName": "John",
          "lastName": "Smith",
          "age": 44
        },
        {
          "id": "3",
          "firstName": "Steve",
          "lastName": "Jones",
          "age": 16
        },
        {
          "id": "4",
          "firstName": "Tom",
          "lastName": "Richards",
          "age": 67
        }
      ]
    }
  }
}

Example Usage Ordering

Get people ordered by firt name on descendent:

query getPersons {
  persons(orderBy:[LAST_NAME_DESC]) {
    info {
      count
      pages
      size
      total
    }
    results {
      id
      firstName
      lastName
      age
    }
  }
}

Response:

{
  "data": {
    "persons": {
      "info": {
        "count": 4,
        "pages": 1,
        "size": 1000,
        "total": 4
      },
      "results": [
        {
          "id": "4",
          "firstName": "Tom",
          "lastName": "Richards",
          "age": 67
        },
        {
          "id": "3",
          "firstName": "Steve",
          "lastName": "Jones",
          "age": 16
        },
        {
          "id": "2",
          "firstName": "John",
          "lastName": "Smith",
          "age": 44
        },
        {
          "id": "1",
          "firstName": "Joe",
          "lastName": "Briggs",
          "age": 23
        }
      ]
    }
  }
}

Example Usage Filtering

Get people filtered by age.

{
  persons(filter: {age: 16}) {
    results {
      id
      firstName
      lastName
      age
    }
  }
}

Response:

{
  "data": {
    "persons": {
      "results": [
        {
          "id": "3",
          "firstName": "Steve",
          "lastName": "Jones",
          "age": 16
        }
      ]
    }
  }
}

Example Usage Paging

Get people paging in size 2, return page 2.

{
  persons(page:2, size: 2) {
    results {
      firstName
      age
    }
  }
}

Response:

{
  "data": {
    "persons": {
      "results": [
        {
          "firstName": "Steve",
          "age": 16
        },
        {
          "firstName": "Tom",
          "age": 67
        }
      ]
    }
  }
}

Example Usage Mutation

Create a new person

mutation {
  newPerson(firstName: "Jose", lastName: "Palma", age:39) {
    id
  }
}

Response:

{
  "data": {
    "newPerson": {
      "id": "5"
    }
  }
}