Comments (9)
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.
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.
@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.
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.
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.
Yeah, I feel this topic should definitely be revisited. Testing this part of an API is super important.
from apollo-upload-client.
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.
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.
Any update on how to test file upload with apollo-upload-client
?
from apollo-upload-client.
Related Issues (20)
- Module not found - after updating to 18.0.1 HOT 2
- Problem with Typescript HOT 2
- Document required TypeScript libs HOT 1
- How do you use ReactNativeFile in the latest version HOT 3
- Error with Vite HOT 1
- Failed to resolve entry for package "apollo-upload-client". HOT 2
- Support for @defer directive?
- Version 18 doesn't work with React Native HOT 2
- Module not found HOT 2
- Broken compatibility with HotChocolate 13.2 -> HOT 1
- Why js instead of ts? HOT 3
- Version 18 incompatible with Next.js HOT 3
- Support with @habx/apollo-multi-endpoint-link HOT 2
- Query on Compatibility with @apollo/client 3.9.0 and apollo-upload-client HOT 1
- Pending requests issue HOT 2
- importing the library in react js project HOT 1
- "Reached maximum amount of queued data" in Safari HOT 2
- Must provide query string HOT 3
- Can this lib provider `typings` HOT 4
- ReactNativeFile HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apollo-upload-client.