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 core = require('@apollo/client/link/core');
|
8 | const apolloUploadClient = require('apollo-upload-client');
|
9 | const FormData = _interopDefault(require('form-data'));
|
10 | const crossFetch = require('cross-fetch');
|
11 | const utilities = require('@apollo/client/utilities');
|
12 | const core$1 = require('@apollo/client/core');
|
13 | const utils = require('@graphql-tools/utils');
|
14 | const graphql = require('graphql');
|
15 | const isPromise = _interopDefault(require('is-promise'));
|
16 |
|
17 | function getFinalPromise(object) {
|
18 | return Promise.resolve(object).then(resolvedObject => {
|
19 | if (resolvedObject == null) {
|
20 | return resolvedObject;
|
21 | }
|
22 | if (Array.isArray(resolvedObject)) {
|
23 | return Promise.all(resolvedObject.map(o => getFinalPromise(o)));
|
24 | }
|
25 | else if (typeof resolvedObject === 'object') {
|
26 | const keys = Object.keys(resolvedObject);
|
27 | return Promise.all(keys.map(key => getFinalPromise(resolvedObject[key]))).then(awaitedValues => {
|
28 | for (let i = 0; i < keys.length; i++) {
|
29 | resolvedObject[keys[i]] = awaitedValues[i];
|
30 | }
|
31 | return resolvedObject;
|
32 | });
|
33 | }
|
34 | return resolvedObject;
|
35 | });
|
36 | }
|
37 | class AwaitVariablesLink extends core.ApolloLink {
|
38 | request(operation, forward) {
|
39 | return new utilities.Observable(observer => {
|
40 | let subscription;
|
41 | getFinalPromise(operation.variables)
|
42 | .then(resolvedVariables => {
|
43 | operation.variables = resolvedVariables;
|
44 | subscription = forward(operation).subscribe({
|
45 | next: observer.next.bind(observer),
|
46 | error: observer.error.bind(observer),
|
47 | complete: observer.complete.bind(observer),
|
48 | });
|
49 | })
|
50 | .catch(observer.error.bind(observer));
|
51 | return () => {
|
52 | if (subscription != null) {
|
53 | subscription.unsubscribe();
|
54 | }
|
55 | };
|
56 | });
|
57 | }
|
58 | }
|
59 |
|
60 | const createServerHttpLink = (options) => core.concat(new AwaitVariablesLink(), apolloUploadClient.createUploadLink({
|
61 | ...options,
|
62 | fetch: crossFetch.fetch,
|
63 | FormData,
|
64 | isExtractableFile: (value) => apolloUploadClient.isExtractableFile(value) || (value === null || value === void 0 ? void 0 : value.createReadStream),
|
65 | formDataAppendFile: (form, index, file) => {
|
66 | if (file.createReadStream != null) {
|
67 | form.append(index, file.createReadStream(), {
|
68 | filename: file.filename,
|
69 | contentType: file.mimetype,
|
70 | });
|
71 | }
|
72 | else {
|
73 | apolloUploadClient.formDataAppendFile(form, index, file);
|
74 | }
|
75 | },
|
76 | }));
|
77 |
|
78 | const linkToExecutor = (link) => async (params) => {
|
79 | const { document, variables, extensions, context, operationType, operationName, info } = params;
|
80 | const observable = core.execute(link, {
|
81 | query: document,
|
82 | variables,
|
83 | context: {
|
84 | graphqlContext: context,
|
85 | graphqlResolveInfo: info,
|
86 | clientAwareness: {},
|
87 | },
|
88 | extensions,
|
89 | operationName,
|
90 | });
|
91 | if (operationType === 'subscription') {
|
92 | return utils.observableToAsyncIterable(observable)[Symbol.asyncIterator]();
|
93 | }
|
94 | return core$1.toPromise(observable);
|
95 | };
|
96 |
|
97 | const GraphQLUpload = new graphql.GraphQLScalarType({
|
98 | name: 'Upload',
|
99 | description: 'The `Upload` scalar type represents a file upload.',
|
100 | parseValue: value => {
|
101 | if (value != null && isPromise(value.promise)) {
|
102 |
|
103 | return value.promise;
|
104 | }
|
105 | else if (isPromise(value)) {
|
106 |
|
107 | return value;
|
108 | }
|
109 | throw new graphql.GraphQLError('Upload value invalid.');
|
110 | },
|
111 |
|
112 | serialize: value => value,
|
113 | parseLiteral: ast => {
|
114 | throw new graphql.GraphQLError('Upload literal unsupported.', ast);
|
115 | },
|
116 | });
|
117 |
|
118 | exports.AwaitVariablesLink = AwaitVariablesLink;
|
119 | exports.GraphQLUpload = GraphQLUpload;
|
120 | exports.createServerHttpLink = createServerHttpLink;
|
121 | exports.linkToExecutor = linkToExecutor;
|