UNPKG

15 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __importDefault = (this && this.__importDefault) || function (mod) {
11 return (mod && mod.__esModule) ? mod : { "default": mod };
12};
13Object.defineProperty(exports, "__esModule", { value: true });
14const graphql_1 = require("graphql");
15const types_1 = __importDefault(require("../types"));
16const libs_1 = require("../libs");
17const entry_component_1 = require("./entry-component");
18const datalayer_libs_1 = require("./datalayer-libs");
19exports.DATALAYER_INSTANCE_TYPE = "DataLayerComponent";
20;
21/**
22 * identifies a component as a DataLayer
23 *
24 * @param component to be tested
25 */
26function isDataLayer(component) {
27 return component !== undefined && component.instanceType === exports.DATALAYER_INSTANCE_TYPE;
28}
29exports.isDataLayer = isDataLayer;
30;
31exports.default = (props) => {
32 const componentProps = {
33 infrastructureType: types_1.default.INFRASTRUCTURE_TYPE_COMPONENT,
34 instanceType: exports.DATALAYER_INSTANCE_TYPE,
35 instanceId: props.id
36 };
37 //const listEntities = getChildrenArray(props).filter(child => isEntity(child));
38 //const entries = getChildrenArray(props).filter(child => isEntry(child));
39 const entries = libs_1.findComponentRecursively(props.children, entry_component_1.isEntry);
40 const complementedProps = {};
41 /**
42 * create the
43 * @type {{queries: {}, mutations: {}}}
44 */
45 const datalayerProps = {
46 entries: entries,
47 mutations: (resolveWithData) => entries.reduce((result, entry) => {
48 result[entry.getSetMutationName()] = {
49 args: entry.createEntryArgs(),
50 type: entry.createEntryType("set_"),
51 resolve: (source, args, context, info) => {
52 if (!resolveWithData) {
53 return entry.id;
54 }
55 //console.log("resolve: ", resolveWithData, source, context, info, args);
56 // This context gets the data from the context put into the <Query/> or Mutation...
57 //console.log("context: ", context);
58 const result = entry.setEntry(args, context, process.env.TABLE_NAME, complementedProps["isOffline"]);
59 //console.log("result: ", result);
60 return result;
61 }
62 };
63 result[entry.getDeleteMutationName()] = {
64 args: entry.createEntryArgs(),
65 type: entry.createEntryType("delete_"),
66 resolve: (source, args, context, info) => {
67 if (!resolveWithData) {
68 return entry.id;
69 }
70 //console.log("resolve: ", resolveWithData, source, context, info, args);
71 // This context gets the data from the context put into the <Query/> or Mutation...
72 //console.log("context: ", context);
73 const result = entry.deleteEntry(args, context, process.env.TABLE_NAME, complementedProps["isOffline"]);
74 console.log("result: ", result);
75 return result;
76 }
77 };
78 //console.log("mutation definition: ", result["set_"+entry.id]);
79 return result;
80 }, {}),
81 queries: (resolveWithData) => entries.reduce((result, entry) => {
82 const listType = entry.createEntryType("list_");
83 const getType = entry.createEntryType("get_");
84 //console.log("listType: ", listType);
85 //console.log("dl-comp-props: ", complementedProps["isOffline"], datalayerProps["isOffline"])
86 // list all the items, specifying the primaryKey
87 const inputArgs = {};
88 inputArgs[entry.primaryKey] = { name: entry.primaryKey, type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString) };
89 result[entry.getPrimaryListQueryName()] = {
90 args: inputArgs,
91 type: resolveWithData ? new graphql_1.GraphQLList(listType) : listType,
92 resolve: (source, args, context, info) => {
93 console.log("resolve list: ", resolveWithData, source, args, context, complementedProps["isOffline"]);
94 if (!resolveWithData) {
95 return entry.id;
96 }
97 return entry.listEntries(args, context, process.env.TABLE_NAME, "pk", complementedProps["isOffline"]);
98 }
99 };
100 // list all the items, specifying the RANGE
101 const inputRangeArgs = {};
102 inputRangeArgs[entry.rangeKey] = { name: entry.rangeKey, type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString) };
103 result[entry.getRangeListQueryName()] = {
104 args: inputRangeArgs,
105 type: resolveWithData ? new graphql_1.GraphQLList(listType) : listType,
106 resolve: (source, args, context, info) => {
107 console.log("resolve: ", resolveWithData, source, args, context, complementedProps["isOffline"]);
108 if (!resolveWithData) {
109 return entry.id;
110 }
111 return entry.listEntries(args, context, process.env.TABLE_NAME, "sk", complementedProps["isOffline"]);
112 }
113 };
114 const inputArgsGet = {};
115 if (entry.primaryKey) {
116 inputArgsGet[entry.primaryKey] = { name: entry.primaryKey, type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString) };
117 }
118 if (entry.rangeKey) {
119 inputArgsGet[entry.rangeKey] = { name: entry.rangeKey, type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString) };
120 }
121 result[entry.getGetQueryName()] = {
122 args: inputArgsGet,
123 type: getType,
124 resolve: (source, args, context, info) => {
125 console.log("resolve: ", resolveWithData, source, args, context);
126 if (!resolveWithData) {
127 return entry.id;
128 }
129 return entry.getEntry(args, context, process.env.TABLE_NAME, complementedProps["isOffline"]);
130 }
131 };
132 const scanRangeArgs = {};
133 scanRangeArgs[`start_${entry.rangeKey}`] = { name: `start_${entry.rangeKey}`, type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString) };
134 scanRangeArgs[`end_${entry.rangeKey}`] = { name: `end_${entry.rangeKey}`, type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString) };
135 // scan the table
136 result[entry.getRangeScanName()] = {
137 args: scanRangeArgs,
138 type: resolveWithData ? new graphql_1.GraphQLList(listType) : listType,
139 resolve: (source, args, context, info) => {
140 console.log("resolve scan: ", resolveWithData, source, args, context);
141 if (!resolveWithData) {
142 return entry.id;
143 }
144 return entry.scan(args, context, process.env.TABLE_NAME, "sk", complementedProps["isOffline"]);
145 }
146 };
147 const scanPrimaryArgs = {};
148 scanPrimaryArgs[`start_${entry.primaryKey}`] = { name: `start_${entry.primaryKey}`, type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString) };
149 scanPrimaryArgs[`end_${entry.primaryKey}`] = { name: `end_${entry.primaryKey}`, type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString) };
150 // scan the table
151 result[entry.getPrimaryScanName()] = {
152 args: scanPrimaryArgs,
153 type: resolveWithData ? new graphql_1.GraphQLList(listType) : listType,
154 resolve: (source, args, context, info) => {
155 console.log("resolve scan: ", resolveWithData, source, args, context);
156 if (!resolveWithData) {
157 return entry.id;
158 }
159 return entry.scan(args, context, process.env.TABLE_NAME, "pk", complementedProps["isOffline"]);
160 }
161 };
162 const scanAllArgs = { scanall: { name: "scanall", type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString) } };
163 // scan the table
164 result[entry.getScanName()] = {
165 args: scanAllArgs,
166 type: resolveWithData ? new graphql_1.GraphQLList(listType) : listType,
167 resolve: (source, args, context, info) => {
168 console.log("resolve scan: ", resolveWithData, source, args, context);
169 if (!resolveWithData) {
170 return entry.id;
171 }
172 return entry.scan(args, context, process.env.TABLE_NAME, "pk", complementedProps["isOffline"]);
173 }
174 };
175 return result;
176 }, {}),
177 /*
178 getEntryDataFields: (entryId) => {
179 const entry = entries.find(entry => entry.id === entryId);
180 if (entry !== undefined) {
181 return entry.createEntryFields()
182 };
183
184 console.warn("could not find entry: ",entryId);
185 return {};
186 },*/
187 // TODO forward this request to the Entry and let the entry handle the whole request!
188 getEntryListQuery: (entryId, dictKey) => {
189 const entry = entries.find(entry => entry.id === entryId);
190 if (entry !== undefined) {
191 return entry.getEntryListQuery(dictKey);
192 }
193 ;
194 console.warn("could not find entry: ", entryId);
195 return {};
196 /*
197 const fields = datalayerProps.getEntryDataFields(entryId);
198 //console.log("fields: ", fields);
199
200 return getEntryListQuery(
201 entryId,
202 dictKey,
203 fields
204 );*/
205 },
206 getEntryQuery: (entryId, dictKey) => {
207 const entry = entries.find(entry => entry.id === entryId);
208 if (entry !== undefined) {
209 return entry.getEntryQuery(dictKey);
210 }
211 ;
212 console.warn("could not find entry: ", entryId);
213 return {};
214 },
215 getEntryScanQuery: (entryId, dictKey) => {
216 const entry = entries.find(entry => entry.id === entryId);
217 if (entry !== undefined) {
218 return entry.getEntryScanQuery(dictKey);
219 }
220 ;
221 console.warn("could not find entry: ", entryId);
222 return {};
223 },
224 setEntryMutation: (entryId, values) => {
225 const entry = entries.find(entry => entry.id === entryId);
226 if (entry !== undefined) {
227 return entry.setEntryMutation(values);
228 }
229 ;
230 console.warn("could not find entry: ", entryId);
231 return {};
232 },
233 deleteEntryMutation: (entryId, values) => {
234 const entry = entries.find(entry => entry.id === entryId);
235 if (entry !== undefined) {
236 return entry.deleteEntryMutation(values);
237 }
238 ;
239 console.warn("could not find entry: ", entryId);
240 return {};
241 },
242 updateEntryQuery: (entryId, fDictKey) => {
243 return {
244 entryId: entryId,
245 getEntryQuery: () => datalayerProps.getEntryQuery(entryId, fDictKey({})),
246 setEntryMutation: (oldData) => datalayerProps.setEntryMutation(entryId, fDictKey(oldData)),
247 };
248 },
249 setClient: (client) => {
250 complementedProps["client"] = client;
251 },
252 setOffline: (offline) => {
253 complementedProps["isOffline"] = offline;
254 }
255 };
256 const schemaProps = {
257 getSchema: (resolveWithData) => new graphql_1.GraphQLSchema({
258 query: new graphql_1.GraphQLObjectType({
259 name: 'RootQueryType',
260 fields: datalayerProps.queries(resolveWithData)
261 }), mutation: new graphql_1.GraphQLObjectType({
262 name: 'RootMutationType',
263 fields: datalayerProps.mutations(resolveWithData)
264 })
265 })
266 };
267 // we need to provide the DataLayerId to webApps, these may be anywhere in the tree, not
268 // only direct children. So rather than mapping the children, we need to change them
269 libs_1.findComponentRecursively(props.children, (child) => child.setDataLayerId !== undefined).forEach(child => {
270 child.setDataLayerId(props.id);
271 });
272 libs_1.findComponentRecursively(props.children, (child) => child.setStoreData !== undefined).forEach(child => {
273 child.setStoreData(function (pkEntity, pkVal, skEntity, skVal, jsonData) {
274 return __awaiter(this, void 0, void 0, function* () {
275 return yield datalayer_libs_1.setEntry(process.env.TABLE_NAME, //"code-architect-dev-data-layer",
276 pkEntity, // schema.Entry.ENTITY, //pkEntity
277 pkVal, // pkId
278 skEntity, //schema.Data.ENTITY, // skEntity
279 skVal, // skId
280 jsonData, // jsonData
281 complementedProps["isOffline"]);
282 });
283 });
284 child.setGetData(function (pkEntity, pkVal, skEntity, skVal) {
285 return __awaiter(this, void 0, void 0, function* () {
286 if (pkVal !== undefined) {
287 return yield datalayer_libs_1.ddbGetEntry(process.env.TABLE_NAME, //"code-architect-dev-data-layer",
288 pkEntity, // schema.Entry.ENTITY, //pkEntity
289 pkVal, // pkId
290 skEntity, //schema.Data.ENTITY, // skEntity
291 skVal, // skId
292 complementedProps["isOffline"]);
293 }
294 else {
295 return datalayer_libs_1.ddbListEntries(process.env.TABLE_NAME, //tableName
296 "sk", //key
297 skEntity, // entity
298 skVal, //value,
299 pkEntity, // rangeEntity
300 complementedProps["isOffline"]);
301 }
302 });
303 });
304 });
305 return Object.assign({}, props, componentProps, datalayerProps, schemaProps, complementedProps);
306};
307//# sourceMappingURL=datalayer-component.js.map
\No newline at end of file