解析类型为Hasura.GraphQL.Transport.HTTP.Protocol.GQLReq的构造函数GQLReq预期的对象但得到String

时间:2020-04-12 12:21:20

标签: flutter hasura built-value chopper

我正在尝试使用Flutter,Chopper和Built Value向Hasura后端提出请求,但出现以下错误

> When parsing the constructor GQLReq of type Hasura.GraphQL.Transport.HTTP.Protocol.GQLReq expected Object but got String.,

斩波器服务

@ChopperApi(baseUrl: '/v1/graphql')
abstract class PostApiService extends ChopperService {
  @Post()
  Future<Response<BuiltPost>> get(@Body() String body);

  static PostApiService create(AuthHeaderProvider authHeaderProvider) {
    final client = ChopperClient(
      baseUrl: 'https://arrivee-app-test.herokuapp.com',
      services: [
        _$PostApiService(),
      ],
      converter: BuiltValueConverter(),
      interceptors: [
        HttpLoggingInterceptor(),
     //   HeadersInterceptor({'content-type': 'application/json'}),
        HeadersInterceptor({'Authorization':'Bearer token'})
      ],
    );
    return _$PostApiService(client);
  }
}

我使用以下代码进行请求

    var request = RequestModel((b) => b
  ..query = fetchAccommodations()
  ..variables = null
  ..operationName = 'AccommodationGet');

var response = await client.get(request.toJson());

RequestModel

    abstract class RequestModel
    implements Built<RequestModel, RequestModelBuilder> {
  String get query;

  @nullable
  String get variables;

  String get operationName;

  RequestModel._();

  factory RequestModel([updates(RequestModelBuilder b)]) = _$RequestModel;

  String toJson() {
    return json
        .encode(serializers.serializeWith(RequestModel.serializer, this));
  }

  static RequestModel fromJson(String jsonString) {
    return serializers.deserializeWith(
        RequestModel.serializer, json.decode(jsonString));
  }

  static Serializer<RequestModel> get serializer => _$requestModelSerializer;
}

2 个答案:

答案 0 :(得分:0)

我设法解决了。在此处发布以供将来参考。 我让斩波器执行从模型到字符串的转换,而不是事先手动进行 我从更改了斩波器服务的签名:

 @Post()
  Future<Response<BuiltPost>> get(@Body() String body);

@Post()
Future<Response<BuiltPost>> get(@Body() RequestModel body);

并从以下位置调用服务:

 var request = RequestModel((b) => b
  ..query = fetchAccommodations()
  ..variables = null
  ..operationName = 'AccommodationGet');

var response = await client.get(request.toJson());

 var request = RequestModel((b) => b
      ..query = fetchAccommodations()
      ..variables = null
      ..operationName = 'AccommodationGet');

var value = await client.get(request);

答案 1 :(得分:0)

尝试在Angular Dart中对Hasura进行简单的HTTP POST调用时遇到相同的错误(没有GraphQL客户端可用)。

我发现必须以以下方式构建Json字符串:

   String query = """
      {
        account {
            id
          }
      }
    """;

    Map<String, dynamic> variables;

    if (query.trimLeft().split(' ')[0] != 'query') {
      query = 'query $docQuery';
    }

    var jsonMap = {'query': query, 'variables': variables};

    final _headers = {'Content-Type': 'application/json'};
    return _authHttpService.post(_apiUrl,
        headers: _headers, body: json.encode(jsonMap));

希望它可以帮助任何寻求香草Dart解决方案的人。