搜尋套件、儲存庫與 APP 分類

套件詳情v2.0.0

graphql_2_rest

一個輕量級庫,用於將 GraphQL 查詢轉換為 REST 格式,讓您能使用任何 HTTP 客戶端執行請求。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
522
喜歡數
64
Pub 分數
160

安裝套件

flutter pub add graphql_2_rest

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個輕量級的 Flutter/Dart 庫,可將 GraphQL 查詢轉換為相容 REST 的 HTTP 請求。它讓開發者能夠使用標準 HTTP 客戶端執行 GraphQL 操作,而無需專用的 GraphQL 客戶端。這簡化了與現有基於 REST 的基礎設施和工具的整合,特別是在無法使用或不偏好 GraphQL 客戶端的環境中。該套件支援所有主要平台,非常適合需要靈活 API 通訊的跨平台應用。

適用情境

  • 將 GraphQL 查詢轉換為 REST 以用於舊系統
  • 使用標準 HTTP 客戶端取得 GraphQL 數據
  • 在資源受限環境中簡化 API 集成
  • 在具有嚴格 HTTP 客戶端策略的應用程式中啟用 GraphQL 使用
  • 支援混合 REST-GraphQL 後端架構

主要特色

  • 輕量且依賴極少
  • 從 GraphQL 到 REST 的自動查詢轉換
  • 跨平台相容性
  • 適用於任何 HTTP 客戶端
  • 透過 pub add 命令進行簡單整合

適合對象

  • 使用 REST 客戶端但需要 GraphQL 資料的開發者
  • 從 REST 迁移到 GraphQL 的團隊逐步進行
  • 具有嚴格依賴約束的專案
  • 面向多個平台的應用程式
  • 需要靈活API層設計的用例

使用建議

  • 僅支援基本的 GraphQL 查詢
  • 無內建錯誤處理或快取
  • 不支援變更或訂閱
  • 需要手動管理標頭和驗證
  • 不適用於複雜的GraphQL模式

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

GraphQL to REST

Pub style: very good analysis License: MIT

A lightweight library for converting GraphQL queries into REST format, enabling the use of any HTTP client for executing requests.

Usage

To work effectively with the graphql_2_rest library, you primarily need to use two key components: GraphQLQueryPayload and GraphQLQueryBuilder.

  1. GraphQLQueryPayload is responsible for providing the arguments and input models to be used in a GraphQL query. For example, if you have queries like:
const queryA = '''query {
  user(age: %age, firstName: %firstName) {
    first_name
    last_name
    site {
      site_name
    }
  }
}''';

const queryB = '''query {
  user(@userInput) {
    first_name
    last_name
    site {
      site_name
    }
  }
}''';

then you need to create the following GraphQLQueryPayloads with arguments or/and inputs:

class UserQueryPayloadA with GraphQLQueryPayload {
  const UserQueryPayloadA(
    this.age,
    this.firstName,
  );

  final int age;
  final String firstName;

  @override
  Map<String, dynamic> get arguments {
    return {
      'age': age,
      'firstName': firstName,
    };
  }
}

class UserQueryPayloadB with GraphQLQueryPayload {
  const UserQueryPayloadB(
    this.age,
    this.firstName,
  );

  final int age;
  final String firstName;

  @override
  Map<String, Map<String, dynamic>> get inputs {
    return {
      'userInput': {
        'age': age,
        'firstName': firstName,
      },
    };
  }
}
  1. GraphQLQueryBuilder automatically replaces all argument placeholders in the query using values provided by the GraphQLQueryPayload. By default, GraphQLQueryBuilder uses the following prefixes:
  • % for arguments
  • @ for input models

These default prefixes can be customized by specifying different values for argumentPrefix and inputPrefix when creating an instance of the class.

After building the query, you can use any HTTP client to perform the request, but keep in mind that the request must use the POST method:

final dio = Dio(BaseOptions(baseUrl: 'https://endpoint/'));

final queryBuilder = GraphQLQueryBuilder(
  transformer: (query) => query.replaceAll(RegExp(r'\s+'), ' ').trim(),
);

dio.post<dynamic>(
  'graphql/',
  data: queryBuilder.build(
    queryA, //or queryB
    UserQueryPayloadA(27, 'John'), //or UserQueryPayloadB(27, 'John')
  ),
);

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。