Skip to content

joaquin-alfaro/graphql-gateway

Repository files navigation

graphql-gateway

Gateway to expose rest services using GraphQL.

Overview

architecture

Modules

graphql-server

Exposes the endpoint POST /graphql for query requests

query {
  countriesById(id: "DE") {
    name
    cities{
      name
    }
  }
}
type CityDto {
  id: ID
  name: String
}

type CountryDto {
  cities: [CityDto]
  iso: String
  name: String
}

graphql-registry

Exposes the following endpoints to manage registration of services:

POST /registry to register services in the gateway

curl --location --request POST 'http://localhost:8080/registry' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "rest-countries-service",
"url": "http://localhost:8082/v2/api-docs"
}'

DELETE /registry to unregister services from the gateway

curl --location --request DELETE 'http://localhost:8080/registry?service=rest-countries-service' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "booksApi",
"url": "http://localhost:8081/v2/api-docs"
}'

GET /registry to list registered services in the gateway

curl --location --request GET 'http://localhost:8080/registry'

graphql-registry-client

Includes the annotation @GraphQLRegistryService to register services during starting

graphql-schema

Includes the code to map swagger with graphql

graphql-gateway-server

springboot service that includes graphql-server and grapqhl-registry

How it works

Service registry

service registry

Graphql queries

graphql queries

Setup

Configuration of springboot service to register in graphql-gateway

Dependency with graphql-registry-client

<dependency>
    <groupId>org.formentor</groupId>
    <artifactId>graphql-registry-client</artifactId>
    <version>${graphql-gateway.version}</version>
</dependency>   

Annotation of spring boot class with @GraphQLRegistryService

@SpringBootApplication
@GraphQLRegistryService
public class BooksApplication {
	public static void main(String[] args) {
		SpringApplication.run(BooksApplication.class, args);
	}
}

Configuration

Configuration of the url of the graphql-gateway in application.yaml

graphql:
  registry:
    uri: http://localhost:8080

Usage and example

  1. Install
mvn clean package
  1. Start graphql-gateway
java -jar graphql-gateway-server/target/graphql-gateway.jar

Example

The project includes the samples rest-books-service and rest-countries-service to test the gateway

Start rest-books-service

java -jar rest-books-service/target/*.jar

The service rest-books-service will register the fields books and booksById in query type

query {
    books {
        name
        id
        author {
            lastName
            firstName
        }
    }
}
query {
  booksById(id: "one") {
    id
    name
    author {
      lastName
    }
  }
}

Start rest_countries_service

java -jar rest-countries-service/target/*.jar

The service rest-countries-service will register the fields countries and countriesById in query type

query {
    countries {
        iso
        name
        cities {
            name
            id
        }
    }
}
query {
    countriesById(id: "DE") {
        name
        cities{
            name
        }
    }
}

Launch GraphQL queries using GraphQL Playground

graphql playground

About

GraphQL gateway

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages