首页 > 解决方案 > Apollo 以 GET 形式发送订阅

问题描述

我的技术设置如下:

GraphQl 通过阿波罗

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 调用。任何人都可以帮助我/知道这里有什么问题吗?

标签: androidkotlingraphqlapolloapollo-android

解决方案


推荐阅读