Skip to content

Commit

Permalink
Add context.fetchOptions to fetch options (#17)
Browse files Browse the repository at this point in the history
* Add context.fetchOptions to fetch options.

* Fix comment

* Fix semi colon and whitespace
  • Loading branch information
Benjamin Leeds authored and ihorkatkov committed Dec 31, 2018
1 parent 012a274 commit c8a2dc0
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,27 @@ export const createUploadMiddleware = ({ uri, headers, fetch }) =>
const { variables, files } = extractFiles(operation.variables)

if (files.length > 0) {
const { headers: contextHeaders } = operation.getContext()
const context = operation.getContext()
const { headers: contextHeaders } = context
const formData = new FormData()

formData.append('query', printAST(operation.query))
formData.append('variables', JSON.stringify(variables))
files.forEach(({ name, file }) => formData.append(name, file))

let options = {
method: 'POST',
headers: Object.assign({}, contextHeaders, headers),
body: formData,
}

// add context.fetchOptions to fetch options
options = Object.assign(context.fetchOptions || {}, options)

// is there a custom fetch? then use it
if (fetch) {
return new Observable(observer => {
fetch(uri, {
method: 'POST',
headers: Object.assign({}, contextHeaders, headers),
body: formData,
})
fetch(uri, options)
.then(response => {
operation.setContext({ response })
return response
Expand Down

0 comments on commit c8a2dc0

Please sign in to comment.