使用GraphQl的FileUpload在尝试上传客户端时将文件显示为无效输入
我正在尝试将文件上传到Apollo GraphQL。使用上传
标量与graphQlupload
当前失败的客户端
type
export const typeDefs = gql`
scalar Upload
type File {
url: String!
}
突变&解析器
export const resolvers = {
Upload: GraphQLUpload
uploadFile(file: Upload!): File!
uploadFile: async (_, { file }) => {
const { createReadStream, filename } = await file;
const stream = createReadStream();
const { ext } = path.parse(filename)
const name = ext + v4()
const pathName = path.join(__dirname, `../public/images/${name}`)
await stream.pipe(fs.createWriteStream(pathName))
return {
url: `http://localhost:5000/images/${name}`
};
},
客户端
export const UPLOAD_FILE = gql`
mutation UploadFile($file: Upload!){
uploadFile(file: $file){
url
}
}
`
const handleUpload = async () => {
if (!image) return;
try {
const { data } = await uploadFile({
variables: { file: image }
})
if (data) {
console.log('res =>', data)
handleCreateNewPost(data.uploadFile.url)
}
}
catch (err) {
console.log(err)
}
当前我得到一个无效的输入错误:
{"errors":[{"message":"Variable \"$file\" got invalid value { path: \"Photo on 2021-09-07 at 1.34 PM.jpg\" }
image
console.logged显示一个文件对象
File {path: 'Photo on 2021-09-07 at 1.34 PM.jpg', name: 'Photo on 2021-09-07 at 1.34 PM.jpg', lastModified: 1631036079471, lastModifiedDate: Tue Sep 07 2021 13:34:39 GMT-0400 (Eastern Daylight Time), webkitRelativePath: '', …}
path: "Photo on 2021-09-07 at 1.34 PM.jpg"
lastModified: 1631036079471
lastModifiedDate: Tue Sep 07 2021 13:34:39 GMT-0400 (Eastern Daylight Time) {}
name: "Photo on 2021-09-07 at 1.34 PM.jpg"
size: 134828
type: "image/jpeg"
webkitRelativePath: ""
[[Prototype]]: File
I am trying to upload a file with apollo graphQL. using the Upload
scalar with GraphQLUpload
which is currently failing client side
Type
export const typeDefs = gql`
scalar Upload
type File {
url: String!
}
Mutation & Resolvers
export const resolvers = {
Upload: GraphQLUpload
uploadFile(file: Upload!): File!
uploadFile: async (_, { file }) => {
const { createReadStream, filename } = await file;
const stream = createReadStream();
const { ext } = path.parse(filename)
const name = ext + v4()
const pathName = path.join(__dirname, `../public/images/${name}`)
await stream.pipe(fs.createWriteStream(pathName))
return {
url: `http://localhost:5000/images/${name}`
};
},
CLIENT SIDE
export const UPLOAD_FILE = gql`
mutation UploadFile($file: Upload!){
uploadFile(file: $file){
url
}
}
`
const handleUpload = async () => {
if (!image) return;
try {
const { data } = await uploadFile({
variables: { file: image }
})
if (data) {
console.log('res =>', data)
handleCreateNewPost(data.uploadFile.url)
}
}
catch (err) {
console.log(err)
}
currently I get an invalid input error:
{"errors":[{"message":"Variable \"$file\" got invalid value { path: \"Photo on 2021-09-07 at 1.34 PM.jpg\" }
image
console.logged shows a file object
File {path: 'Photo on 2021-09-07 at 1.34 PM.jpg', name: 'Photo on 2021-09-07 at 1.34 PM.jpg', lastModified: 1631036079471, lastModifiedDate: Tue Sep 07 2021 13:34:39 GMT-0400 (Eastern Daylight Time), webkitRelativePath: '', …}
path: "Photo on 2021-09-07 at 1.34 PM.jpg"
lastModified: 1631036079471
lastModifiedDate: Tue Sep 07 2021 13:34:39 GMT-0400 (Eastern Daylight Time) {}
name: "Photo on 2021-09-07 at 1.34 PM.jpg"
size: 134828
type: "image/jpeg"
webkitRelativePath: ""
[[Prototype]]: File
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
遇到了同一问题。
问题在于我们的前端的阿波罗客户端,我们没有在客户端中包含上传链接。
还要注意,根据文档,您只能拥有一个这样的终止链接
因此,如果您的客户端中都有两个链接,则仍然会遇到相同的错误。
Ran into this same issue.
The problem was in our apollo client on our frontend, we did not include the upload link in the client.
Also note that according to the documentation, you can only have one such terminating link
So if you have both links in your client, you will still get the same error.