Buscar pacotes, repositórios e categorias de apps

Repositório GitHubihorbokov/graphql_2_rest

graphql_2_rest

Uma maneira simples de converter consultas GraphQL em REST para usar qualquer cliente HTTP para realizar uma solicitação.

Capa do projeto graphql_2_rest
Estrelas
3
Forks
1
Último push (UTC)
6 de out. de 2024
Status do projeto
Ativo

README original

Texto original em inglês. Visite o GitHub para a versão atual.

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')
  ),
);
Ihor Bokov GitHub avatar
Autor no GitHub

Ihor Bokov ↗

Crazy Flutter Developer

Ukraine
LinguagensDartSwiftKotlinObjective-C

Pacotes publicados por este repositório

Dependências usadas

Lista de dependências 1 itens

Como escolher um repositório para seu projeto?

Filtre pelo caso de uso, compare a atividade e confira o projeto original antes de adotá-lo.

  1. Busque por nome, autor ou descrição e filtre por tema.
  2. Compare estrelas, forks e envios recentes. Estrelas indicam interesse, mas não garantem qualidade.
  3. Confira o README, a licença e as issues no GitHub antes de usar o projeto.

Fontes e manutenção

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