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.12k forks source link

Modular Amplify library usage with yarn workspaces causes #10310

Closed pfried closed 9 months ago

pfried commented 2 years ago

Before opening, please confirm:

JavaScript Framework

React

Amplify APIs

Authentication, GraphQL API, PubSub

Amplify Categories

auth, api

Environment information

``` # Put output below this line System: OS: Windows 10 10.0.19044 CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor Memory: 11.40 GB / 31.95 GB Binaries: Node: 16.10.0 - C:\Program Files\nodejs\node.EXE Yarn: 3.2.3 - C:\Program Files (x86)\Yarn\bin\yarn.CMD npm: 7.21.0 - C:\Program Files\nodejs\npm.CMD Browsers: Chrome: 104.0.5112.102 Edge: Spartan (44.19041.1266.0), Chromium (104.0.1293.70) Internet Explorer: 11.0.19041.1566 npmPackages: @aws-amplify/api: * => 4.0.53 @aws-amplify/auth: * => 4.6.6 @aws-amplify/core: * => 4.7.4 @aws-amplify/pubsub: * => 4.5.3 @casl/ability: ^5.4.4 => 5.4.4 @casl/ability/extra: undefined () @casl/react: ^3.0.0 => 3.0.1 @craco/craco: ^6.4.5 => 6.4.5 @headlessui/react: ^1.6.6 => 1.6.6 @heroicons/react: ^1.0.6 => 1.0.6 @hookform/resolvers: 2.8.0 => 2.8.0 @juggle/resize-observer: ^3.3.0 => 3.4.0 @mapbox/mapbox-gl-style-spec: 13.26.0 @nantis/grafana-data: * => 0.0.0 @nantis/gridknight-measurements: * => 0.0.0 @nantis/gridknight-styles: * => 0.0.0 @nantis/gridknight-ui: * => 0.0.0 @reduxjs/toolkit: ^1.8.5 => 1.8.5 @reduxjs/toolkit-query: 1.0.0 @reduxjs/toolkit-query-react: 1.0.0 @swc/core: ^1.2.140 => 1.2.242 @swc/jest: ^0.2.17 => 0.2.22 @tailwindcss/forms: ^0.5.2 => 0.5.2 @testing-library/dom: ^8.11.3 => 8.17.1 @testing-library/jest-dom: ^5.16.2 => 5.16.5 @testing-library/react: ^13.3.0 => 13.3.0 @testing-library/user-event: ^13.5.0 => 13.5.0 @types/jest: ^27.0.3 => 27.5.2 (28.1.8) @types/leaflet: ^1.7.11 => 1.7.11 @types/node: ^12.0.0 => 12.20.55 (18.7.13) @types/react: ^18.0.15 => 18.0.17 @types/react-date-range: ^1.4.4 => 1.4.4 @types/react-dom: ^18.0.6 => 18.0.6 autoprefixer: ^10.0.2 => 10.4.8 class-validator: 1.0.0 computed-types: 1.0.0 cuid: ^2.1.8 => 2.1.8 date-fns: ^2.28.0 => 2.29.2 history: ^5.0.0 => 5.3.0 i18next: ^21.8.9 => 21.9.1 i18next-browser-languagedetector: ^6.1.4 => 6.1.5 io-ts: 1.0.0 jest: ^27.4.5 => 27.5.1 joi: 1.0.0 leaflet: ^1.8.0 => 1.8.0 mapbox-gl: ^2.8.2 => 2.10.0 nope: 1.0.0 postcss: ^8 => 8.4.16 (7.0.39) react: ^18.2.0 => 18.2.0 react-date-range: ^1.4.0 => 1.4.0 react-dom: ^18.2.0 => 18.2.0 react-error-boundary: ^3.1.4 => 3.1.4 react-hook-form: ^7.33.1 => 7.34.2 react-i18next: ^11.17.0 => 11.18.4 react-leaflet: ^4.0.1 => 4.0.2 react-redux: ^8.0.2 => 8.0.2 react-router-dom: ^6.3.0 => 6.3.0 react-scripts: ^5.0.1 => 5.0.1 react-spring: ^9.5.2 => 9.5.2 source-map-explorer: ^2.5.2 => 2.5.2 superstruct: 1.0.0 tailwindcss: ^3.1.1 => 3.1.8 typanion: 1.0.0 typescript: ^4.7.4 => 4.7.4 vest: 1.0.0 web-vitals: ^1.0.1 => 1.1.2 yup: 0.32.9 => 0.32.9 (1.0.0) zen-observable-ts: ^0.8.0 => 0.8.21 (0.8.19) zod: 1.0.0 npmGlobalPackages: @aws-amplify/cli: 4.37.1 @maizzle/cli: 1.4.0 agentkeepalive: 4.1.4 aws-cdk-lib: 2.2.0 full-icu: 1.4.0 live-server: 1.2.1 npm-windows-upgrade: 6.0.1 npm: 7.21.0 pkg: 5.4.1 yarn: 1.22.17 ```

