AWS-LAMBDA:没有这样的文件或目录,打开 './src/graphql/schema.graphql'
Posted
技术标签:
【中文标题】AWS-LAMBDA:没有这样的文件或目录,打开 \'./src/graphql/schema.graphql\'【英文标题】:AWS-LAMBDA: no such file or directory, open './src/graphql/schema.graphql'AWS-LAMBDA:没有这样的文件或目录,打开 './src/graphql/schema.graphql' 【发布时间】:2022-01-19 16:28:11 【问题描述】:我正在练习 Graphql 和 AWS。我使用简单的无服务器框架也创建了简单的 Graphql 模式。我部署了架构(好像这个 graphql.schema
文件没有部署),解析器到 AWS。它成功地创建了一个 DynamoDB 表和 lambda。我可以使用 serverless-offline 通过 Graphql Playground 发出 POST/GET 请求。但问题是 api 端点不起作用。它告诉我internal server error
。我正在调查这个问题。从 cloud watch 我发现了我创建的 Lambda 函数的本地模式确实找到了graphql.schema
。这是我收到的错误"ENOENT: no such file or directory, open './src/graphql/schema.graphql'".
这是 lambda 错误Image
这是我的 lambda 函数
import ApolloServer from 'apollo-server-lambda';
import ApolloServerPluginLandingPageGraphQLPlayground from 'apollo-server-core';
import runWarm from '../utils/run-warm';
import fs from 'fs';
const schema = fs.readFileSync('./src/graphql/schema.graphql', 'utf8'); // This is local my schema
import resolvers from '../resolvers';
const server = new ApolloServer(
typeDefs: schema,
resolvers,
introspection: true,
plugins: [ApolloServerPluginLandingPageGraphQLPlayground()],
);
export default runWarm(
server.createHandler(
expressGetMiddlewareOptions:
cors:
origin: '*',
credentials: true,
allowedHeaders: ['Content-Type', 'Origin', 'Accept'],
optionsSuccessStatus: 200,
,
,
)
);
这是我的无服务器 YAML 文件
service: serverless-aws-graphql
package:
individually: true
provider:
name: aws
profile: $env:profile
runtime: nodejs14.x
stage: $env:stage
region: eu-north-1
timeout: 30
apiName: $self:service.name-$self:provider.stage
environment:
ITEM_TABLE: $self:service-items-$self:provider.stage
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: 'arn:aws:dynamodb:$opt:region, self:provider.region:*:table/$self:provider.environment.ITEM_TABLE'
apiGateway:
shouldStartNameWithService: true
custom:
webpack:
webpackConfig: ./webpack.config.js
includeModules: true
packager: 'npm' # Packager that will be used to package your external modules
warmup:
enabled: true
events:
- schedule: rate(5 minutes)
prewarm: true
concurrency: 1
prune:
automatic: true
number: 5
functions:
graphql:
handler: src/handlers/graphql.default
events:
- http:
path: $env:api_prefix/graphql
method: any
cors: true
resources:
Resources:
ItemsTable:
Type: 'AWS::DynamoDB::Table'
Properties:
AttributeDefinitions:
- AttributeName: PK
AttributeType: S
- AttributeName: SK
AttributeType: S
- AttributeName: GSI1PK
AttributeType: S
- AttributeName: GSI1SK
AttributeType: S
KeySchema:
- AttributeName: PK
KeyType: HASH
- AttributeName: SK
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
GlobalSecondaryIndexes:
- IndexName: GSI1
KeySchema:
- AttributeName: GSI1PK
KeyType: HASH
- AttributeName: GSI1SK
KeyType: RANGE
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: $self:provider.environment.ITEM_TABLE
plugins:
- serverless-webpack
- serverless-offline
- serverless-plugin-warmup
- serverless-dotenv-plugin
- serverless-prune-plugin
这是我的 webpack.config.js 设置
const nodeExternals = require('webpack-node-externals');
const slsw = require('serverless-webpack');
module.exports =
entry: slsw.lib.entries,
target: 'node',
mode: slsw.lib.webpack.isLocal ? 'development' : 'production',
externals: [nodeExternals()],
module:
rules: [
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
,
],
,
resolve:
extensions: ['.tsx', '.ts', '.js', '.jsx'],
,
;
这是我的 tsconfig 设置
"compilerOptions":
"target": "esnext",
"allowJs": true,
"skipLibCheck": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"noEmit": false,
"jsx": "preserve",
"noUnusedLocals": true,
"noUnusedParameters": true
,
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
]
【问题讨论】:
【参考方案1】:正如您所观察到的,./src/graphql/schema.graphql
没有被打包到无服务器构建和部署的最终工件中。
您可以通过在函数中指定 package 属性来添加它:
graphql:
handler: src/handlers/graphql.default
events:
- http:
path: $env:api_prefix/graphql
method: any
cors: true
package:
include:
- src/graphql/schema.graphql
来源:https://www.serverless.com/framework/docs/providers/aws/guide/packaging#patterns
【讨论】:
仍然出现同样的错误:( 当您将 Typescript 与 Serverless 一起使用时,似乎还有一些额外的构建步骤。您可以查看并尝试this question 或this question 的答案。最新的一个与你的情况非常相似。 谢谢!我想我想通了,但没有找到主要原因。它有些方法不加载 schema.graphql 文件,但它接受 schema.ts 文件。 ,我认为我的 webpack 和 typescript 设置有问题。我将在问题中附上我的 webpack 和 tsconfig 设置。 好吧,知道您正在使用 webpack 会有所帮助。很高兴您找到了解决方案!以上是关于AWS-LAMBDA:没有这样的文件或目录,打开 './src/graphql/schema.graphql'的主要内容,如果未能解决你的问题,请参考以下文章
Qt无法打开包含文件:'QNetworkAccessManager':没有这样的文件或目录
无法打开包含文件 - 'gtest.h' - 没有这样的文件或目录
无法打开包含文件:'unistd.h':没有这样的文件或目录