简体   繁体   English

如何将 AWS Amplify 与 Sapper 一起使用?

[英]How to use AWS Amplify with Sapper?

My goal is to use AWS Amplify in a Sapper project.我的目标是在Sapper项目中使用AWS Amplify

Creating a Sapper project from scratch (using webpack) then adding AWS Amplify and running it in dev is a success, but run it in production throws a GraphQL error in the console ( Uncaught Error: Cannot use e "__Schema" from another module or realm ).从头开始创建一个 Sapper 项目(使用 webpack)然后添加 AWS Amplify 并在开发中运行它是成功的,但是在生产中运行它会在控制台中引发 GraphQL 错误(未捕获的错误:无法使用来自另一个模块的 e“__Schema”或 realm )。

Fixing this error thows another one ( Uncaught ReferenceError: process is not defined ).修复此错误会导致另一个错误(未捕获的 ReferenceError:未定义进程)。

A solution is to upgrade GraphQL from 0.13.0 to 14.0.0 unfortunatly GraphQL 0.13.0 is an AWS Amplify API dependency.一个解决方案是将 GraphQL 从 0.13.0 升级到 14.0.0 不幸的是 GraphQL 0.13.0 是 AWS Amplify API 依赖项。

Does anyone know what can be done to get AWS Amplify work with Sapper in production?有谁知道如何让 AWS Amplify 在生产中与 Sapper 一起工作?

The link to the repo containing the source files is located here: https://github.com/ehemmerlin/sapper-aws-amplify包含源文件的 repo 链接位于此处: https://github.com/ehemmerlin/sapper-aws-amplify


(Apologies for the long post but I want to be explicit) (为长篇道歉,但我想明确一点)

Detailled steps详细步骤

