Last active
February 26, 2018 17:12
-
-
Save xelaz/e1aca7a1aa0523ab923924d5eaf41f82 to your computer and use it in GitHub Desktop.
Upload Files in Mocha with GraphQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const boundary = '----' + Math.random(); | |
const data = [ | |
'--' + boundary, | |
'Content-Disposition: form-data; name="operations"', | |
'\r\n', | |
'{"query":"mutation($file: Upload!){uploadImage(file:$file){id,uuid}}"}', | |
'--' + boundary, | |
'Content-Disposition: form-data; name="map"', | |
'\r\n', | |
'{"0":["variables.file"]}', | |
'--' + boundary, | |
'Content-Disposition: form-data; name="0"; filename="zwilling.svg"', | |
'Content-Type: image/svg+xml', | |
'\r\n', | |
fs.readFileSync('build/migration/img/brand/zwilling.svg'), | |
'--' + boundary + '--' | |
].join('\r\n'); | |
const options = Object.assign({}, url.parse(url), { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'multipart/form-data; boundary=' + boundary, | |
'Content-Length': data.length, | |
} | |
}); | |
return new Promise((res, rej) => { | |
const req = request(options, function(response) { | |
response.setEncoding('utf8'); | |
const chunks = []; | |
response.on('data', function(chunk) { | |
chunks.push(chunk); | |
}); | |
response.on('error', rej); | |
response.on('end', function() { | |
res(chunks.join()); | |
}); | |
}); | |
req.on('error', rej); | |
req.write(data); | |
req.end(); | |
}) | |
.then(function(body) { | |
console.log('BODY::', body); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment