1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
|
6 |
|
7 | const apolloLink = require('apollo-link');
|
8 | const apolloUploadClient = require('apollo-upload-client');
|
9 | const FormData = _interopDefault(require('form-data'));
|
10 | const crossFetch = require('cross-fetch');
|
11 | const utils = require('@graphql-tools/utils');
|
12 | const graphql = require('graphql');
|
13 |
|
14 | function getFinalPromise(object) {
|
15 | return Promise.resolve(object).then(resolvedObject => {
|
16 | if (resolvedObject == null) {
|
17 | return resolvedObject;
|
18 | }
|
19 | if (Array.isArray(resolvedObject)) {
|
20 | return Promise.all(resolvedObject.map(o => getFinalPromise(o)));
|
21 | }
|
22 | else if (typeof resolvedObject === 'object') {
|
23 | const keys = Object.keys(resolvedObject);
|
24 | return Promise.all(keys.map(key => getFinalPromise(resolvedObject[key]))).then(awaitedValues => {
|
25 | for (let i = 0; i < keys.length; i++) {
|
26 | resolvedObject[keys[i]] = awaitedValues[i];
|
27 | }
|
28 | return resolvedObject;
|
29 | });
|
30 | }
|
31 | return resolvedObject;
|
32 | });
|
33 | }
|
34 | class AwaitVariablesLink extends apolloLink.ApolloLink {
|
35 | request(operation, forward) {
|
36 | return new apolloLink.Observable(observer => {
|
37 | let subscription;
|
38 | getFinalPromise(operation.variables)
|
39 | .then(resolvedVariables => {
|
40 | operation.variables = resolvedVariables;
|
41 | subscription = forward(operation).subscribe({
|
42 | next: observer.next.bind(observer),
|
43 | error: observer.error.bind(observer),
|
44 | complete: observer.complete.bind(observer),
|
45 | });
|
46 | })
|
47 | .catch(observer.error.bind(observer));
|
48 | return () => {
|
49 | if (subscription != null) {
|
50 | subscription.unsubscribe();
|
51 | }
|
52 | };
|
53 | });
|
54 | }
|
55 | }
|
56 |
|
57 | class FormDataWithStreamSupport extends FormData {
|
58 | constructor(options) {
|
59 | super(options);
|
60 | this.hasUnknowableLength = false;
|
61 | }
|
62 | append(key, value, optionsOrFilename = {}) {
|
63 |
|
64 | const options = typeof optionsOrFilename === 'string' ? { filename: optionsOrFilename } : optionsOrFilename;
|
65 |
|
66 | if (!options.knownLength &&
|
67 | !Buffer.isBuffer(value) &&
|
68 | typeof value !== 'string' &&
|
69 | !value.path &&
|
70 | !(value.readable && 'httpVersion' in value)) {
|
71 | this.hasUnknowableLength = true;
|
72 | }
|
73 | super.append(key, value, options);
|
74 | }
|
75 | getLength(callback) {
|
76 | if (this.hasUnknowableLength) {
|
77 | return null;
|
78 | }
|
79 | return super.getLength(callback);
|
80 | }
|
81 | getLengthSync() {
|
82 | if (this.hasUnknowableLength) {
|
83 | return null;
|
84 | }
|
85 |
|
86 | return super.getLengthSync();
|
87 | }
|
88 | }
|
89 | const createServerHttpLink = (options) => apolloLink.concat(new AwaitVariablesLink(), apolloUploadClient.createUploadLink({
|
90 | ...options,
|
91 | fetch: crossFetch.fetch,
|
92 | FormData: FormDataWithStreamSupport,
|
93 | isExtractableFile: (value) => apolloUploadClient.isExtractableFile(value) || (value === null || value === void 0 ? void 0 : value.createReadStream),
|
94 | formDataAppendFile: (form, index, file) => {
|
95 | if (file.createReadStream != null) {
|
96 | form.append(index, file.createReadStream(), {
|
97 | filename: file.filename,
|
98 | contentType: file.mimetype,
|
99 | });
|
100 | }
|
101 | else {
|
102 | apolloUploadClient.formDataAppendFile(form, index, file);
|
103 | }
|
104 | },
|
105 | }));
|
106 |
|
107 | const linkToExecutor = (link) => ({ document, variables, context, info, }) => apolloLink.toPromise(apolloLink.execute(link, {
|
108 | query: document,
|
109 | variables,
|
110 | context: {
|
111 | graphqlContext: context,
|
112 | graphqlResolveInfo: info,
|
113 | },
|
114 | }));
|
115 |
|
116 | const linkToSubscriber = (link) => async ({ document, variables, context, info, }) => utils.observableToAsyncIterable(apolloLink.execute(link, {
|
117 | query: document,
|
118 | variables,
|
119 | context: {
|
120 | graphqlContext: context,
|
121 | graphqlResolveInfo: info,
|
122 | },
|
123 | }));
|
124 |
|
125 | const GraphQLUpload = new graphql.GraphQLScalarType({
|
126 | name: 'Upload',
|
127 | description: 'The `Upload` scalar type represents a file upload.',
|
128 | parseValue: value => {
|
129 | if (value != null && value.promise instanceof Promise) {
|
130 |
|
131 | return value.promise;
|
132 | }
|
133 | else if (value instanceof Promise) {
|
134 |
|
135 | return value;
|
136 | }
|
137 | throw new graphql.GraphQLError('Upload value invalid.');
|
138 | },
|
139 |
|
140 | serialize: value => value,
|
141 | parseLiteral: ast => {
|
142 | throw new graphql.GraphQLError('Upload literal unsupported.', ast);
|
143 | },
|
144 | });
|
145 |
|
146 | exports.AwaitVariablesLink = AwaitVariablesLink;
|
147 | exports.GraphQLUpload = GraphQLUpload;
|
148 | exports.createServerHttpLink = createServerHttpLink;
|
149 | exports.linkToExecutor = linkToExecutor;
|
150 | exports.linkToSubscriber = linkToSubscriber;
|
151 |
|