首页 > 解决方案 > Apollo GraphQL 客户端“网络错误:在未定义的对象上找不到字段 XXXX”,disableOffline:false

问题描述

我在使用 apollo-client 版本 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
}

这是用于在 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-clientaws-appsync

解决方案


根据Apollo Docs 关于本地状态管理

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

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

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


推荐阅读