Apollo GraphQL客户端“网络错误:在未定义的对象上找不到字段XXXX”,带有disableOffline:假

时间:2019-10-10 14:59:36

标签: apollo-client aws-appsync

使用apollo客户端版本2.4.6来查询我的AWS AppSync终端节点时遇到问题。
我可以使用curl命令成功查询AWS AppSync终端节点,但完全相同 通过Apollo客户端执行的GraphQL返回“无法在未定义的对象上找到字段getTickets。”

我是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
}

这是curl命令,用于查询AWS上的该端点。注意有效的响应:

$ 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"}]}}

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

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
}

1 个答案:

答案 0 :(得分:1)

根据Apollo Docs on local state management

  

我们需要在运行查询之前将初始状态写入缓存   以防止其出错。

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

要初始化AWSAppSyncClient的状态,您可以参考React-Native + Apollo-Link-State + AWS Appsync : Defaults are not stored in cachehttps://github.com/awslabs/aws-mobile-appsync-sdk-js/pull/96