Giter VIP home page Giter VIP logo

Comments (9)

jaydenseric avatar jaydenseric commented on May 22, 2024 2

Short answer; it is untestable. The File API only exists in browsers and no spec-compliant polyfill exists for the Node.js environment. Believe me I tried when making tests for apollo-fetch-upload.

from apollo-upload-client.

Fonz001 avatar Fonz001 commented on May 22, 2024 1

It's been a while, is there already a suitable polyfill available? Did you try it with: https://github.com/node-file-api/file-api ?

from apollo-upload-client.

jaydenseric avatar jaydenseric commented on May 22, 2024 1

@schickling part of the challenge of testing browser functionality in Node.js is that Blob, File, FileList, FormData, and fetch global polyfills all need to be cross compatible. Detecting a File instance in variables for extraction is one thing, being able to FormData.append() a File instance is another.

It would be awesome if someone were to create a family of cross-compatible polyfills for these browser globals; then apollo-upload-client could be used on the server the same as the browser making tests easy.

from apollo-upload-client.

jaydenseric avatar jaydenseric commented on May 22, 2024 1

It depends what you want to test. If you are testing your client handles uploads provided from the user on the front end, then you are in a pickle. But if you want to test your GraphQL API can handle uploads properly, then testing that in Node.js is easy! @jure is right, the apollo-upload-server tests are the best resource to look at for this. You just have to follow the GraphQL multipart request spec; you can even use cURL.

from apollo-upload-client.

Fonz001 avatar Fonz001 commented on May 22, 2024 1

This works for us -

file is a Buffer
fileName is a string
uri is a string

You'll be needing node-fetch and form-data (npm)

And this is TypeScript... So feel free to ignore the typings.

  const body = new FormData()
  body.append(
    'operations',
    JSON.stringify({
      variables: {
        file: null,
      },
      query: `mutation($file:Upload!){
        uploadFile(file:$file) {
          id
          createdAt
          updatedAt
          deletedAt
          notes
          originalName
          mimeType
          contentType
          location
        }
      }`,
    }),
  )
  body.append('map', JSON.stringify({ 0: ['variables.file'] }))
  body.append('0', file.toString(), { filename: fileName })

  // Upload the file
  const result = await fetch(uri, {
    method: 'POST',
    body: body
  })

  const jsonData = await result.json()

  return jsonData.data.uploadFile as File

from apollo-upload-client.

schickling avatar schickling commented on May 22, 2024

Yeah, I feel this topic should definitely be revisited. Testing this part of an API is super important.

from apollo-upload-client.

jure avatar jure commented on May 22, 2024

In case anyone stumbles upon this issue trying to test upload from Node.js, I'm happy with the workaround that apollo-upload-server itself is using, which is to use form-data: https://github.com/jaydenseric/apollo-upload-server/blob/master/src/test.mjs#L73-L83.

I know it's not the same as testing with apollo-upload-client itself, but it does the job for us and might do it for you too.

from apollo-upload-client.

Fonz001 avatar Fonz001 commented on May 22, 2024

We actually just finished implementing 10 tests all inspired by: https://github.com/jaydenseric/apollo-upload-server/blob/master/src/test.mjs#L73-L83

We are big fans of using graphql-bindings based of apollo-upload-client for testing our api, so we were a little disappointed that, that's not working just yet (neither did we have the time to make it work). However, writing the first succesful test only took about 2 hours, which is fine by us.

from apollo-upload-client.

ragnarkurmwunder avatar ragnarkurmwunder commented on May 22, 2024

Any update on how to test file upload with apollo-upload-client?

from apollo-upload-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.