我试图使用graphql-upload
在typescript express应用程序(graphql api)与webpack构建。当我使用ts-node运行时,我的应用程序工作正常。但是当我用webpack编译然后运行时,当我试图上传文件时,我得到一个奇怪的错误。
- 打印稿应用
- 阿波罗服务器/Typegraphql
- Build with webpack
我的代码
https://github.com/ziggy6792/graphql-file-uploads
我的问题当我使用ts-nodeyarn start:ts:node
运行时,然后运行yarn test
,图像上传没有问题
但然后我用webpackyarn start
构建,然后运行yarn test
,我得到以下错误
/Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:56105
if (!isObject(operations) && !Array.isArray(operations))
^
TypeError: isObject is not a function
at Busboy.<anonymous> (/Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:56105:18)
at Busboy.emit (node:events:378:20)
at Busboy.module.exports../node_modules/busboy/lib/main.js.Busboy.emit (/Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:32204:33)
at PartStream.onEnd (/Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:32528:15)
at PartStream.emit (node:events:390:22)
at Dicer.onPart (/Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:32386:13)
at Dicer.emit (node:events:378:20)
at Dicer.module.exports../node_modules/dicer/lib/Dicer.js.Dicer.emit (/Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:45439:35)
at Dicer.module.exports../node_modules/dicer/lib/Dicer.js.Dicer._oninfo (/Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:45540:12)
at SBMH.<anonymous> (/Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:45486:10)
[nodemon] app crashed - waiting for file changes before starting...
My Attempts to solve
我不确定这实际上是graphql-upload
或webpack
或其他东西的问题。我也注意到我的问题看起来类似于这个问题,但我真的不明白是否有任何修复这个问题,如果它确实是我所面临的相同的问题。
我试图升级webpack(分支upgraded-packages
),但这使得问题更糟,因为我得到这个问题,当我启动服务器
TypeError: Cannot read property 'graphql' of undefined
at getPeerDependencyGraphQLRequirement (/Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:104216:44)
at Object.ensureInstalledCorrectGraphQLPackage (/Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:104221:32)
at Function.checkForErrors (/Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:103346:27)
at Function.generateFromMetadataSync (/Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:103325:14)
at Function.generateFromMetadata (/Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:103315:29)
at Object.buildSchema (/Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:103940:61)
at /Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:99831:41
at Generator.next (<anonymous>)
at /Users/sive/Documents/workspace/graphql-file-uploads/dist/index.js:99819:71
at new Promise (<anonymous>)
任何帮助都将是非常感激的。
我对这个问题的最佳猜测
从阅读链接的问题来看,isObject
似乎以较新版本发布了ECMA脚本模块。Webpack可能会选择ES模块版本,但这个版本的不同之处在于,默认导出现在是default
,而Apollo服务器将不得不导入它或调用isObject.default
。在这个更新日志中,他们说使用捆绑器的用户可能会崩溃。
现在你的两个选择:
你可以使用yarn强制依赖于以前版本的isobject
:
{
...
"resolutions": {
"graphql-file-uploads/isobject": "3.0.1"
}
}
或者你可以不捆绑你的应用,而是用TypeScript来构建。您失去了捆绑的好处,但是您使用的环境更适合运行节点应用程序。此外,你将使用TypeScript作为编译器,就像ts-node
一样,统一你的开发和生产工具。
你可以使用TypeScript像这样编译:
yarn tsc 'src/**/*.ts'
在您的TSConfig中,您应该配置输出,以便它也向/dist
发送文件。