W. *_*yna 18 node.js graphql apollo-server express-graphql
我正在尝试使用 Apollo Server 的Upload
标量将文件直接发送到 S3。我的架构:
const { gql } = require('apollo-server-express')
module.exports = gql`
extend type Mutation {
createPicture(
name: String!
picture: Upload!
): Picture!
}
type Picture {
name: String!
picture: String!
}
`
Run Code Online (Sandbox Code Playgroud)
解析器:
const { combineResolvers } = require('graphql-resolvers')
const isAuthenticated = require('./auth')
const { uploadPhoto } = require('../services/picture')
module.exports = {
Mutation: {
createPicture: combineResolvers(
isAuthenticated,
async (
parent,
{ name, picture = null },
{ models, me }
) => {
const { createReadStream, filename, mimetype, encoding } = await picture
// Does not get past this line
const stream = createReadStream()
uploadPhoto(stream, filename)
const pictureModel = models.Picture.create({
name,
picture
})
return pictureModel
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
但是我的代码错误是这样的:
const { combineResolvers } = require('graphql-resolvers')
const isAuthenticated = require('./auth')
const { uploadPhoto } = require('../services/picture')
module.exports = {
Mutation: {
createPicture: combineResolvers(
isAuthenticated,
async (
parent,
{ name, picture = null },
{ models, me }
) => {
const { createReadStream, filename, mimetype, encoding } = await picture
// Does not get past this line
const stream = createReadStream()
uploadPhoto(stream, filename)
const pictureModel = models.Picture.create({
name,
picture
})
return pictureModel
}
)
}
}
Run Code Online (Sandbox Code Playgroud)
注意:我确定图像已正确发送,filename
正确无误
小智 13
将此添加到 package.json:
"resolutions": {
"**/**/fs-capacitor":"^6.2.0",
"**/graphql-upload": "^11.0.0"
}
Run Code Online (Sandbox Code Playgroud)
来源:https : //github.com/jaydenseric/graphql-upload/issues/170#issuecomment-641938198
小智 6
This error occured to me in node version 14 too! I solved it as follows:
Install latest version of graphql-upload !
use graphqlUploadExpress middleware to define the maximum file limit.
import { graphqlUploadExpress } from "graphql-upload";
const app = express()
app.use(graphqlUploadExpress({ maxFileSize: 1000000000, maxFiles: 10 }));
Run Code Online (Sandbox Code Playgroud)Set uploads to false while initializing the ApolloServer
const server = new ApolloServer({
uploads: false,
schema,
});
Run Code Online (Sandbox Code Playgroud) 归档时间: |
|
查看次数: |
8816 次 |
最近记录: |