Netlify 函数无法导入相对的 .graphql 类型定义文件



我刚刚重写了一个 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.jsn- /var/task/graphql.jsn- /var/runtime/UserFunction.jsn- /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.graphqlfunctions/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 }
});

同样,这在本地工作,所以我假设压缩文件中完全缺少该文件。

我不知所措。有什么想法吗?

您是否尝试过在本地进行测试,但通过netlify functions:invoke使用网络化环境?它可以帮助调试此类问题。(见 https://github.com/netlify/cli/blob/master/docs/netlify-dev.md#locally-testing-functions-with-netlify-functionsinvoke(

关于您的问题,正如您已经建议的那样,某些文件似乎被忽略了:

Netlify 将访问函数目录 在每次部署期间,将每个受支持的代码文件压缩并部署为 AWS 上的无服务器 Lambda 函数。(请注意,除了一个例外, 子目录中的任何文件都将被忽略。

要使用多个文件,您需要在函数文件夹中定义一个包含函数名称和其中 js 的文件夹,否则它只会考虑.js文件:

您可以使用导出处理程序的独立.js文件(例如 函数/{function_name}.js( 或与.js同名的文件夹 其中导出处理程序的文件(例如 functions/{function_name}/{function_name}.js(.

确保使用相对导入或考虑完整路径。

您也可以尝试从 CLI 版本 2.7.0 开始部署非捆绑的 JavaScript。请参阅: https://docs.netlify.com/cli/get-started/#unbundled-javascript-function-deploys

最新更新