Describe the bug

Using the modular amplify packages as in https://github.com/aws-amplify/amplify-js/wiki/Amplify-Modularization i run into an error where the auth (called by the pubsub) instance errors with:

AppSync Realtime subscription init error: Error: No User Pool in the configuration.

My setup is a yarn monorepo with a nmHoistingLimits: dependencies configuration

My guess is that it is due to the fact that the @aws-ampify/api package has a node_modules folder with another @aws-ampify/auth package which is not hoisted to the top. Should those dependencies not be peer dependencies?

Expected behavior

To work exactly like the monolothic package

Reproduction steps

Clone my repo and create a subscription on an API,. Login via a Cognito pool and see the error when the subscription is called

Repro: https://github.com/pfried/appsync-module-lib-issue-repro-webapp

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line ```

aws-exports.js

const config = {
    // AUTH
    aws_cognito_region: region, // (required) - Region where Amazon Cognito project was created
    aws_user_pools_id:  userPoolId, // (optional) -  Amazon Cognito User Pool ID
    aws_user_pools_web_client_id: userPoolClientId, // (optional) - Amazon Cognito App Client ID (App client secret needs to be disabled)
    aws_cognito_identity_pool_id: identityPoolRef, // (optional) - Amazon Cognito Identity Pool ID
    aws_mandatory_sign_in: "disabled", // (optional) - Users are not allowed to get the aws credentials unless they are signed in

    // API
    aws_appsync_region: region, // (optional) - AWS AppSync region
    aws_appsync_graphqlEndpoint: graphqlUrl, // (optional) - AWS AppSync endpoint
    // Seems to be more a kind of default auth type, you can override it per query, see create tenant
    aws_appsync_authenticationType: "AMAZON_COGNITO_USER_POOLS", // (optional) - Primary AWS AppSync authentication type

    // Usually we send only the access key which does not contain the username and other things encoded in the JWT
    // So we send our "own" header with the id token
    API: {
        graphql_headers: async () => {
            try {
                const session = await Auth.currentSession();
                if(session) {
                    return {
                        Authorization: session.getIdToken().getJwtToken(),
                    };
                }
            } catch(e) {
                console.warn(e)
            }
        },
    }
}

Manual configuration

Amplify.register(Auth)
Amplify.register(API)
Amplify.register(PubSub)

Amplify.configure(config);

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

app node_modules image

@aws-amplify/api node_modules

image

pfried commented 1 year ago

The same thing happens if you install the monolithic package + PubSub:

"aws-amplify": "^5.0.8",
"@aws-amplify/pubsub": "^5.0.8",

Then the Hub (+Observers) do not work as the pacakges are not hoistsed. This is a serious issue as it is not so easy to diagnose

pfried commented 1 year ago

Possibly related to https://github.com/aws-amplify/amplify-js/issues/7772

cwomack commented 10 months ago

@pfried, can you see if you continue to experience this issue if you upgrade to the most recent version of Amplify? With the release of the latest major version of Amplify (aws-amplify@>6), we believe this issue should now be resolved! Please refer to our release announcement, migration guide, and documentation for more information.

cwomack commented 10 months ago

@pfried, just wanted to circle back and see if you've had a chance to upgrade and test this in v6 of Amplify. Let us know if you do!

pfried commented 9 months ago

@cwomack No unfortunately this could take some weeks until I get to that

cwomack commented 9 months ago

@pfried, we'll close this issue then for now but will be happy to reopen it if you experience this in v6 after upgrading. This should be resolved in the latest version.

Thank you!