搜索插件、代码仓与 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')
  ),
);

所属分类

源码仓库

使用此插件的项目

暂无相关数据。