1

I am having trouble using apollo-client version 2.4.6 to query my AWS AppSync endpoint.
I can successfully query the AWS AppSync endpoint using a curl command, but the exact same GraphQL executed over the Apollo client is returning "Can't find field getTickets on object undefined."

I am a newby at GraphQL and Apollo. Am I doing something stupid to cause that error? Why does it say NetworkError? Why is the object undefined?

EDIT: I noticed that if I pass disableOffline: true in the constructor to AWSAppSyncClient then it starts working. Why? Why is the default client behavior with disableOffline: false not working?

Here is my super simple schema.graphql deployed at AWS:

schema {
  query: Query
}

type Query {
  getTickets: [EmmDDavidTickets]
  @aws_api_key
}

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

Here is the curl command that works to query that endpoint at AWS. Note the valid response:

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

Here is my NodeJS code to execute the same query using 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);
});

Here is the error response from 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
}
davidgyoung
  • 63,876
  • 14
  • 121
  • 204

1 Answers1

2

According 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.

I had the almost same error thrown out when I did not initialise the state in the Apollo's InMemoryCache.

To initialize the state for AWSAppSyncClient, you can refer to React-Native + Apollo-Link-State + AWS Appsync : Defaults are not stored in cache, and https://github.com/awslabs/aws-mobile-appsync-sdk-js/pull/96

Yuci
  • 27,235
  • 10
  • 114
  • 113
  • 1
    Thanks for the answer. I never would have guessed this. I think iti is crazy for the default usage of the API to require such cache priming without throwing an error that does not indicate what you need to do. – davidgyoung Dec 16 '19 at 11:51
  • This issue comment is also worth noting https://github.com/awslabs/aws-mobile-appsync-sdk-js/issues/453#issuecomment-520246437, `redux-offline` will stuck when `auth.jwtToken` throws. – Vicary Sep 15 '20 at 05:35