使用GraphQl的FileUpload在尝试上传客户端时将文件显示为无效输入

发布于 2025-01-24 21:02:31 字数 2061 浏览 0 评论 0原文

我正在尝试将文件上传到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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没有你 2025-01-31 21:02:31

遇到了同一问题。

问题在于我们的前端的阿波罗客户端,我们没有在客户端中包含上传链接。

import { createUploadLink } from 'apollo-upload-client'

...

const uploadLink = createUploadLink({ uri: process.env.GATSBY_APOLLO_URL })
  apolloLinks.push(uploadLink)


const client = new ApolloClient({
    link: from(apolloLinks),
    cache: new InMemoryCache(),
  })

还要注意,根据文档,您只能拥有一个这样的终止链接

Apollo Client can only have 1 terminating Apollo Link that sends the GraphQL requests; 
if one such as HttpLink is already setup, remove it.

因此,如果您的客户端中都有两个链接,则仍然会遇到相同的错误。

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.

import { createUploadLink } from 'apollo-upload-client'

...

const uploadLink = createUploadLink({ uri: process.env.GATSBY_APOLLO_URL })
  apolloLinks.push(uploadLink)


const client = new ApolloClient({
    link: from(apolloLinks),
    cache: new InMemoryCache(),
  })

Also note that according to the documentation, you can only have one such terminating link

Apollo Client can only have 1 terminating Apollo Link that sends the GraphQL requests; 
if one such as HttpLink is already setup, remove it.

So if you have both links in your client, you will still get the same error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文