Java Examples of Netflix DGS
Module | Description |
---|---|
✅a-start | Example of multiple *.graphqls ,@DgsData.List |
✅b-codegen | Example of codegen,multiple modules,methods in type,support constant in @DgsData,@RequestHeader |
✅c-scalar | Example of custom scalar |
✅d-http | Example of Query,Mutation,Subscription,params validation,Apollo Tracing |
✅e-file | Example of file upload |
✅f-auth | Example of authentication and authorization |
✅g-error | Example of custom error type |
✅h-ut | Example of uni test, integration test, unit test of supporting custom scalar |
✅i-nplusone | Example of N+1 , support custom tracing |
✅j-sample | Example of split Query and Mutation into different configruation files to avoid too many definitions in one file |
k-postg | Example of supporting PostGraphile(Experimental)(TODO) |
✅l-interfaceunion | Example of interface and union |
✅m-dynamicschema | Example of dynamic schema |
❎n-webflux | Example of dynamic webflux, there are problems with Spring Security |
✅o-metrics | Example of metrics |
✅p-apollo-gateway ✅p-federation-customer ✅p-federation-name ✅p-federation-profile |
Apollo Federation Gateway |
✅o-metrics | Example of metrics |
✅x-kotlin | Example of Kotlin |
✅y-bff | Example of client and server,support voyager |
✅z-domain | Example of client and server,support voyager |
{
shows {
title
releaseYear
}
}
------
{
shows(titleFilter: "Ozark") {
title
releaseYear
}
}
------
{
showsWithDgsData {
id
title
releaseYear
actors {
name
}
}
}
------
{
user {
id
name
}
}
plugins {
id "com.netflix.dgs.codegen" version "5.1.17" apply false
}
plugins {
id "com.netflix.dgs.codegen"
}
generateJava{
schemaPaths = ["${projectDir}/src/main/resources/schema"] // List of directories containing schema files
packageName = 'com.codegen.graphqldgs' // The package name to use to generate sources
generateClient = true // Enable generating the type safe query API
}
{
shows {
id
title
releaseYear
}
}
------
{
shows(titleFilter: "Ozark") {
id
title
releaseYear
}
}
{
shows {
id
title
releaseYear
price
dateTime
bigDecimal
uuid
}
}
{
show(people: {name: "zhangsan"}) {
id
name
}
shows(personList: [{name: "zhangsan"}]) {
id
name
}
}
------
{
showWithGood {
id
name
}
}
------
{
showWithGood(good: {name: "Car"}) {
id
name
}
}
------
mutation {
addRating(title: "title", stars: 100) {
avgStars
}
addRatingWithInput(input: {title: "title", stars: 200}) {
avgStars
}
}
------
mutation {
addRating(title: "title", stars: 100) {
avgStars
}
addRatingWithInput(input: {title: "hel", stars: 200}) {
avgStars
}
}
Use Postman
to visit Subscription
curl
curl localhost:10005/graphql \
-F operations='{ "query": "mutation upload($file: Upload!) { upload(file: $file) }" , "variables": { "file": null } }' \
-F map='{ "0": ["variables.file"] }' \
-F [email protected]
------
curl localhost:10005/graphql \
-F operations='{ "query": "mutation addArtwork($file: Upload!) { addArtwork(file: $file) }" , "variables": { "file": null } }' \
-F map='{ "0": ["variables.file"] }' \
-F [email protected]
Please check
project uploaded-images
folder
{"data":{"upload":true}}
------
{"data":{"addArtwork":true}}
{
salary
}
------
{
salary
}
# REQUEST HEADERS中Input{ "Authorization": "Basic aHI6aHI=" },This is hr username and password
------
mutation {
updateSalary(salaryInput: {employeeId: "1", newSalary: "100"}) {
id
employeeId
newSalary
}
}
------
mutation {
updateSalary(salaryInput: {employeeId: "1", newSalary: "100"}) {
id
employeeId
newSalary
}
}
# REQUEST HEADERS中Input{ "Authorization": "Basic aHI6aHI=" },This is hr username and password
{
show(people: {name: "haha"}) {
id
name
}
}
------
{
show(people: {name: "zhangsan"}) {
id
name
}
}
------
{
getRating(id: "1") {
avgStars
}
}
see
test
folder
DemoControllerTests
and ShowDataFetcherTest
to check{
shows {
showId
title
reviews {
starRating
}
}
}
------
{
showsN {
id
title
releaseYear
artwork {
url
}
reviewsN {
username
starScore
submittedDate
}
}
}
{
movies {
__typename
... on ActionMovie {
title
nrOfExplosions
}
... on ScaryMovie {
title
gory
scareFactor
}
}
}
{
search {
__typename
... on Actor {
name
}
... on Series {
title
}
}
}
query randomNumber {
randomNumber(bound: 10)
}
------
mutation createUser {
createUser(username: "hello", password: "world") {
id
username
password
}
}
query getUsers {
getUsers {
id
username
password
}
getUserById(id: 1) {
id
username
password
}
}
mutation createUser {
createUser(username: "Trudy", password: "Trudy") {
id
username
password
}
}
visit http://localhost:10015/actuator/metrics to check output
Use docker-compose to start Grafana and Prometheus servers.
/build/libs
folderdocker-compose up -d
Check the Prometheus server.
Configure the Grafana.
admin
Start customer
,name
,profile
services
Start Apollo Gateway
npm install
node index.js
Visit http://localhost:4000
Variables is
{
"customerId": "1"
}
Query is
query Customer($customerId: String!) {
customer(customerId: $customerId) {
age
id
name {
firstName
fullName
lastName
middleName
prefix
}
profile {
email
phone
}
}
}
{
shows {
title
releaseYear
id
}
}
z-domain
,visit http://localhost:20000/graphiql
z-domain
,visit http://localhost:20000/voyager
{
shows {
id
title
releaseYear
}
}
------
mutation {
addShow(input: {title: "title", releaseYear: 2022}) {
id
title
releaseYear
}
}
{
shows {
id
title
releaseYear
}
}
------
mutation {
addShow(input: {title: "title", releaseYear: 2022}) {
id
title
releaseYear
}
}