Apollo GraphQL Client 'Network error: Can't find field XXXX on object undefined' with disableOffline: false

Apollo GraphQL Client 'Network error: Can't find field XXXX on object undefined' with disableOffline: false

我在使用 apollo-client 版本 2.4.6 查询我的 AWS AppSync 端点时遇到问题。
我可以使用 curl 命令成功查询 AWS AppSync 端点,但完全相同 在 Apollo 客户端上执行的 GraphQL 正在返回 "Can't find field getTickets on object undefined."

我是 GraphQL 和 Apollo 的新手。我是在做一些愚蠢的事情来导致那个错误吗?为什么会说 NetworkError?为什么对象未定义?

编辑:我注意到如果我在构造函数中将 disableOffline: true 传递给 AWSAppSyncClient 然后它开始工作。为什么?为什么 disableOffline: false 的默认客户端行为不起作用?

这是我在 AWS 部署的超级简单 schema.graphql:

schema {
  query: Query
}

type Query {
  getTickets: [EmmDDavidTickets]
  @aws_api_key
}

type EmmDDavidTickets @aws_api_key {
  ticketNumber: ID!
  pnrNumber: String
}

这是用于查询 AWS 端点的 curl 命令。请注意有效回复:

$ curl -X POST -H "x-api-key: --REDACTED--" https://wm3mz6anrjbrfpgbewnyyrio3u.appsync-api.us-east-1.amazonaws.com/graphql -d '{ "query": "query list {\ngetTickets { ticketNumber\n pnrNumber\n }\n}"}'

{"data":{"getTickets":[{"ticketNumber":"12345","pnrNumber":null},{"ticketNumber":"0001020202020","pnrNumber":"ABC123"}]}}

这是我使用 Apollo 执行相同查询的 NodeJS 代码:

const apiKey ='--REDACTED--';
const region = 'us-east-1';
const type = 'API_KEY';
const url = 'https://wm3mz6anrjbrfpgbewnyyrio3u.appsync-api.us-east-1.amazonaws.com/graphql';

const gql = require('graphql-tag');
const query = gql(`
query list {
  getTickets {
      ticketNumber
  }
}`);

// Set up Apollo client
const client = new AWSAppSyncClient({
    url: url,
    region: region,
    auth: {
        type: type,
        apiKey: apiKey,
    },
    disableOffline: false
});

client.hydrated().then(function (client) {
    //Now run a query
    client.query({ query: query })
        .then(function logData(data) {
            console.log('results of query: ', data);
        })
        .catch(console.error);
});

这是 Apollo 的错误响应:

ApolloError: Network error: Can't find field getTickets on object undefined.
    at new ApolloError (/Users/dyoung/workspace//appsync_javascript_test/node_modules/apollo-client/bundle.umd.js:85:32)
    at /Users/dyoung/workspace//appsync_javascript_test/node_modules/apollo-client/bundle.umd.js:1039:45
    at /Users/dyoung/workspace//appsync_javascript_test/node_modules/apollo-client/bundle.umd.js:1411:21
    at Array.forEach (<anonymous>)
    at /Users/dyoung/workspace//appsync_javascript_test/node_modules/apollo-client/bundle.umd.js:1410:22
    at Map.forEach (<anonymous>)
    at QueryManager.broadcastQueries (/Users/dyoung/workspace//appsync_javascript_test/node_modules/apollo-client/bundle.umd.js:1405:26)
    at /Users/dyoung/workspace//appsync_javascript_test/node_modules/apollo-client/bundle.umd.js:988:35 {
  graphQLErrors: [],
  networkError: Error: Can't find field getTickets on object undefined.
      at /Users/dyoung/workspace//appsync_javascript_test/node_modules/apollo-cache-inmemory/lib/bundle.umd.js:429:27
      at Array.forEach (<anonymous>)
      at StoreReader.diffQueryAgainstStore (/Users/dyoung/workspace//appsync_javascript_test/node_modules/apollo-cache-inmemory/lib/bundle.umd.js:426:36)
      at StoreReader.readQueryFromStore (/Users/dyoung/workspace//appsync_javascript_test/node_modules/apollo-cache-inmemory/lib/bundle.umd.js:401:25)
      at processOfflineQuery (/Users/dyoung/workspace//appsync_javascript_test/node_modules/aws-appsync/lib/link/offline-link.js:154:34)
      at /Users/dyoung/workspace//appsync_javascript_test/node_modules/aws-appsync/lib/link/offline-link.js:110:28
      at new Subscription (/Users/dyoung/workspace//appsync_javascript_test/node_modules/zen-observable/lib/Observable.js:183:34)
      at Observable.subscribe (/Users/dyoung/workspace//appsync_javascript_test/node_modules/zen-observable/lib/Observable.js:262:14)
      at /Users/dyoung/workspace//appsync_javascript_test/node_modules/aws-appsync/lib/client.js:182:67,
  message: "Network error: Can't find field getTickets on object undefined.",
  extraInfo: undefined
}

根据Apollo Docs on local state management

We need to write an initial state to the cache before the query is run to prevent it from erroring out.

当我没有在 Apollo 的 InMemoryCache 中初始化状态时,我抛出了几乎相同的错误。

A​​WSAppSyncClient的状态初始化,可以参考, and https://github.com/awslabs/aws-mobile-appsync-sdk-js/pull/96