知識庫 / Spring / Spring Boot RSS 訂閱

領域圖服務(DGS)框架介紹

Spring Boot
HongKong
6
12:21 PM · Dec 06 ,2025

1. 概述

近年來,客户端/服務器通信領域最顯著的轉變之一是 GraphQL,一種開源查詢語言和運行時,用於操作 API。我們可以使用它來請求我們所需的確切數據,從而限制我們需要的請求數量。

Netflix 創建了一個領域圖服務框架 (DGS) 服務器框架,以簡化開發。在本快速教程中,我們將涵蓋 DGS 框架的關鍵功能。我們將看到如何將此框架添加到我們的應用程序,並檢查其基本註釋的工作方式。要了解更多關於 GraphQL 本身的知識,請查看我們的《GraphQL 簡介》文章。

2. 領域圖服務框架 (Domain Graph Service Framework)

Netflix DGS (Domain Graph Service) 是一個使用 Kotlin 編寫,基於 Spring Boot 的 GraphQL 服務器框架。它旨在儘可能減少外部依賴,僅依賴 Spring 框架。

Netflix DGS 框架使用基於 Spring Boot 的註解驅動的 GraphQL Java 庫。 此外,它還提供了一些有用的功能:它允許從 GraphQL 模式生成源代碼。 總結一下關鍵特性:

  • 註解驅動的 Spring Boot 編程模型
  • 用於編寫查詢測試的測試框架,作為單元測試
  • Gradle/Maven 代碼生成插件,用於從模式創建類型
  • 與 GraphQL Federation 的易於集成
  • 與 Spring Security 的集成
  • GraphQL 訂閲 (WebSockets 和 SSE)
  • 文件上傳
  • 錯誤處理
  • 許多擴展點

3. 配置

首先,由於 DGS 框架基於 Spring Boot,我們創建一個 Spring Boot 應用。然後,我們將 DGS 依賴項 添加到我們的項目中:

<dependency>
    <groupId>com.netflix.graphql.dgs</groupId>
    <artifactId>graphql-dgs-spring-boot-starter</artifactId>
    <version>4.9.16</version>
</dependency>

4. Schema

This section describes the schema used for the data. The schema defines the structure and data types of the information stored. Understanding the schema is crucial for interpreting and utilizing the data effectively.

The schema consists of the following key components:

  • Entities: These represent the core objects in the system (e.g., User, Product, Order).
  • Attributes: Each entity has a set of attributes that describe its properties (e.g., User.name, Product.price).
  • Relationships: These define how entities are related to each other (e.g., a User can place multiple Orders).
// This code defines the basic structure of the schema.
// It uses a JSON-like format to represent the entities and their relationships.
const schema = {
  "User": {
    "name": "string",
    "email": "string",
    "orders": ["Order"]
  },
  "Order": {
    "order_id": "integer",
    "user_id": "integer",
    "total_amount": "number"
  }
};

4.1. 開發方法

DGS 框架支持兩種開發方法:基於模式和基於代碼。 但推薦的方法是基於模式,主要因為這種方法更容易跟上數據模型的變化。基於模式意味着我們首先定義 GraphQL 服務的模式,然後通過匹配模式中的定義來實現代碼。框架默認會從 src/main/resources/schema 文件夾中獲取任何模式文件。

4.2. 實現

讓我們使用 GraphQL 模式定義語言 (SDL) 為我們的示例應用程序創建一個簡單的 GraphQL 模式:

type Query {
    albums(titleFilter: String): [Album]
}

type Album {
    title: String
    artist: String
    recordNo: Int
}

該模式允許查詢專輯列表,並可選地按標題進行過濾。

5. 基本標註

讓我們從創建與我們的模式相對應的 Album 類開始:

public class Album {
    private final String title;
    private final String artist;
    private final Integer recordNo;

    public Album(String title, String artist, Integer recordNo) {
        this.title = title;
        this.recordNo = recordNo;
        this.artist = artist;
    }

    // standard getters
}

5.1. 數據獲取器

數據獲取器負責返回查詢的數據。`以下 @DgsQuery, @DgsMutation, @DgsSubscription 標註 是簡寫,用於在 Query, Mutation, Subscription 類型上定義數據獲取器。 所有 提到的標註等效於 @DgsData 標註。我們可以使用這些標註在一個 Java 方法上,將該方法變成一個數據獲取器,並定義一個帶有參數的類型。

5.2. 實現

因此,為了定義 DGS 數據獲取器,我們需要在 @DgsComponent 類中創建一個查詢方法。我們希望查詢一個 Albums 列表,所以讓我們用 @DgsQuery 標記該方法:

private final List<Album> albums = Arrays.asList(
  new Album("Rumours", "Fleetwood Mac", 20),
  new Album("What's Going On", "Marvin Gaye", 10), 
  new Album("Pet Sounds", "The Beach Boys", 12)
  );

@DgsQuery
public List<Album> albums(@InputArgument String titleFilter) {
    if (titleFilter == null) {
        return albums;
    }
    return albums.stream()
      .filter(s -> s.getTitle().contains(titleFilter))
      .collect(Collectors.toList());
}

我們還為方法的參數標記了 @InputArgument 註解。此註解將使用方法參數的名稱與查詢中發送的輸入參數的名稱進行匹配。

6. Code-Gen 插件

DGS 還包含一個 Code-Gen 插件,用於從 GraphQL Schema 生成 Java 或 Kotlin 代碼。代碼生成通常與構建集成在一起。

DGS 代碼生成插件適用於 Gradle 和 Maven。該插件在我們的項目構建過程中,根據 Domain Graph Service 的 GraphQL 模式文件生成代碼。該插件可以生成數據類型,包括類型、輸入類型、枚舉和接口,以及樣例文檔檢索器和類型安全的查詢 API。 此外,還有一個 DgsConstants 類,其中包含類型和字段的名稱。

7. 測試

使用我們的 API 的便捷方式是 GraphiQLGraphiQL 是 DGS 框架自帶的查詢編輯器。 讓我們從默認的 Spring Boot 端口啓動我們的應用程序,並檢查 URL  http://localhost:8080/graphiql。 讓我們嘗試以下查詢並測試結果:

{
    albums{
        title
    }
}

請注意,與 REST 相比,我們必須明確指定要從查詢中返回哪些字段。下面我們來看一下響應:

{
  "data": {
    "albums": [
      {
        "title": "Rumours"
      },
      {
        "title": "What's Going On"
      },
      {
        "title": "Pet Sounds"
      }
    ]
  }
}

8. 結論

Domain Graph Service Framework 是一種簡單且引人注目的使用 GraphQL 的方式。它利用高級構建塊來處理查詢執行等操作。DGS 框架通過便捷的 Spring Boot 編程模型提供了所有這些功能。本文檔將介紹該框架的一些有用的特性。

user avatar
0 位用戶收藏了這個故事!
收藏

發佈 評論

Some HTML is okay.