Giter Site home page Giter Site logo

netflix-dgs-example-java's Introduction

DGS

module说明

Module 说明
✅a-start 简单的使用,使用多个*.graphqls,举例@DgsData.List
✅b-codegen 使用codegen,多module,type中带方法,使用常量在@DgsData指定parentType和field,添加@RequestHeader
✅c-scalar 支持自定义类型
✅d-http 支持Query,Mutation,Subscription,参数校验,支持Apollo Tracing
✅e-file 支持文件上传下载
✅f-auth 支持认证和授权
✅g-error 支持自定义错误类型
✅h-ut 支持单元测试,集成测试,支持自定义类型(custom scalar)的单元测试
✅i-nplusone 解决N+1的问题,支持自定义Tracing
✅j-sample 将Query和Mutation的配置分解到各个配置文件中,避免出现请求方法的爆炸
k-federation 使用Federation作为统一的入口(TODO)
✅y-bff 支持Client和Server,支持voyager的description,支持https://github.com/APIs-guru/graphql-voyager
✅z-domain 支持Client和Server,支持https://github.com/APIs-guru/graphql-voyager

Intellij Idea Plugin的安装

问题

  1. 是否支持多个*.graphqls文件?

支持,请查看a-start

  1. 减少代码编写量是否支持?例如只编写*.graphqls,Request和Response自动生成

支持,请查看b-codegen

  1. 自定义类型如何支持?例如Long,BigDecimal,UUID

请查看c-scalar,支持自定义类型,也搭配了codegen进行使用

  1. 是否支持HTTP的所有方法?参数校验如何支持?

支持,请查看d-http

参数校验:列表,重命名,Optional,Validation

方法:Query,Mutation

  1. 文件上传MultiPartFile如何支持?

请查看e-file

请查看https://github.com/jaydenseric/graphql-multipart-request-spec

  1. 认证和授权如何支持?

请查看f-auth

  1. 错误类型如何支持?

请查看g-error

  1. 单元测试如何支持?

请查看h-ut

  1. N+1问题如何支持?

请查看i-nplusone

  1. GraphQL作为Client调用提供GraphQL的Server如何支持?

请查看y-bffz-domain

  1. type中添加method是否支持?

查看b-codegen

  1. 如何获取HttpServletRequest ?
@DgsQuery
public String user(DgsDataFetchingEnvironment dfe) {
    DgsWebMvcRequestData requestData = (DgsWebMvcRequestData) dfe.getDgsContext().getRequestData();
    ServletWebRequest webRequest = (ServletWebRequest) requestData.getWebRequest();
    HttpServletRequest httpServletRequest = webRequest.getRequest();
    return "";
}
  1. Tracing是否支持?

查看d-httpi-nplusone

  1. WebSocket是否支持?

支持,请查看d-http

  1. file类型的download是否支持?

因为GraphQL的Response是作为JSON,所以无法使用Binary作为类型,解决方案有如下两种:

  1. 将file转为BASE64 String,写入到Response的data中(不推荐)
  2. 将file上传至文件服务器,返回文件的URL

a-start

{
    shows {
        title
        releaseYear
    }
}
------
{
  shows(titleFilter: "Ozark") {
    title
    releaseYear
  }
}
------
{
  showsWithDgsData {
    id
    title
    releaseYear
    actors {
      name
    }
  }
}
------
{
  user {
    id
    name
  }
}

b-codegen

  • root的build.gradle添加
plugins {
    id "com.netflix.dgs.codegen" version "5.1.17" apply false
}
  • module的build.gradle添加
plugins {
    id "com.netflix.dgs.codegen"
}
  • module的build.gradle添加并运行
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
  }
}

c-scalar

{
  shows {
    id
    title
    releaseYear
    price
    dateTime
    bigDecimal
    uuid
  }
}

d-http

{
  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
  }
}

使用Postman访问Subscription img.png

e-file

  • 启动
  • 通过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]

  • 输出

第二个请求之后查看project下面的uploaded-images文件

{"data":{"upload":true}}
------
{"data":{"addArtwork":true}}

f-auth

{
  salary
}
------
{
  salary
}
# REQUEST HEADERS中输入{ "Authorization": "Basic aHI6aHI=" },这里对应hr的用户名和密码
------
mutation {
  updateSalary(salaryInput: {employeeId: "1", newSalary: "100"}) {
    id
    employeeId
    newSalary
  }
}
------
mutation {
  updateSalary(salaryInput: {employeeId: "1", newSalary: "100"}) {
    id
    employeeId
    newSalary
  }
}
# REQUEST HEADERS中输入{ "Authorization": "Basic aHI6aHI=" },这里对应hr的用户名和密码

g-error

{
  show(people: {name: "haha"}) {
    id
    name
  }
}
------
{
  show(people: {name: "zhangsan"}) {
    id
    name
  }
}
------
{
  getRating(id: "1") {
    avgStars
  }
}

h-ut

查看test文件夹

  • 运行DemoControllerTestsShowDataFetcherTest查看效果

i-nplusone

{
  shows {
    showId
    title
    reviews {
      starRating
    }
  }
}
------
{
  showsN {
    id
    title
    releaseYear
    artwork {
      url
    }
    reviewsN {
      username
      starScore
      submittedDate
    }
  }
}

y-bff

{
  shows {
    id
    title
    releaseYear
  }
}
------
mutation {
  addShow(input: {title: "title", releaseYear: 2022}) {
    id
    title
    releaseYear
  }
}

z-domain

{
  shows {
    id
    title
    releaseYear
  }
}
------
mutation {
  addShow(input: {title: "title", releaseYear: 2022}) {
    id
    title
    releaseYear
  }
}

netflix-dgs-example-java's People

Contributors

springmonster avatar tyzzt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.