aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.13k forks source link

require("aws-appsync") fails in Lambda function: message: "Cannot convert undefined or null to object". #9591

Closed duckbytes closed 2 years ago

duckbytes commented 2 years ago

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

GraphQL API

Amplify Categories

function, api

Environment information

``` # Put output below this line System: OS: Linux 5.17 Arch Linux CPU: (16) x64 AMD Ryzen 7 3700X 8-Core Processor Memory: 3.81 GB / 31.26 GB Container: Yes Shell: 5.8.1 - /bin/zsh Binaries: Node: 16.13.2 - ~/.nvm/versions/node/v16.13.2/bin/node Yarn: 1.22.17 - /usr/bin/yarn npm: 8.1.2 - ~/.nvm/versions/node/v16.13.2/bin/npm Browsers: Firefox: 97.0 npmGlobalPackages: @aws-amplify/cli: 7.6.15 corepack: 0.10.0 npm: 8.1.2 ```

Describe the bug

Attempting to require("aws-appsync") causes this error to return:

Lambda:Unhandled", message: "Cannot convert undefined or null to object"

I'm following the guide and using the example code from here: https://aws.amazon.com/blogs/mobile/supporting-backend-and-internal-processes-with-aws-appsync-multiple-authorization-types/

If I do the same import locally in a node shell, using the same version of aws-appsync the import works fine in node 14 and node 16.

amplify mock function also fails with the same error

Expected behavior

The Lambda function should run without error.

Reproduction steps

Code Snippet

index.js:

// Put your code below this line.

/* Amplify Params - DO NOT EDIT
    API_PLATELET_GRAPHQLAPIENDPOINTOUTPUT
    API_PLATELET_GRAPHQLAPIIDOUTPUT
    AUTH_PLATELET61A0AC07_USERPOOLID
    ENV
    REGION
Amplify Params - DO NOT EDIT */

require("isomorphic-fetch");
const AUTH_TYPE = require('aws-appsync').AUTH_TYPE;
const AWSAppSyncClient = require('aws-appsync').default;

exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        //  Uncomment below to enable CORS requests
        //  headers: {
        //      "Access-Control-Allow-Origin": "*",
        //      "Access-Control-Allow-Headers": "*"
        //  },
        body: JSON.stringify("hello"),
    };
    return response;
};

In my GraphQL schema:

type Mutation {
   registerUser(name: String, email: String, tenantId: ID, roles: [Role]): String @function(name: "plateletAdminAddNewUser-${env}")
 }

In my React app:

                await API.graphql(
                    graphqlOperation(mutations.registerUser, {
                        name: state.name,
                        email: state.email,
                        roles: rolesState,
                        tenantId,
                    })
                );

Log output

``` // Put your logs below this line ``` 2022-02-15T15:55:46.334Z undefined ERROR Uncaught Exception { "errorType": "TypeError", "errorMessage": "Cannot convert undefined or null to object", "stack": [ "TypeError: Cannot convert undefined or null to object", " at Function.keys ()", " at /var/task/node_modules/apollo-cache-inmemory/lib/bundle.umd.js:331:12", " at /var/task/node_modules/apollo-cache-inmemory/lib/bundle.umd.js:2:68", " at Object. (/var/task/node_modules/apollo-cache-inmemory/lib/bundle.umd.js:5:2)", " at Module._compile (internal/modules/cjs/loader.js:1085:14)", " at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)", " at Module.load (internal/modules/cjs/loader.js:950:32)", " at Function.Module._load (internal/modules/cjs/loader.js:790:12)", " at Module.require (internal/modules/cjs/loader.js:974:19)", " at require (internal/modules/cjs/helpers.js:93:18)" ] } ``` ```

aws-exports.js

/* eslint-disable */
// WARNING: DO NOT EDIT. This file is automatically generated by AWS Amplify. It will be overwritten.

const awsmobile = {
    "aws_project_region": "eu-west-1",
    "aws_cognito_identity_pool_id": "eu-west-1:cc349bd0-e021-430f-ba95-e99670291386",
    "aws_cognito_region": "eu-west-1",
    "aws_user_pools_id": "eu-west-1_stEEm1Pqz",
    "aws_user_pools_web_client_id": "1a2c5v258g1t29s95b8it9872o",
    "oauth": {},
    "aws_cognito_username_attributes": [],
    "aws_cognito_social_providers": [],
    "aws_cognito_signup_attributes": [
        "EMAIL"
    ],
    "aws_cognito_mfa_configuration": "OFF",
    "aws_cognito_mfa_types": [
        "SMS"
    ],
    "aws_cognito_password_protection_settings": {
        "passwordPolicyMinLength": 8,
        "passwordPolicyCharacters": []
    },
    "aws_cognito_verification_mechanisms": [
        "EMAIL"
    ],
    "aws_appsync_graphqlEndpoint": "https://btadyx5acrfk7h2mbk7fx2bouy.appsync-api.eu-west-1.amazonaws.com/graphql",
    "aws_appsync_region": "eu-west-1",
    "aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS",
    "aws_user_files_s3_bucket": "platelet26fb7449fb884a3eb4c5fd7539c78dd3193348-jan",
    "aws_user_files_s3_bucket_region": "eu-west-1",
    "geo": {
        "amazon_location_service": {
            "region": "eu-west-1",
            "search_indices": {
                "items": [
                    "plateletPlaceIndex-jan"
                ],
                "default": "plateletPlaceIndex-jan"
            }
        }
    },
    "aws_cognito_login_mechanisms": [
        "EMAIL"
    ]
};

export default awsmobile;

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

package.json file:

{
  "name": "plateletAdminAddNewUser",
  "version": "2.0.0",
  "description": "Lambda function generated by Amplify",
  "main": "index.js",
  "license": "Apache-2.0",
  "dependencies": {
    "aws-appsync": "^4.1.4",
    "graphql": "^16.3.0",
    "graphql-tag": "^2.12.6",
    "isomorphic-fetch": "^3.0.0",
    "uuid": "^8.3.2"
  }
}
duckbytes commented 2 years ago

I was able to work around the issue for now by downgrading:

yarn add aws-appsync@4.0.0

djsjr commented 2 years ago

npm install aws-appsync@4.0.0 did not solve it for me. Same error

menendezjaume commented 2 years ago

Same issue here. Downgrading to 4.0.0 fixed it for me too.

ffxsam commented 2 years ago

Downgrading to 4.0.0 worked for me too. Thanks!

@chrisbonifacio Any idea what's going on here?

chrisbonifacio commented 2 years ago

Downgrading to 4.0.0 worked for me too. Thanks!

@chrisbonifacio Any idea what's going on here?

Hey @ffxsam, we've since updated our AppSync docs to recommend using node-fetch when performing GraphQL mutation from a Lambda and/or Node.js rather than the AppSync SDK. Apologies for the confusion.

https://docs.amplify.aws/lib/graphqlapi/graphql-from-nodejs/q/platform/js/

ffxsam commented 2 years ago

@chrisbonifacio Perfect, thanks for the heads-up on that!

dsharygin commented 2 years ago

@chrisbonifacio Thank you for the update. Can you please elaborate what this means for the AWSAppSync JS SDK? We are using AWSAppSyncClient caching capabilities in a few places in our code.

ffxsam commented 2 years ago

@chrisbonifacio Just in retrospect: since this was a breaking change, shouldn't the version have been bumped to 5.0.0?

github-actions[bot] commented 1 year ago

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server amplify-help forum.