How to upload correctly with graphql and cloudinary

413 views Asked by At

so i have an issue i can't figure it out, i can't upload picture correctly with graphql and cloudinary

here is my server graphql yoga ====>

import { GraphQLServer } from 'graphql-yoga'
// import { permissions } from './permissions'
import { schema } from './schema'
import { createContext } from './context'
import { TIMEOUT } from 'dns'

const { PORT } = process.env

const server = new GraphQLServer({
  schema,
  context: createContext,
  // middlewares: [permissions],
})
server.start(({ port: PORT }) =>
  console.log(
    ` Server ready at: http://localhost:4000\n⭐️ See sample queries: http://pris.ly/e/ts/graphql-auth#using-the-graphql-api`
  )
)

here is my mutation =====>

t.field('uploadSupportAttachment', {
      type: 'FileUpload',
      args: {
        file: arg({ type: 'Upload' }),
      },
      resolve: async (_, { file }, ctx) => {
        console.log(file)
        const result = await uploadFile(file)
        return file
      },
    })

here is my function uploadFile ====>

export const uploadFile = async (file: any) => {
  // The Upload scalar return a a promise
  cloudinary.v2.config({
    cloud_name: '///',
    api_key: '///',
    api_secret: '///',
  })
  const { createReadStream, filename } = await file

  const streamLoad = cloudinary.v2.uploader.upload_stream(
    { timeout: 120000 },
    function (error: any, result: any) {
      if (result) {
        console.log('result ', result)
      } else {
        console.log('error ', error)
      }
    }
  )
  console.log(streamLoad)
  // var file_reader = fs.createReadStream(filename).pipe(streamLoad)
  return ''
}

cant stop to get theses error ====>

  message: 'Server returned unexpected status code - 504',
  http_code: 504,
  name: 'UnexpectedResponse'
}
OR 
error  Error: socket hang up
    at connResetException (internal/errors.js:612:14)
    at TLSSocket.socketOnEnd (_http_client.js:493:23)
    at TLSSocket.emit (events.js:326:22)
    at endReadableNT (_stream_readable.js:1252:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'ECONNRESET'

thanks in advance for your help

0

There are 0 answers