/
graphqlFetchOptions.mjs
60 lines (55 loc) · 1.4 KB
/
graphqlFetchOptions.mjs
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
import { ReactNativeFile } from 'extract-files'
import FormData from 'formdata-node'
import t from 'tap'
import { graphqlFetchOptions } from '../universal/graphqlFetchOptions.mjs'
// Global FormData polyfill.
global.FormData = FormData
t.test('graphqlFetchOptions', async t => {
await t.test('Without files', t => {
t.deepEquals(
graphqlFetchOptions({ query: '' }),
{
url: '/graphql',
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: '{"query":""}'
},
'Fetch options'
)
t.end()
})
await t.test('With files', t => {
const file = new ReactNativeFile({
uri: '',
name: 'a.jpg',
type: 'image/jpeg'
})
const options = graphqlFetchOptions({
query: '',
variables: { a: file }
})
// See the GraphQL multipart request spec:
// https://github.com/jaydenseric/graphql-multipart-request-spec
t.type(options.body, FormData, 'Fetch options `body` type')
t.deepEquals(
options,
{
url: '/graphql',
method: 'POST',
headers: {
Accept: 'application/json'
},
body: [
['operations', '{"query":"","variables":{"a":null}}'],
['map', '{"1":["variables.a"]}'],
['1', '[object Object]']
]
},
'Fetch options'
)
t.end()
})
})