/
index.js
75 lines (66 loc) · 2.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { HttpLink } from 'apollo-link-http'
import { ApolloLink, concat } from 'apollo-link'
import { print } from 'graphql/language/printer'
import request from './request'
import extractFiles from './extractFiles'
import { isObject } from './validators'
import { parseAndCheckHttpResponse } from 'apollo-link-http-common'
import { Observable } from 'apollo-link'
export const createUploadMiddleware = ({ uri, headers, fetch, credentials }) =>
new ApolloLink((operation, forward) => {
if (typeof FormData !== 'undefined' && isObject(operation.variables)) {
const { variables, files } = extractFiles(operation.variables)
if (files.length > 0) {
const context = operation.getContext()
const { headers: contextHeaders } = context
const formData = new FormData()
formData.append('query', print(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,
credentials,
}
// 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, options)
.then(response => {
operation.setContext({ response })
return response
})
.then(parseAndCheckHttpResponse(operation))
.then(result => {
// we have data and can send it to back up the link chain
observer.next(result)
observer.complete()
return result
})
.catch(err => {
if (err.result && err.result.errors && err.result.data) {
observer.next(err.result)
}
observer.error(err)
})
})
} else {
const withCredentials = credentials === 'include'
return request({
uri,
body: formData,
headers: Object.assign({}, contextHeaders, headers),
withCredentials,
crossDomain: withCredentials,
})
}
}
}
return forward(operation)
})
export const createLink = opts =>
concat(createUploadMiddleware(opts), new HttpLink(opts))
export { ReactNativeFile } from './validators'