Skip to content

Nodejs microservices implementation with grapqhql gateway for data relation

Notifications You must be signed in to change notification settings

segpacto/graphql-gateway-microservices-sample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphql-gateway-rest-microservices-sample

This Repository contains three microservices, from these two are REST services and one graphql gateway . All implemented in NodeJs. The idea is to demonstrate how to implement a GraphQL Gateway using REST microservices documented with OpenApi/Swagger. Using the mocked data from the REST microservices, graphql is able to establish relations between the data that belongs to different services. The graphql-gateway performs autogeneration of Types, Queries and Mutations based on the Swagger documentation of each microservice.

Note: This a simplified version of how to build a GraphQL Gateway, and it is meant to be for educational purposes only. To make a proper use of an architecture like the one described here, is necessary to implement tests, configure and use in a better way the project environment variables, create a proper cache policies according to your business case need, API gateway to control the access to your microservices and make partial validation of JWT token if it is the case, introduce good CI/CD strategies and keep the good practices for building APIs.

Article

For a deeper understanding of the reasons and context for this Repo. Please, take a look at the full article.

Runnning all services

To run all services

docker-compose up --build # Build services
docker-compose up # Start services

Can be accessed visually accessing to http://localhost:5000 to create Queries, Mutations and access all Types autogenerated from the microservices

Queries and Mutations samples

Performs a search of customers by name, also includes per customer a list of invoices.

mutation customersSearch {
  searchCustomerByName(body: {name: "John"}) {
    data {
      customerId
      firstName
      lastName
      invoices {
        amount
        invoiceDate
        customerId
      }
    }
  }
}

Get a customer by Id and the associated invoices

query {
  getCustomerById(customerId: "1") {
    firstName
    invoices {
      amount
      customerType
      invoiceNumber
    }
  }
}

Retrieve an invoice by Id and returning the associated customer

query {
  getInvoiceById(invoiceId: "10") {
    amount
    invoiceNumber
    invoiceDate
    customer {
      firstName
      lastName
      email
    }
  }
}

Running services independently

Customer-Api

Contains the follwowing endpoints:

  • POST /customers/search # Customers search, Returns a list of Customers
  • GET /customers/{customerId} #Find a customer by ID, Return a Customer

The service can be found under the directory /customer.

Running standalone service:

npm install
npm run start

Can be accessed visually accessing to http://localhost:3000/api-docs To use the Swagger-UI to create the requests directly from the web, on /customer/swagger.json set the property host to localhost:3000

Invoice-Api

Contains the follwowing endpoints:

  • GET /invoices/customer/{customerId} # Find invoices by customer ID, Returns a list of Invoices
  • GET /invoices/{invoiceId} # Find an invoice by ID, Returns an Invoice

The service can be found under the directory /invoice. Running standalone service:

npm install
npm run start

Can be accessed visually accessing to http://localhost:3001/api-docs To use the Swagger-UI to create the requests directly from the web, on /invoice/swagger.json set the property host to localhost:3001

GraphQL-Gateway

The service can be found under the directory /graphql-gateway. The service uses under the hood the package gql-gateway. To learn how to establish data relations take a deeper look at the gql-gateway README.

Running standalone service:

npm install
npm run start

Can be accessed visually accessing to http://localhost:5000 To start this service the OpenApi/Swagger documentation endpoints should be reachable, and to create Queries and Mutations those services should be accessible. On the OpenAPI/Swagger documentation of each service the host and basePath should be properly configured, this is what indicates to this service where to direct all the requests.

About

Nodejs microservices implementation with grapqhql gateway for data relation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published