【发布时间】:2020-08-27 15:51:27
【问题描述】:
我刚刚将 Node.js GraphQL API 从旧版本的 graphql-yoga + prisma 重写为使用 express、serverless-http、prisma 和 apollo-server-express。我已经让它作为 Express 应用程序在本地正常工作,但是当我尝试将它作为函数发布在 Netlify 上时,我收到“找不到模块”错误:
{
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module './schema.graphql'\nRequire stack:\n- /var/task/bundle/index.js\n- /var/task/graphql.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
"trace": [
"Runtime.ImportModuleError: Error: Cannot find module './schema.graphql'",
"Require stack:",
"- /var/task/bundle/index.js",
"- /var/task/graphql.js",
"- /var/runtime/UserFunction.js",
"- /var/runtime/index.js",
" at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
" at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
" at Object.<anonymous> (/var/runtime/index.js:43:30)",
" at Module._compile (internal/modules/cjs/loader.js:1156:30)",
" at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)",
" at Module.load (internal/modules/cjs/loader.js:1000:32)",
" at Function.Module._load (internal/modules/cjs/loader.js:899:14)",
" at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)",
" at internal/main/run_main_module.js:18:47"
]
}
我尝试了多种方法来导入这些 graphql 文件(graphql-imports、require.resolve、fs.readDirSync 等),但要么根本不起作用,要么只能从项目根目录起作用。 require.resolve 在本地项目根目录下工作,所以我认为这个文件只是没有包含在构建中。我怎么知道是什么被压缩了?
我的 netlify.toml 是:
[build]
command = "npm run bundle"
functions = "functions"
npm run bundle 基本上是cpx 'src/**/*' 'functions/bundle',并且文件在functions/bundle/schema.graphql 和functions/bundle/generated/prisma.graphql 中本地可见。
我的functions/graphql.js 只是使用 Express 服务器并创建一个 lambda 包装器:
const serverlessHttp = require("serverless-http");
const { app } = require("./bundle/index.js");
exports.handler = serverlessHttp(app, {
request(req, event, context) {
req.event = event;
req.context = context;
}
});
"graphql-import" 非常烦人,因为它只适用于项目的根目录,因此将 src/ 文件复制到 functions/bundle/ 会更改目录结构,并且这些导入不再起作用
在functions/bundle/index.js里面,我尝试require.resolve一些.grapqhl类型定义文件:
const { importSchema } = require("graphql-import");
const typeDefs = importSchema(require.resolve("./schema.graphql"));
const prismaTypeDefs = importSchema(
require.resolve("./generated/prisma.graphql")
);
const db = new Prisma({
typeDefs: prismaTypeDefs,
endpoint: process.env.PRISMA_ENDPOINT,
secret: process.env.PRISMA_SECRET,
debug: process.env.NODE_ENV === "development"
});
const schema = makeExecutableSchema({
typeDefs: gql`
${typeDefs}
`,
resolvers: {
Mutation,
Query,
},
resolverValidationOptions: { requireResolversForResolveType: false }
});
同样,这在本地工作,所以我假设该文件完全从压缩文件中丢失。
我很茫然。有什么想法吗?
【问题讨论】:
标签: lambda graphql apollo-server netlify prisma