-
Notifications
You must be signed in to change notification settings - Fork 582
/
useGqlHandler.ts
199 lines (190 loc) · 7 KB
/
useGqlHandler.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import path from "path";
import fs from "fs";
import { createHandler } from "@webiny/handler-aws";
import graphqlHandlerPlugins from "@webiny/handler-graphql";
import fileManagerPlugins from "@webiny/api-file-manager/plugins";
import fileManagerDynamoDbPlugins from "@webiny/api-file-manager-ddb";
import i18nContext from "@webiny/api-i18n/graphql/context";
import i18nDynamoDbStorageOperations from "@webiny/api-i18n-ddb";
import i18nContentPlugins from "@webiny/api-i18n-content/plugins";
import { mockLocalesPlugins } from "@webiny/api-i18n/graphql/testing";
import { SecurityIdentity } from "@webiny/api-security/types";
import { createFormBuilder } from "~/index";
// Graphql
import { INSTALL as INSTALL_FILE_MANAGER } from "./graphql/fileManagerSettings";
import {
GET_SETTINGS,
UPDATE_SETTINGS,
INSTALL,
IS_INSTALLED
} from "./graphql/formBuilderSettings";
import {
CREATE_FROM,
CREATE_REVISION_FROM,
DELETE_FORM,
UPDATE_REVISION,
PUBLISH_REVISION,
UNPUBLISH_REVISION,
DELETE_REVISION,
SAVE_FORM_VIEW,
GET_FORM,
GET_FORM_REVISIONS,
LIST_FORMS,
GET_PUBLISHED_FORM
} from "./graphql/forms";
import {
CREATE_FROM_SUBMISSION,
LIST_FROM_SUBMISSIONS,
EXPORT_FORM_SUBMISSIONS
} from "./graphql/formSubmission";
import { SecurityPermission } from "@webiny/api-security/types";
import { until } from "@webiny/project-utils/testing/helpers/until";
import { createTenancyAndSecurity } from "./tenancySecurity";
export interface UseGqlHandlerParams {
permissions?: SecurityPermission[];
identity?: SecurityIdentity;
plugins?: any;
}
const defaultIdentity = { id: "12345678", type: "admin", displayName: "John Doe" };
export default (params: UseGqlHandlerParams = {}) => {
const { permissions, identity, plugins = [] } = params;
// @ts-ignore
if (typeof __getStorageOperations !== "function") {
throw new Error(`There is no global "__getStorageOperations" function.`);
}
// @ts-ignore
const { createStorageOperations, getGlobalPlugins } = __getStorageOperations();
if (typeof createStorageOperations !== "function") {
throw new Error(
`A product of "__getStorageOperations" must be a function to initialize storage operations.`
);
}
if (typeof getGlobalPlugins === "function") {
plugins.push(...getGlobalPlugins());
}
const handler = createHandler(
...plugins,
graphqlHandlerPlugins(),
...createTenancyAndSecurity({ permissions, identity: identity || defaultIdentity }),
i18nContext(),
i18nDynamoDbStorageOperations(),
i18nContentPlugins(),
mockLocalesPlugins(),
fileManagerPlugins(),
fileManagerDynamoDbPlugins(),
/**
* We need to create the form builder API app.
* It requires storage operations and plugins from the storage operations.
*/
createFormBuilder({
storageOperations: createStorageOperations()
}),
{
type: "api-file-manager-storage",
name: "api-file-manager-storage",
async upload(args) {
// TODO: use tmp OS directory
const key = path.join(__dirname, args.name);
fs.writeFileSync(key, args.buffer);
return {
file: {
key: args.name,
name: args.name,
type: args.type,
size: args.size
}
};
},
async delete() {
// dummy
}
}
);
// Let's also create the "invoke" function. This will make handler invocations in actual tests easier and nicer.
const invoke = async ({ httpMethod = "POST", body, headers = {}, ...rest }) => {
const response = await handler({
httpMethod,
headers,
body: JSON.stringify(body),
...rest
});
// The first element is the response body, and the second is the raw response.
return [JSON.parse(response.body), response];
};
return {
until,
sleep: (ms = 100) => {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
},
handler,
invoke,
defaultIdentity,
// Form builder settings
async updateSettings(variables = {}) {
return invoke({ body: { query: UPDATE_SETTINGS, variables } });
},
async getSettings(variables = {}) {
return invoke({ body: { query: GET_SETTINGS, variables } });
},
// Install Form builder
async install(variables = {}) {
return invoke({ body: { query: INSTALL, variables } });
},
async isInstalled(variables = {}) {
return invoke({ body: { query: IS_INSTALLED, variables } });
},
// Install File Manager
async installFileManager(variables) {
return invoke({ body: { query: INSTALL_FILE_MANAGER, variables } });
},
// Forms
async createForm(variables) {
return invoke({ body: { query: CREATE_FROM, variables } });
},
async createRevisionFrom(variables) {
return invoke({ body: { query: CREATE_REVISION_FROM, variables } });
},
async deleteForm(variables) {
return invoke({ body: { query: DELETE_FORM, variables } });
},
async updateRevision(variables) {
return invoke({ body: { query: UPDATE_REVISION, variables } });
},
async publishRevision(variables) {
return invoke({ body: { query: PUBLISH_REVISION, variables } });
},
async unpublishRevision(variables) {
return invoke({ body: { query: UNPUBLISH_REVISION, variables } });
},
async deleteRevision(variables) {
return invoke({ body: { query: DELETE_REVISION, variables } });
},
async saveFormView(variables) {
return invoke({ body: { query: SAVE_FORM_VIEW, variables } });
},
async getForm(variables) {
return invoke({ body: { query: GET_FORM, variables } });
},
async getFormRevisions(variables) {
return invoke({ body: { query: GET_FORM_REVISIONS, variables } });
},
async getPublishedForm(variables) {
return invoke({ body: { query: GET_PUBLISHED_FORM, variables } });
},
async listForms(variables = {}) {
return invoke({ body: { query: LIST_FORMS, variables } });
},
// Form Submission
async createFormSubmission(variables) {
return invoke({ body: { query: CREATE_FROM_SUBMISSION, variables } });
},
async listFormSubmissions(variables) {
return invoke({ body: { query: LIST_FROM_SUBMISSIONS, variables } });
},
async exportFormSubmissions(variables) {
return invoke({ body: { query: EXPORT_FORM_SUBMISSIONS, variables } });
}
};
};