-
Notifications
You must be signed in to change notification settings - Fork 582
/
useGqlHandler.ts
193 lines (184 loc) · 7.17 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
import richTextFieldPlugin from "./mocks/richTextFieldPlugin";
import fileManagerPlugins from "@webiny/api-file-manager/plugins";
import fileManagerDdbEsPlugins from "~/index";
import dynamoToElastic from "@webiny/api-dynamodb-to-elasticsearch/handler";
import { DynamoDbDriver } from "@webiny/db-dynamodb";
import { DocumentClient } from "aws-sdk/clients/dynamodb";
import dbPlugins from "@webiny/handler-db";
import dynamoDbPlugins from "@webiny/db-dynamodb/plugins";
import { simulateStream } from "@webiny/project-utils/testing/dynamodb";
import elasticsearchClientContextPlugin from "@webiny/api-elasticsearch";
import { createHandler } from "@webiny/handler-aws";
import graphqlHandlerPlugins from "@webiny/handler-graphql";
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";
/**
* Load some test stuff from the api-file-manager
*/
import {
CREATE_FILE,
CREATE_FILES,
UPDATE_FILE,
DELETE_FILE,
GET_FILE,
LIST_FILES
} from "../../api-file-manager/__tests__/graphql/file";
import {
INSTALL,
IS_INSTALLED,
GET_SETTINGS,
UPDATE_SETTINGS
} from "../../api-file-manager/__tests__/graphql/fileManagerSettings";
import { SecurityPermission } from "@webiny/api-security/types";
import { until } from "@webiny/project-utils/testing/helpers/until";
import { FilePhysicalStoragePlugin } from "@webiny/api-file-manager/plugins/definitions/FilePhysicalStoragePlugin";
import { createTenancyAndSecurity } from "./tenancySecurity";
import { SecurityIdentity } from "@webiny/api-security/types";
import { createElasticsearchClient } from "@webiny/api-elasticsearch/client";
type UseGqlHandlerParams = {
permissions?: SecurityPermission[];
identity?: SecurityIdentity;
};
export default (params?: UseGqlHandlerParams) => {
const { permissions, identity } = params || {};
const ELASTICSEARCH_PORT = process.env.ELASTICSEARCH_PORT || "9200";
const elasticsearchClient = createElasticsearchClient({
node: `http://localhost:${ELASTICSEARCH_PORT}`,
auth: {}
});
const documentClient = new DocumentClient({
convertEmptyValues: true,
endpoint: process.env.MOCK_DYNAMODB_ENDPOINT || "http://localhost:8001",
sslEnabled: false,
region: "local",
accessKeyId: "test",
secretAccessKey: "test"
});
const elasticsearchClientContext = elasticsearchClientContextPlugin(elasticsearchClient);
const clearElasticsearch = async () => {
return elasticsearchClient.indices.delete({
index: "_all"
});
};
const createElasticsearchIndice = async () => {
return elasticsearchClient.indices.create({
index: "root-file-manager",
body: {
settings: {
analysis: {
analyzer: {
lowercase_analyzer: {
type: "custom",
filter: ["lowercase", "trim"],
tokenizer: "keyword"
}
}
}
},
mappings: {
properties: {
property: {
type: "text",
fields: {
keyword: {
type: "keyword",
ignore_above: 256
}
},
analyzer: "lowercase_analyzer"
},
rawValues: {
type: "object",
enabled: false
}
}
}
}
});
};
// Intercept DocumentClient operations and trigger dynamoToElastic function (almost like a DynamoDB Stream trigger)
simulateStream(documentClient, createHandler(elasticsearchClientContext, dynamoToElastic()));
const tenant = { id: "root", name: "Root", parent: null };
// Creates the actual handler. Feel free to add additional plugins if needed.
const handler = createHandler(
dbPlugins({
table: process.env.DB_TABLE,
driver: new DynamoDbDriver({
documentClient
})
}),
dynamoDbPlugins(),
...createTenancyAndSecurity({ permissions, identity }),
graphqlHandlerPlugins(),
i18nContext(),
i18nDynamoDbStorageOperations(),
i18nContentPlugins(),
mockLocalesPlugins(),
elasticsearchClientContext,
richTextFieldPlugin(),
fileManagerPlugins(),
fileManagerDdbEsPlugins(),
/**
* Mock physical file storage plugin.
*/
new FilePhysicalStoragePlugin({
// eslint-disable-next-line
upload: async () => {},
// eslint-disable-next-line
delete: async () => {}
})
);
// 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 {
tenant,
until,
handler,
invoke,
clearElasticsearch,
createElasticsearchIndice,
// Files
async createFile(variables, fields: string[] = []) {
return invoke({ body: { query: CREATE_FILE(fields), variables } });
},
async updateFile(variables, fields: string[] = []) {
return invoke({ body: { query: UPDATE_FILE(fields), variables } });
},
async createFiles(variables, fields: string[] = []) {
return invoke({ body: { query: CREATE_FILES(fields), variables } });
},
async deleteFile(variables) {
return invoke({ body: { query: DELETE_FILE, variables } });
},
async getFile(variables, fields: string[] = []) {
return invoke({ body: { query: GET_FILE(fields), variables } });
},
async listFiles(variables = {}, fields: string[] = []) {
return invoke({ body: { query: LIST_FILES(fields), variables } });
},
// File Manager settings
async isInstalled(variables) {
return invoke({ body: { query: IS_INSTALLED, variables } });
},
async install(variables) {
return invoke({ body: { query: INSTALL, variables } });
},
async getSettings(variables = {}) {
return invoke({ body: { query: GET_SETTINGS, variables } });
},
async updateSettings(variables) {
return invoke({ body: { query: UPDATE_SETTINGS, variables } });
}
};
};