awslabs / aws-mobile-appsync-sdk-js

JavaScript library files for Offline, Sync, Sigv4. includes support for React Native
Apache License 2.0
917 stars 265 forks source link

Stale apollo-cache-inmemory causes error on Lambda when using AWSAppSyncClient #691

Open katesclau opened 2 years ago

katesclau commented 2 years ago

Report a bug

What is the current behavior?

Using aws-appsync in Lambdas (ie.: AWSAppSyncClient) makes lambda execution fail with error:

ERROR   Invoke Error     {"errorType":"TypeError","errorMessage":"Cannot convert undefined or  null to object","stack":["TypeError: Cannot convert undefined or null to  object","    at Function.keys (<anonymous>)","    at  /opt/nodejs/node_modules/apollo-cache-inmemory/lib/bundle.umd.js:331:12","     at  /opt/nodejs/node_modules/apollo-cache-inmemory/lib/bundle.umd.js:2:68","     at Object.<anonymous>  (/opt/nodejs/node_modules/apollo-cache-inmemory/lib/bundle.umd.js:5:2)","     at Module._compile (internal/modules/cjs/loader.js:999:30)","    at  Object.Module._extensions..js  (internal/modules/cjs/loader.js:1027:10)","    at Module.load  (internal/modules/cjs/loader.js:863:32)","    at Function.Module._load  (internal/modules/cjs/loader.js:708:14)","    at Module.require  (internal/modules/cjs/loader.js:887:19)","    at require  (internal/modules/cjs/helpers.js:74:18)"]}

Caused by apollo-cache-inmemory:1.3.12 @apollo-cache-inmemory/lib/bundle.umd.js

...
    // visitor || visitor.QueryDocumentKeys may be undefined
    Object.keys(visitor.QueryDocumentKeys).forEach(function (parentKind) {
        var childKeys = queryKeyMap[parentKind] = Object.create(null);
        visitor.QueryDocumentKeys[parentKind].forEach(function (childKey) {
            childKeys[childKey] = true;
        });
        if (parentKind === "FragmentSpread") {
            childKeys["fragment"] = true;
        }
    });
...

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem.

ie.:

exports.handler = async (event) => {
  require("isomorphic-fetch");
  const gql = require("graphql-tag");
  const AWSAppSyncClient = require("aws-appsync").default;

  const appSyncClient = new AWSAppSyncClient({
    url: process.env.API_ENDPOINT,
    region: process.env.AWS_REGION,
    auth: {
      type: "API_KEY",
      apiKey: process.env.API_KEY,
    },
    fetchPolicy: "network-only",
    disableOffline: true,
  });

  const response = await appSyncClient
    .query({ query: gql`query foo{ foo }` })
    .catch((error) => {
      throw new Error(error);
    });
 console.log({ response });
 return response;
}

What is the expected behavior? logs and returns { data: { foo: "bar" } }

Which versions and which environment (nodejs) / OS are affected by this issue? Did this work in previous versions? Running on AWS Lambda

eugen-maser commented 2 years ago

Got the same problem!

Is there an older version which works properly?

eugen-maser commented 2 years ago

With the following dependency versions, it works!

"dependencies": { "aws-appsync": "^4.1.4", "cross-fetch": "^3.1.4", "graphql": "^15.7.0", "graphql-tag": "^2.12.5" }

Do not use newer "graphql" version.

katesclau commented 2 years ago

Works for me, thanks @eugen-maser :bow:

hahaha0404 commented 2 years ago

Worked for me, thanks @eugen-maser 🙇

DylanBruzenak commented 2 years ago

The key thing here appears to be the "graphql": "^15.7.0" line. That's all it took to fix things for me.

NevRA commented 2 years ago

Thanks. Still working!

JetimLee commented 2 years ago

This doesn't fix it for me. Any suggestions?

sators commented 1 year ago

"graphql": "^15.7.0" worked for me! Thanks!