KG
Size: a a a
OG
OG
OG
l
OG
OG
l
VR
VR
OG
ИП
ИП
ИП
ИП
uploadFile(variables: Record<string, any>, onProgress=(e: ProgressEvent)=>{}): [XMLHttpRequest, Promise<any>] {
const form = this.client.toFormData(actions.uploadFiles, variables)
const xhr = new XMLHttpRequest()
xhr.open('POST', this.url, true)
return [xhr, new Promise<XMLHttpRequest>((resolve, reject) => {
for (let [k, v] of Object.entries(this.auth))
xhr.setRequestHeader(k, v || '')
xhr.onload = () => resolve(xhr)
xhr.onerror = () => reject(xhr)
xhr.onabort = () => reject(xhr)
if (onProgress)
xhr.upload.onprogress = onProgress
xhr.send(form)
}).then(response => prepare(JSON.parse(response.responseText)))]
}
ИП
toFormData(query: DocumentNode, variables: Record<string, any>) {
const files = extractFiles(variables, 'variables')
// GraphQL multipart request spec:
// https://github.com/jaydenseric/graphql-multipart-request-spec
const form = new FormData()
form.append('operations', JSON.stringify({query: print(query), variables}))
form.append('map', JSON.stringify(
files.reduce((map: any, {path}, index) => {
map[index.toString()] = [path]
return map
}, {})
))
files.forEach(({file}, index) =>
form.append(index.toString(), file, file instanceof File ? file.name : 'Blob')
)
return form
}
P@
OG
OG