Skip to content

itsandreramon/spring-graphql-books

Repository files navigation

codecov

Reactive Spring GraphQL

An example project showcasing a simple service which serves data about famous books.

Define your GraphQL queries

import com.expediagroup.graphql.spring.operations.Query
import org.springframework.stereotype.Component

@Component
class AuthorQuery(
    private val authorService: AuthorService
) : Query {

    fun authorById(id: Int): CompletableFuture<Author> {
        return authorService.findAuthorById(id)
            .toFuture()
    }
}

data class Author(val id: Int, val name: String?)

Let GraphQL Kotlin generate a type-safe schema for you

schema {
    query: Query
}

type Author {
    id: Int!
    name: String
}

type Query {
    authorById(id: Int!): Author!
}

Send a query to your GraphQL endpoint

{
  authorById(id: 1) {
    name
  }
}

How to run the sample

  1. Build a Docker image using Spring Boot's Buildpack support
$ ./gradlew bootBuildImage --imageName=itsandreramon/spring
  1. Make sure the corresponding itsandreramon/spring image has been built
$ docker images -a
  1. Run Docker Compose
$ docker-compose up
  1. Make sure everything is running correctly
$ docker-compose ps
  1. Access PostgreSQL to make changes according to your needs (e.g. via CLI)
$ docker exec -it database psql -U postgres

Default routes

Your newly created GraphQL server starts up with following preconfigured default routes:

  • /graphql - GraphQL server endpoint used for processing queries and mutations
  • /subscriptions - GraphQL server endpoint used for processing subscriptions
  • /sdl - Convenience endpoint that returns current schema in Schema Definition Language format
  • /playground - Prisma Labs GraphQL Playground IDE endpoint

Technologies used

About

Sample to demonstrate the usage of GraphQL on Spring Boot in a non-blocking manner.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages