Skip to content

mouton0815/graphql-with-dgs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphQL with Netflix DGS

This is a demo of the Netflix DGS framework to create GraphQL services with Spring Boot.

It showcases a toy store with books, their authors, and sales records. The DGS data fetches allow to implement nested queries of arbitrary depth.

Run the Server

mvn spring-boot:run

The DGS services exposes an interactive GraphQL client on the URL

http://localhost:8080/graphiql

GraphiQL

Example Queries, Mutations, and Subscriptions

Show name, birthdate of the author with ID 3, plus title and year of all their books:

query {
  author(id: 3) {
    name
    birth
    books {
      title
      year  
    }
  }
}

Create a new author:

mutation {
  createAuthor(name: "John Steinbeck", birth: "1902-02-27", city: "Salinas") {
    id
    name
  }
}

To improve reuse, the mutation can be wrapped into a function, which takes its arguments from a variables object:

mutation CreateAuthor($name: String!, $birth: String, $city: String) {
  createAuthor(name: $name, birth: $birth, city: $city) {
    id
    name
  }
}

with example data in variables:

{
  "name": "John Steinbeck",
  "birth": "1902-02-27",
  "city": "Salinas"
}

Create a new book for the author:

mutation {
  createBook(title: "Of Mice and Men", year: 1937, authorId: 5) {
    id
    title
  }
}

Subscribe to book-sale events issued by the server:

subscription {
  sales {
    book {
      title
    }
    sales
  }
}

Curl Equivalents

GraphQL queries and mutations can also be executed by curl or other command-line HTTP clients. Below are the curl equivalents to the query and mutations above:

curl -X POST \
  -H 'Content-Type: application/json' \
  -d '{"query": "query { author(id: 3) { name birth books { title year } } }"}' \
  http://localhost:8080/graphql
curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"query": "mutation { createAuthor(name: \"John Steinbeck\", birth: \"1902-02-27\", city: \"Salinas\") { id name } }"}' \
  http://localhost:8080/graphql
curl -X POST \
  -H "Content-Type: application/json" \
  -d '{"query": "mutation { createBook(title: \"Of Mice and Men\", year: 1937, authorId: 5) { id title } }"}' \
  http://localhost:8080/graphql

GraphQL subscriptions are not supported by curl, see https://stackoverflow.com/a/47860810.

About

A GraphQL demo service with Netflix DGS and Spring Boot

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages