我的技术设置如下:
GraphQl通过Apollo
AWS作为后端
用Kotlin编写的原生Android应用
GraphQl查询和变异可以正常工作。
我有以下问题:
创建订阅时,执行将生成GET而不是POST。导致后端错误说:
<-- 400 Bad Request aws.url/graphql (152ms)
Content-Type: application/json;charset=UTF-8
Content-Length: 135
Connection: close
Date: Wed, 05 Jun 2019 12:40:04 GMT
x-amzn-RequestId: id
x-amzn-ErrorType: MalformedHttpRequestException
X-Cache: Error from cloudfront
Via: 1.1 cloudfront.url (CloudFront)
X-Amz-Cf-Pop: pop
X-Amz-Cf-Id: id
{
"errors" : [ {
"message" : "Invalid request, `query` can't be null.",
"errorType" : "MalformedHttpRequestException"
} ]
}
这是我的代码:
创建客户端:
val logger = HttpLoggingInterceptor()
logger.level = HttpLoggingInterceptor.Level.HEADERS
val blogger = HttpLoggingInterceptor()
blogger.level = HttpLoggingInterceptor.Level.BODY
val client = OkHttpClient.Builder()
.addInterceptor(blogger)
.authenticator(get())
.build()
ApolloClient.builder()
.serverUrl(BuildConfig.backendUrl)
.okHttpClient(client)
.subscriptionTransportFactory(WebSocketSubscriptionTransport.Factory(BuildConfig.backendUrl, client))
.build() as ApolloClient
订阅
override suspend fun subscribeToVehiclePosition(vehicleId: String, listener: DataRegistration.Listener<SubscribeToVehicleSubscription.Data>): DataRegistration {
val registration = RemoteDataRegistration()
authenticatedClient.subscribe(SubscribeToVehicleSubscription.builder().id(vehicleId).build()).execute(object: ApolloSubscriptionCall.Callback<SubscribeToVehicleSubscription.Data> {
override fun onFailure(e: ApolloException) {
listener.onClose(e)
}
override fun onResponse(response: Response<SubscribeToVehicleSubscription.Data>) {
val data = response.data()
if (data != null) {
listener.onData(data)
}
}
override fun onTerminated() {
listener.onClose(IllegalStateException("Connection Terminated!!"))
}
override fun onCompleted() {
listener.onCompleted()
}
})
return registration
}
graphql定义
subscription subscribeToVehicle($id: String!) {
subscribeToCreateVehiclePositionLog(vehicleId: $id) {
lat
lng
date
tripId
}
}
架构
{
"kind": "OBJECT",
"name": "Subscription",
"description": null,
"fields": [
{
"name": "subscribeToCreateVehiclePositionLog",
"description": null,
"args": [
{
"name": "vehicleId",
"description": null,
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"defaultValue": null
}
],
"type": {
"kind": "OBJECT",
"name": "VehiclePositionLog",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [],
"enumValues": null,
"possibleTypes": null
},
为什么将订阅称为GET调用,我有点迷惑。 有人可以帮我/不知道这是怎么回事吗?