我正在尝试使用 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());
请求模型
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;
}