-
Notifications
You must be signed in to change notification settings - Fork 309
Open
Labels
Description
Screenshot
Description
When the Upload extension for graffle is used with a file sent in a GraphQL request, headers previously set on the transport() function are reset.
The issue seems to be linked to a destructuring on a Header Object.
Problematic line of code :
graffle/src/extensions/Upload/Upload.ts
Line 58 in 4c58ee6
| ...pack.input.transport.headers, |
Reproduction Steps
// STEP 1: Prove that destructuring isn't a good approach with a Header Object
const myHeaders = new Headers();
myHeaders.append("Content-Type", "text/xml");
myHeaders.append("Vary", "Accept-Language");
console.log("\n\nLogging headers: ", myHeaders)
console.log("Using destructuring: ", { ...myHeaders })
console.log("Converting to Object from entries: ", Object.fromEntries(myHeaders.entries()))
// STEP 2: Live test: inspect request payload sent @ https://graffle_upload.requestcatcher.com/
import { Graffle } from 'graffle'
import { Upload } from 'graffle/extensions/upload'
import { inspect } from 'node:util';
const graffle = Graffle
.create()
.transport({
url: "https://graffle_upload.requestcatcher.com/",
headers: {
Authorization: 'Basic ' + Buffer.from("username:password").toString('base64'),
'x-custom-header': 'MY_CUSTOM_VALUE',
},
})
.use(Upload)
.anyware(async ({ encode }) => {
// Inspect requests contents
console.log("\n\nENCODE (before pack hook): ==> ", inspect(encode.input.transport, { depth: 3 }))
const { pack } = await encode()
console.log("\n\nPACK (after pack hook): ==> ", inspect(pack.input.transport, { depth: 3 }))
return await pack()
})
// Test Request with file
const data = graffle.gql(`
mutation UploadFile($file: Upload!) {
uploadFile(file: $file) {
id
url
}
}
`).$send({
blob: new Blob([`Hello World: this is a file!`], { type: `text/plain` }),
})
try {
let res = await data
console.log("DATA IS :", res)
} catch (error) {
// Expected error, as ping API will not respond with a JSON.
}Console Output from test code
Notice that the headers are reset between encode & pack.
Logging headers: Headers { 'Content-Type': 'text/xml', Vary: 'Accept-Language' }
Using destructuring: {}
Converting to Object from entries: { 'content-type': 'text/xml', vary: 'Accept-Language' }
ENCODE (before pack hook): ==> {
methodMode: 'post',
raw: undefined,
url: 'https://graffle_upload.requestcatcher.com/',
headers: Headers {
Authorization: 'Basic dXNlcm5hbWU6cGFzc3dvcmQ=',
'x-custom-header': 'MY_CUSTOM_VALUE'
}
}
PACK (after pack hook): ==> {
methodMode: 'post',
raw: undefined,
url: 'https://graffle_upload.requestcatcher.com/',
headers: { 'content-type': '' }
}
Reactions are currently unavailable