1/ Create a Sapper project using webpack ( https://sapper.svelte.dev ). 1/ 使用 webpack ( https://sapper.svelte.dev ) 创建一个 Sapper 项目。

npx degit "sveltejs/sapper-template#webpack" my-app
cd my-app
yarn install

2/ Add AWS Amplify ( https://serverless-stack.com/chapters/configure-aws-amplify.html ) and lodash 2/ 添加 AWS Amplify ( https://serverless-stack.com/chapters/configure-aws-amplify.html ) 和 lodash

yarn add aws-amplify
yarn add lodash

3/ Configure AWS Amplify ( https://serverless-stack.com/chapters/configure-aws-amplify.html ) 3/ 配置 AWS Amplify ( https://serverless-stack.com/chapters/configure-aws-amplify.html )

Create src/config/aws.js config file containing (change the values with yours but works as is for the purpose of this post):创建src/config/aws.js配置文件,其中包含(使用您的更改值,但按本文的目的工作):

export default {
  s3: {
    REGION: "YOUR_S3_UPLOADS_BUCKET_REGION",
    BUCKET: "YOUR_S3_UPLOADS_BUCKET_NAME"
  },
  apiGateway: {
    REGION: "YOUR_API_GATEWAY_REGION",
    URL: "YOUR_API_GATEWAY_URL"
  },
  cognito: {
    REGION: "YOUR_COGNITO_REGION",
    USER_POOL_ID: "YOUR_COGNITO_USER_POOL_ID",
    APP_CLIENT_ID: "YOUR_COGNITO_APP_CLIENT_ID",
    IDENTITY_POOL_ID: "YOUR_IDENTITY_POOL_ID"
  }
};

Add the following code to the existing code in src/client.js :将以下代码添加到src/client.js中的现有代码中:

import config from './config/aws';

Amplify.configure({
  Auth: {
    mandatorySignIn: true,
    region: config.cognito.REGION,
    userPoolId: config.cognito.USER_POOL_ID,
    identityPoolId: config.cognito.IDENTITY_POOL_ID,
    userPoolWebClientId: config.cognito.APP_CLIENT_ID
  },
  Storage: {
    region: config.s3.REGION,
    bucket: config.s3.BUCKET,
    identityPoolId: config.cognito.IDENTITY_POOL_ID
  },
  API: {
    endpoints: [
      {
        name: "notes",
        endpoint: config.apiGateway.URL,
        region: config.apiGateway.REGION
      },
    ]
  }
});

4/ Test it 4/ 测试它

In dev (yarn run dev): it works在开发(纱线运行开发)中:它有效

In production (yarn run build; node __sapper__/build): it throws an error.在生产中(纱线运行构建;节点 __sapper__/build):它会引发错误。

Uncaught Error: Cannot use e "__Schema" from another module or realm.
Ensure that there is only one instance of "graphql" in the node_modules
directory. If different versions of "graphql" are the dependencies of other
relied on modules, use "resolutions" to ensure only one version is installed.
https://yarnpkg.com/en/docs/selective-version-resolutions
Duplicate "graphql" modules cannot be used at the same time since different
versions may have different capabilities and behavior. The data from one
version used in the function from another could produce confusing and
spurious results.

5/ Fix it 5/ 修复它

Following the given link ( https://yarnpkg.com/en/docs/selective-version-resolutions ) I added this code to package.json file:按照给定的链接( https://yarnpkg.com/en/docs/selective-version-resolutions )我将此代码添加到 package.json 文件:

  "resolutions": {
    "aws-amplify/**/graphql": "^0.13.0"
  }

6/ Test it 6/ 测试它

rm -rf node_modules; yarn install

Throws another error in the console (even in dev mode).在控制台中引发另一个错误(即使在开发模式下)。

Uncaught ReferenceError: process is not defined
at Module../node_modules/graphql/jsutils/instanceOf.mjs (instanceOf.mjs:3)
at \_\_webpack_require\_\_ (bootstrap:63)
at Module../node_modules/graphql/type/definition.mjs (definition.mjs:1)
at \_\_webpack_require\_\_ (bootstrap:63)
at Module../node_modules/graphql/type/validate.mjs (validate.mjs:1)
at \_\_webpack_require\_\_ (bootstrap:63)
at Module../node_modules/graphql/graphql.mjs (graphql.mjs:1)
at \_\_webpack_require\_\_ (bootstrap:63)
at Module../node_modules/graphql/index.mjs (main.js:52896)
at \_\_webpack_require\_\_ (bootstrap:63)

A fix given by this thread ( https://github.com/graphql/graphql-js/issues/1536 ) is to upgrade GraphQL from 0.13.0 to 14.0.0 unfortunatly GraphQL 0.13.0 is an AWS Amplify API dependency. A fix given by this thread ( https://github.com/graphql/graphql-js/issues/1536 ) is to upgrade GraphQL from 0.13.0 to 14.0.0 unfortunatly GraphQL 0.13.0 is an AWS Amplify API dependency.

When building my project (I'm using npm and webpack), I got this warning,在构建我的项目时(我正在使用 npm 和 webpack),我收到了这个警告,

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults 
for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/

which seems to be related to the schema error, as these posts indicate that the quickest fix for the error is to set NODE_ENV to production in your environment ( mode is set to the NODE_ENV environment variable in the webpack config):这似乎与架构错误有关,因为这些帖子表明该错误的最快修复是在您的环境中将NODE_ENV设置为productionmode设置为 webpack 配置中的 NODE_ENV 环境变量):

https://github.com/aws-amplify/amplify-js/issues/1445 https://github.com/aws-amplify/amplify-js/issues/1445

https://github.com/aws-amplify/amplify-js/issues/3963 https://github.com/aws-amplify/amplify-js/issues/3963

How to do that:怎么做:

How to set NODE_ENV to production/development in OS X 如何在 OS X 中将 NODE_ENV 设置为生产/开发

How can I set NODE_ENV=production on Windows? 如何在 Windows 上设置 NODE_ENV=production?

Or you can mess with the webpack config directly:或者你可以直接弄乱 webpack 配置:

https://gist.github.com/jmannau/8039787e29190f751aa971b4a91a8901 https://gist.github.com/jmannau/8039787e29190f751aa971b4a91a8901

Unfortunately some posts in those GitHub issues point out the environment variable change might not work out for a packaged app, specifically on mobile.不幸的是,这些 GitHub 问题中的一些帖子指出,环境变量更改可能不适用于打包的应用程序,特别是在移动设备上。

These posts suggest that disabling the mangler might be the next best solution:这些帖子表明禁用 mangler 可能是下一个最佳解决方案:

https://github.com/graphql/graphql-js/issues/1182 https://github.com/graphql/graphql-js/issues/1182

https://github.com/rmosolgo/graphiql-rails/issues/58 https://github.com/rmosolgo/graphiql-rails/issues/58


For anyone just trying to get the basic Sapper and Amplify setup going, to reproduce this error or otherwise, I build up mine with:对于任何试图获得基本 Sapper 和 Amplify 设置、重现此错误或其他情况的人,我构建了我的:

npm install -g @aws-amplify/cli

npx degit "sveltejs/sapper-template#webpack" my-app

npm install

npm install aws-amplify

npm install lodash (Amplify with webpack seems to need this) npm install lodash (用webpack放大好像需要这个)

amplify configure

npm run build

amplify init (dev environment, VS Code, javascript, no framework, src directory, __sapper__\build distribution directory, default AWS profile. This generates aws-exports.js . amplify init (开发环境,VS Code,javascript,无框架, src目录, __sapper__\build分发目录,默认 AWS 配置文件。这会生成aws-exports.js

In src/client.js :src/client.js中:

import Amplify from 'aws-amplify';
import awsconfig from './aws-exports';
Amplify.configure(awsconfig);

npm run build

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM