Search packages, repositories and app categories

Package detailsv2.0.0

graphql_2_rest

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

AndroidiOSWebmacOSWindowsLinux
30-day downloads
522
Likes
64
Pub points
160

Install package

flutter pub add graphql_2_rest

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

A lightweight Flutter/Dart library that converts GraphQL queries into REST-compatible HTTP requests. It enables developers to use standard HTTP clients for executing GraphQL operations without requiring a dedicated GraphQL client. This simplifies integration with existing REST-based infrastructure and tools, especially in environments where GraphQL clients are not available or preferred. The package supports all major platforms, making it ideal for cross-platform applications needing flexible API communication.

Use cases

  • Converting GraphQL queries to REST for legacy systems
  • Using standard HTTP clients with GraphQL data
  • Simplifying API integration in constrained environments
  • Enabling GraphQL usage in apps with strict HTTP client policies
  • Supporting hybrid REST-GraphQL backend architectures

Key features

  • Lightweight and minimal dependency
  • Automatic query transformation from GraphQL to REST
  • Cross-platform compatibility
  • Works with any HTTP client
  • Simple integration via pub add command

Suitable for

  • Developers using REST clients but needing GraphQL data
  • Teams migrating from REST to GraphQL gradually
  • Projects with strict dependency constraints
  • Applications targeting multiple platforms
  • Use cases requiring flexible API layer design

Considerations

  • Limited to basic GraphQL query support
  • No built-in error handling or caching
  • Does not support mutations or subscriptions
  • Requires manual management of headers and authentication
  • Not suitable for complex GraphQL schemas

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

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')
  ),
);

Categories

Source repository

Projects using this package

No data available yet.