question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Need help with Express api upload via Insomania

See original GitHub issue

Thanks @jaydenseric for this plugin, I’m working on Express API (mongodb, apollo server off course ) and using Insomania to test all my features.

Here is my code

index.js

const helperMiddleware = [
  bodyParser.json(),
  bodyParser.urlencoded({ extended: true }),
  bodyParser.text({ type: 'application/graphql' }),
  (req, res, next) => {
    if (req.is('application/graphql')) {
      req.body = { query: req.body }
    }
    next()
  }
]

const buildOptions = async (req, res) => {
  res.removeHeader("X-Powered-By") // remove x power headers
  const mongo = await connectMongo()
  const user = await authentication(req, res, mongo.Users)
  return {
    schema,
    context: {
      mongo,
      dataloaders: buildDataloaders(mongo),
      user
    },
    // tracing: true
  }
}

app.use(
  '/api',
  ...helperMiddleware,
  apolloUploadExpress({
    maxFileSize: 5 * 1000,
    maxFiles: 5
  }),
  graphqlExpress(buildOptions)
)

schema

type Image {
  # upload
  id: ID!
  path: String!
  filename: String!
  mimetype: String!
  encoding: String!
  # data
  caption: String
  created_by: User!
  created_date: Datetime
  width: Int
  height: Int
  url: String
  album: Album
  variants: [ImageVariant]
}

mutation schema

uploadImage(files: [Upload!]!): [Image!]

resolver

const imgDir = './public/uploads'
const storeUpload = async ({ stream, filename }) => {
  const id = uuid.v4()
  const path = `${imgDir}/${id}-${filename}`
  return new Promise((resolve, reject) =>
    stream
      .pipe(createWriteStream(path))
      .on('finish', () => resolve({ id, path }))
      .on('error', reject)
  )
}

const processUpload = async (upload, Images) => {
  const { stream, filename, mimetype, encoding } = await upload
  const { id, path } = await storeUpload({ stream, filename })
  const newImage = { id, filename, mimetype, encoding, path }
  const response = await Images.insert(newImage)
  return Object.assign({ id: response.insertedIds[0] }, newImage)
}

export const uploadImage = async (root, data, { mongo: { Images }, user }) => processUpload(data.files, Images)

Insomania Query

mutation Upload($files: [Upload!]!) { 
	uploadImg(files: $files) {
		filename
		encoding
		mimetype
		path
	}
}
screen shot 2018-01-03 at 9 30 03 am

Expected

  • Upload multiple images within maximum 5 files (and maximum 5MB for each)
  • Insert data to db

Result

Non-stop sending data request in Insomania

I don’t know what I missed, would you please help me out, I’m kind of newbie.

Thank you.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
jaydensericcommented, Jan 4, 2018

You’re getting closer 😛

The reason that did not work is because the query, operationName and variables multipart form fields don’t exist in the spec, and the required operations field is missing.

You might find it useful to study the construction of the requests in the apollo-upload-server tests.

Most people never have to manually construct requests like this; everything is done for you by apollo-upload-client.

1reaction
viiiprockcommented, Jan 4, 2018

yes, follow your example and use apollo-upload-client is more easier for me to make it work, I’ll learn more about the test case, thanks in advance @jaydenseric

Read more comments on GitHub >

github_iconTop Results From Across the Web

Insomnia REST Client Tutorial - YouTube
APIs for Beginners - How to use an API (Full Course / Tutorial) · Salesforce Integration Crash Course | The Ultimate Guide to...
Read more >
Insomnia upload pic and post data at same time - Stack Overflow
Insomnia offers only URL Encoded for data and Binary File for file. I cannot send both at the same time. What should I...
Read more >
How to Create Documentation for Your REST API with Insomnia
Open the Insomnia app and go to your dashboard. Create a new Design Document by clicking the Create button on the top right...
Read more >
Using the Upload API Endpoint – Help Center - Support - Zype
To upload a video using Insomnia, you have to set the body as multipart and add the file and all the other necessary...
Read more >
Building an API with Express and Node.js - DEV Community ‍ ‍
After we install Insomnia, let's make a GET request to localhost:4000/toys , to make sure our backend application is working correctly and data ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found