-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest.html
More file actions
71 lines (66 loc) · 2.49 KB
/
Copy pathtest.html
File metadata and controls
71 lines (66 loc) · 2.49 KB
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script>
// begin-snippet: PostMutationWithTextResult
function PostMutationWithTextResult() {
var postSettings = BuildPostSettings();
return fetch('graphql', postSettings)
.then(function (data) {
return data.text().then(x => {
result.innerHTML = x;
});
});
}
// end-snippet
// begin-snippet: PostMutationAndDownloadFile
function PostMutationAndDownloadFile() {
var postSettings = BuildPostSettings();
return fetch('graphql', postSettings)
.then(function (data) {
return data.formData().then(x => {
var resultContent = '';
x.forEach(e => {
// This is the attachments
if (e.name) {
var a = document.createElement('a');
var blob = new Blob([e]);
a.href = window.URL.createObjectURL(blob);
a.download = e.name;
a.click();
}
else {
resultContent += JSON.stringify(e);
}
});
result.innerHTML = resultContent;
});
});
}
// end-snippet
// begin-snippet: BuildPostSettings
function BuildPostSettings() {
var data = new FormData();
var files = document.getElementById("files").files;
for (var i = 0; i < files.length; i++) {
data.append('files[]', files[i], files[i].name);
}
data.append(
"query",
'mutation{ withAttachment (argument: "argumentValue"){argument}}'
);
return {
method: 'POST',
body: data
};
}
// end-snippet
</script>
</head>
<body>
Select files: <input type="file" id="files" multiple /><br /><br />
<button onclick="PostMutationWithTextResult()">PostMutation With Text Result</button><br /><br />
<button onclick="PostMutationAndDownloadFile()">PostMutation And Download File</button><br /><br />
<pre id="result"></pre>
</body>
</html>