为什么AWSAppSyncClient抛出错误网络错误:在lambda上离线时缺少optimisticResponse?

时间:2018-10-12 05:43:29

标签: amazon-web-services aws-lambda aws-appsync

lambda调用AWSAppSync突变。它的行为方式不稳定。当时它抛出下面提到的错误

  

错误:网络错误:离线时缺少optimisticResponse。

这是用于初始化AWSAppSyncClient客户端对象的代码段。

 client = new AWSAppSyncClient({
        url: settings.url,
        region: settings.region,
        auth: {
            type: type,
            apiKey: settings.apiKey,
        },
        disableOffline: false
    });

在发生突变时,lambda和AppSync之间的连接似乎丢失了。

 client.hydrated().then((client,error) => { 
         client.mutate({ 
                mutation: updateMutation, 
                variables: { 
                    ID: vehicle.VehicleID
             }
         });
 });

-在lambda中使用appsync的原因是进行突变,所有订阅者都将获得静音更新。

详细的错误日志

{ Error: Network error: Missing optimisticResponse while offline.
at new ApolloError (/var/task/node_modules/aws-appsync/node_modules/apollo-client/bundle.umd.js:124:32)
at Object.error (/var/task/node_modules/aws-appsync/node_modules/apollo-client/bundle.umd.js:1088:32)
at notifySubscription (/var/task/node_modules/zen-observable/lib/Observable.js:130:18)
at onNotify (/var/task/node_modules/zen-observable/lib/Observable.js:161:3)
at SubscriptionObserver.error (/var/task/node_modules/zen-observable/lib/Observable.js:220:7)
at notifySubscription (/var/task/node_modules/zen-observable/lib/Observable.js:130:18)
at flushSubscription (/var/task/node_modules/zen-observable/lib/Observable.js:112:5)
at /var/task/node_modules/zen-observable/lib/Observable.js:156:14
at /var/task/node_modules/zen-observable/lib/Observable.js:67:7
at <anonymous>
graphQLErrors: [],
networkError: Error: Missing optimisticResponse while offline.
at /var/task/node_modules/aws-appsync/lib/link/offline-link.js:80:35
at new Subscription (/var/task/node_modules/zen-observable/lib/Observable.js:179:34)
at Observable.subscribe (/var/task/node_modules/zen-observable/lib/Observable.js:258:14)
at /var/task/node_modules/aws-appsync/lib/client.js:151:55
at <anonymous>,
message: 'Network error: Missing optimisticResponse while offline.',
extraInfo: undefined }

1 个答案:

答案 0 :(得分:0)

调用突变的正确方法是在突变方法中包含optimisticReponse选项,如下所示。

client.mutate({ 
                mutation: updateMutation, 
                variables: tempVehicle,
                optimisticResponse: () => ({ 
                    updateDslvehicleStateMutation: 
                    {
                        ID: tempVehicle.ID,
                        OPP: tempVehicle.OPP,
                        CDC: tempVehicle.CDC,
                        MND: tempVehicle.MND,
                        loc: tempVehicle.loc,
                        CSP: tempVehicle.CSP,
                        __typename: 'UpdateVehicleInput'
                    }
                })
            })

如果无法在mutate方法中添加optimisticResponse,将无法在Appsync中执行突变。