@aws-amplify/api-graphql 4.0.1-server-generate-client.1e317a1.0 → 4.0.1-ssr-api.df60e41.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/lib/GraphQLAPI.d.ts +1 -1
  2. package/lib/GraphQLAPI.js +25 -30
  3. package/lib/Providers/AWSAppSyncRealTimeProvider/index.d.ts +1 -1
  4. package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +468 -584
  5. package/lib/Providers/constants.js +4 -5
  6. package/lib/index.js +5 -6
  7. package/lib/internals/InternalGraphQLAPI.d.ts +1 -1
  8. package/lib/internals/InternalGraphQLAPI.js +203 -263
  9. package/lib/internals/index.d.ts +0 -1
  10. package/lib/internals/index.js +5 -8
  11. package/lib/internals/v6.js +3 -4
  12. package/lib/tsconfig.tsbuildinfo +6798 -1
  13. package/lib/types/PubSub.d.ts +2 -2
  14. package/lib/types/PubSub.js +2 -3
  15. package/lib/types/index.d.ts +39 -28
  16. package/lib/types/index.js +3 -4
  17. package/lib/utils/ConnectionStateMonitor.d.ts +3 -3
  18. package/lib/utils/ConnectionStateMonitor.js +51 -65
  19. package/lib/utils/ReachabilityMonitor/index.d.ts +1 -1
  20. package/lib/utils/ReachabilityMonitor/index.js +2 -4
  21. package/lib/utils/ReachabilityMonitor/index.native.d.ts +1 -1
  22. package/lib/utils/ReachabilityMonitor/index.native.js +4 -8
  23. package/lib/utils/ReconnectionMonitor.js +21 -26
  24. package/lib/utils/errors/APIError.js +7 -12
  25. package/lib/utils/errors/assertValidationError.js +4 -5
  26. package/lib/utils/errors/index.js +4 -5
  27. package/lib/utils/errors/validation.js +6 -8
  28. package/lib/utils/index.js +2 -3
  29. package/lib/utils/resolveConfig.d.ts +1 -1
  30. package/lib/utils/resolveConfig.js +10 -13
  31. package/lib/utils/resolveLibraryOptions.d.ts +1 -1
  32. package/lib/utils/resolveLibraryOptions.js +4 -7
  33. package/lib-esm/GraphQLAPI.d.ts +1 -1
  34. package/lib-esm/GraphQLAPI.js +25 -29
  35. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +1 -1
  36. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +459 -575
  37. package/lib-esm/Providers/constants.js +10 -10
  38. package/lib-esm/internals/InternalGraphQLAPI.d.ts +1 -1
  39. package/lib-esm/internals/InternalGraphQLAPI.js +197 -256
  40. package/lib-esm/internals/index.d.ts +0 -1
  41. package/lib-esm/internals/index.js +0 -1
  42. package/lib-esm/internals/v6.js +1 -1
  43. package/lib-esm/tsconfig.tsbuildinfo +6798 -1
  44. package/lib-esm/types/PubSub.d.ts +2 -2
  45. package/lib-esm/types/index.d.ts +39 -28
  46. package/lib-esm/types/index.js +1 -1
  47. package/lib-esm/utils/ConnectionStateMonitor.d.ts +3 -3
  48. package/lib-esm/utils/ConnectionStateMonitor.js +49 -63
  49. package/lib-esm/utils/ReachabilityMonitor/index.d.ts +1 -1
  50. package/lib-esm/utils/ReachabilityMonitor/index.js +1 -1
  51. package/lib-esm/utils/ReachabilityMonitor/index.native.d.ts +1 -1
  52. package/lib-esm/utils/ReachabilityMonitor/index.native.js +1 -3
  53. package/lib-esm/utils/ReconnectionMonitor.js +19 -24
  54. package/lib-esm/utils/errors/APIError.js +6 -11
  55. package/lib-esm/utils/errors/assertValidationError.js +2 -2
  56. package/lib-esm/utils/errors/validation.js +5 -6
  57. package/lib-esm/utils/resolveConfig.d.ts +1 -1
  58. package/lib-esm/utils/resolveConfig.js +8 -9
  59. package/lib-esm/utils/resolveLibraryOptions.d.ts +1 -1
  60. package/lib-esm/utils/resolveLibraryOptions.js +4 -5
  61. package/package.json +6 -12
  62. package/src/GraphQLAPI.ts +6 -1
  63. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +6 -5
  64. package/src/internals/InternalGraphQLAPI.ts +7 -8
  65. package/src/internals/index.ts +0 -1
  66. package/src/types/index.ts +39 -15
  67. package/lib/internals/APIClient.d.ts +0 -101
  68. package/lib/internals/APIClient.js +0 -443
  69. package/lib/internals/generateClient.d.ts +0 -12
  70. package/lib/internals/generateClient.js +0 -30
  71. package/lib/internals/generateModelsProperty.d.ts +0 -4
  72. package/lib/internals/generateModelsProperty.js +0 -169
  73. package/lib/internals/types.d.ts +0 -10
  74. package/lib/internals/types.js +0 -2
  75. package/lib/utils/resolveOwnerFields.d.ts +0 -12
  76. package/lib/utils/resolveOwnerFields.js +0 -63
  77. package/lib-esm/internals/APIClient.d.ts +0 -101
  78. package/lib-esm/internals/APIClient.js +0 -432
  79. package/lib-esm/internals/generateClient.d.ts +0 -12
  80. package/lib-esm/internals/generateClient.js +0 -26
  81. package/lib-esm/internals/generateModelsProperty.d.ts +0 -4
  82. package/lib-esm/internals/generateModelsProperty.js +0 -165
  83. package/lib-esm/internals/types.d.ts +0 -10
  84. package/lib-esm/internals/types.js +0 -1
  85. package/lib-esm/utils/resolveOwnerFields.d.ts +0 -12
  86. package/lib-esm/utils/resolveOwnerFields.js +0 -59
  87. package/src/internals/APIClient.ts +0 -583
  88. package/src/internals/generateClient.ts +0 -30
  89. package/src/internals/generateModelsProperty.ts +0 -192
  90. package/src/internals/types.ts +0 -15
  91. package/src/utils/resolveOwnerFields.ts +0 -65
@@ -1,12 +0,0 @@
1
- import { ResourcesConfig } from '@aws-amplify/core';
2
- type GraphQLConfig = Exclude<ResourcesConfig['API'], undefined>['GraphQL'];
3
- type ModelIntrospectionSchema = Exclude<Exclude<GraphQLConfig, undefined>['modelIntrospection'], undefined>;
4
- type Model = ModelIntrospectionSchema['models'][string];
5
- /**
6
- * Given an introspection schema model, returns all owner fields.
7
- *
8
- * @param model Model from an introspection schema
9
- * @returns List of owner field names
10
- */
11
- export declare function resolveOwnerFields(model: Model): string[];
12
- export {};
@@ -1,63 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveOwnerFields = void 0;
4
- var tslib_1 = require("tslib");
5
- /**
6
- * Given an introspection schema model, returns all owner fields.
7
- *
8
- * @param model Model from an introspection schema
9
- * @returns List of owner field names
10
- */
11
- function resolveOwnerFields(model) {
12
- var e_1, _a, e_2, _b;
13
- var ownerFields = new Set();
14
- try {
15
- for (var _c = tslib_1.__values(model.attributes || []), _d = _c.next(); !_d.done; _d = _c.next()) {
16
- var attr = _d.value;
17
- if (isAuthAttribute(attr)) {
18
- try {
19
- for (var _e = (e_2 = void 0, tslib_1.__values(attr.properties.rules)), _f = _e.next(); !_f.done; _f = _e.next()) {
20
- var rule = _f.value;
21
- if (rule.allow === 'owner') {
22
- ownerFields.add(rule.ownerField || 'owner');
23
- }
24
- }
25
- }
26
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
27
- finally {
28
- try {
29
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
30
- }
31
- finally { if (e_2) throw e_2.error; }
32
- }
33
- }
34
- }
35
- }
36
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
37
- finally {
38
- try {
39
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
40
- }
41
- finally { if (e_1) throw e_1.error; }
42
- }
43
- return Array.from(ownerFields);
44
- }
45
- exports.resolveOwnerFields = resolveOwnerFields;
46
- /**
47
- * Type guard that identifies an auth attribute with an attached rules list that
48
- * specifies an `allow` attribute at a minimum.
49
- *
50
- * @param attribute Any object. Ideally a model introspection schema model attribute
51
- * @returns True if given object is an auth attribute
52
- */
53
- function isAuthAttribute(attribute) {
54
- var _a, _b;
55
- if ((attribute === null || attribute === void 0 ? void 0 : attribute.type) === 'auth') {
56
- if (typeof (attribute === null || attribute === void 0 ? void 0 : attribute.properties) === 'object') {
57
- if (Array.isArray((_a = attribute === null || attribute === void 0 ? void 0 : attribute.properties) === null || _a === void 0 ? void 0 : _a.rules)) {
58
- return ((_b = attribute === null || attribute === void 0 ? void 0 : attribute.properties) === null || _b === void 0 ? void 0 : _b.rules).every(function (rule) { return !!rule.allow; });
59
- }
60
- }
61
- }
62
- return false;
63
- }
@@ -1,101 +0,0 @@
1
- type ListArgs = {
2
- selectionSet?: string[];
3
- filter?: {};
4
- };
5
- /**
6
- *
7
- * @param GraphQL response object
8
- * @returns response object with `items` properties flattened
9
- */
10
- export declare const flattenItems: (obj: Record<string, any>) => Record<string, any>;
11
- export declare function initializeModel(client: any, modelName: string, result: any[], modelIntrospection: any): any[];
12
- export declare const graphQLOperationsInfo: {
13
- CREATE: {
14
- operationPrefix: "create";
15
- usePlural: boolean;
16
- };
17
- READ: {
18
- operationPrefix: "get";
19
- usePlural: boolean;
20
- };
21
- UPDATE: {
22
- operationPrefix: "update";
23
- usePlural: boolean;
24
- };
25
- DELETE: {
26
- operationPrefix: "delete";
27
- usePlural: boolean;
28
- };
29
- LIST: {
30
- operationPrefix: "list";
31
- usePlural: boolean;
32
- };
33
- ONCREATE: {
34
- operationPrefix: "onCreate";
35
- usePlural: boolean;
36
- };
37
- ONUPDATE: {
38
- operationPrefix: "onUpdate";
39
- usePlural: boolean;
40
- };
41
- ONDELETE: {
42
- operationPrefix: "onDelete";
43
- usePlural: boolean;
44
- };
45
- };
46
- export type ModelOperation = keyof typeof graphQLOperationsInfo;
47
- /**
48
- * Generates nested Custom Selection Set IR from path
49
- *
50
- * @param modelIntrospection
51
- * @param modelName
52
- * @param selectionSet - array of object paths
53
- * @example
54
- * ### Given
55
- * `selectionSet = ['id', 'comments.post.id']`
56
- * ### Returns
57
- * ```ts
58
- * {
59
- * id: '',
60
- * comments: {
61
- * items: { post: { id: '' } }
62
- * }
63
- * }
64
- * ```
65
- */
66
- export declare function customSelectionSetToIR(modelIntrospection: any, modelName: string, selectionSet: string[]): Record<string, string | object>;
67
- /**
68
- * Stringifies selection set IR
69
- * * @example
70
- * ### Given
71
- * ```ts
72
- * {
73
- * id: '',
74
- * comments: {
75
- * items: { post: { id: '' } }
76
- * }
77
- * }
78
- * ```
79
- * ### Returns
80
- * `'id comments { items { post { id } } }'`
81
- */
82
- export declare function selectionSetIRToString(obj: Record<string, string | any>): string;
83
- export declare function generateSelectionSet(modelIntrospection: any, modelName: string, selectionSet?: string[]): string;
84
- export declare function generateGraphQLDocument(modelIntrospection: any, modelName: string, modelOperation: ModelOperation, listArgs?: ListArgs): string;
85
- export declare function buildGraphQLVariables(modelDefinition: any, operation: ModelOperation, arg: any, modelIntrospection: any): object;
86
- /**
87
- * Iterates over mutation input values and resolves any model inputs to their corresponding join fields/values
88
- *
89
- * @example
90
- * ### Usage
91
- * ```ts
92
- * const result = normalizeMutationInput({ post: post }, model, modelDefinition);
93
- * ```
94
- * ### Result
95
- * ```ts
96
- * { postId: "abc123" }
97
- * ```
98
- *
99
- */
100
- export declare function normalizeMutationInput(mutationInput: any, model: any, modelDefinition: any): Record<string, unknown>;
101
- export {};
@@ -1,432 +0,0 @@
1
- import { __assign, __read, __spreadArray, __values } from "tslib";
2
- // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
- // SPDX-License-Identifier: Apache-2.0
4
- import { resolveOwnerFields } from '../utils/resolveOwnerFields';
5
- var connectionType = {
6
- HAS_ONE: 'HAS_ONE',
7
- HAS_MANY: 'HAS_MANY',
8
- BELONGS_TO: 'BELONGS_TO',
9
- };
10
- /**
11
- *
12
- * @param GraphQL response object
13
- * @returns response object with `items` properties flattened
14
- */
15
- export var flattenItems = function (obj) {
16
- var res = {};
17
- Object.entries(obj).forEach(function (_a) {
18
- var _b = __read(_a, 2), prop = _b[0], value = _b[1];
19
- if (typeof value === 'object' && value !== null) {
20
- if (value.items !== undefined) {
21
- res[prop] = value.items.map(function (item) {
22
- return flattenItems(item);
23
- });
24
- return;
25
- }
26
- res[prop] = flattenItems(value);
27
- return;
28
- }
29
- res[prop] = value;
30
- });
31
- return res;
32
- };
33
- // TODO: this should accept single result to support CRUD methods; create helper for array/list
34
- export function initializeModel(client, modelName, result, modelIntrospection) {
35
- var introModel = modelIntrospection.models[modelName];
36
- var introModelFields = introModel.fields;
37
- var modelFields = Object.entries(introModelFields)
38
- .filter(function (_a) {
39
- var _b;
40
- var _c = __read(_a, 2), _ = _c[0], field = _c[1];
41
- return ((_b = field === null || field === void 0 ? void 0 : field.type) === null || _b === void 0 ? void 0 : _b.model) !== undefined;
42
- })
43
- .map(function (_a) {
44
- var _b = __read(_a, 1), fieldName = _b[0];
45
- return fieldName;
46
- });
47
- return result.map(function (record) {
48
- var e_1, _a;
49
- var _b, _c;
50
- var initializedRelationalFields = {};
51
- var _loop_1 = function (field) {
52
- var relatedModelName = introModelFields[field].type.model;
53
- var relatedModel = modelIntrospection.models[relatedModelName];
54
- var relatedModelPKFieldName = relatedModel.primaryKeyInfo.primaryKeyFieldName;
55
- var relatedModelSKFieldNames = relatedModel.primaryKeyInfo.sortKeyFieldNames;
56
- var relationType = introModelFields[field].association.connectionType;
57
- var connectionFields = introModelFields[field].association.associatedWith;
58
- var targetNames = ((_b = introModelFields[field].association) === null || _b === void 0 ? void 0 : _b.targetNames) || [];
59
- switch (relationType) {
60
- case connectionType.HAS_ONE:
61
- case connectionType.BELONGS_TO:
62
- var sortKeyValues_1 = relatedModelSKFieldNames.reduce(function (acc, curVal) {
63
- if (record[curVal]) {
64
- return (acc[curVal] = record[curVal]);
65
- }
66
- }, {});
67
- initializedRelationalFields[field] = function () {
68
- var _a;
69
- if (record[targetNames[0]]) {
70
- return client.models[relatedModelName].get(__assign((_a = {}, _a[relatedModelPKFieldName] = record[targetNames[0]], _a), sortKeyValues_1));
71
- }
72
- return undefined;
73
- };
74
- break;
75
- case connectionType.HAS_MANY:
76
- var parentPk_1 = introModel.primaryKeyInfo.primaryKeyFieldName;
77
- var parentSK_1 = introModel.primaryKeyInfo.sortKeyFieldNames;
78
- // M:N check - TODO: refactor
79
- if ((_c = relatedModel.fields[connectionFields[0]]) === null || _c === void 0 ? void 0 : _c.type.model) {
80
- var relatedTargetNames = relatedModel.fields[connectionFields[0]].association.targetNames;
81
- var hasManyFilter_1 = relatedTargetNames.map(function (field, idx) {
82
- var _a, _b;
83
- if (idx === 0) {
84
- return _a = {}, _a[field] = { eq: record[parentPk_1] }, _a;
85
- }
86
- return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
87
- });
88
- initializedRelationalFields[field] = function () {
89
- if (record[parentPk_1]) {
90
- return client.models[relatedModelName].list({
91
- filter: { and: hasManyFilter_1 },
92
- });
93
- }
94
- return [];
95
- };
96
- break;
97
- }
98
- var hasManyFilter_2 = connectionFields.map(function (field, idx) {
99
- var _a, _b;
100
- if (idx === 0) {
101
- return _a = {}, _a[field] = { eq: record[parentPk_1] }, _a;
102
- }
103
- return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
104
- });
105
- initializedRelationalFields[field] = function () {
106
- if (record[parentPk_1]) {
107
- return client.models[relatedModelName].list({
108
- filter: { and: hasManyFilter_2 },
109
- });
110
- }
111
- return [];
112
- };
113
- break;
114
- default:
115
- break;
116
- }
117
- };
118
- try {
119
- for (var modelFields_1 = __values(modelFields), modelFields_1_1 = modelFields_1.next(); !modelFields_1_1.done; modelFields_1_1 = modelFields_1.next()) {
120
- var field = modelFields_1_1.value;
121
- _loop_1(field);
122
- }
123
- }
124
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
125
- finally {
126
- try {
127
- if (modelFields_1_1 && !modelFields_1_1.done && (_a = modelFields_1.return)) _a.call(modelFields_1);
128
- }
129
- finally { if (e_1) throw e_1.error; }
130
- }
131
- return __assign(__assign({}, record), initializedRelationalFields);
132
- });
133
- }
134
- export var graphQLOperationsInfo = {
135
- CREATE: { operationPrefix: 'create', usePlural: false },
136
- READ: { operationPrefix: 'get', usePlural: false },
137
- UPDATE: { operationPrefix: 'update', usePlural: false },
138
- DELETE: { operationPrefix: 'delete', usePlural: false },
139
- LIST: { operationPrefix: 'list', usePlural: true },
140
- ONCREATE: { operationPrefix: 'onCreate', usePlural: false },
141
- ONUPDATE: { operationPrefix: 'onUpdate', usePlural: false },
142
- ONDELETE: { operationPrefix: 'onDelete', usePlural: false },
143
- };
144
- var graphQLDocumentsCache = new Map();
145
- var SELECTION_SET_WILDCARD = '*';
146
- function defaultSelectionSetForModel(modelDefinition) {
147
- // fields that are explicitly part of the graphql schema; not
148
- // inferred from owner auth rules.
149
- var fields = modelDefinition.fields;
150
- var explicitFields = Object.values(fields)
151
- .map(function (_a) {
152
- var type = _a.type, name = _a.name;
153
- return typeof type === 'string' && name;
154
- }) // Default selection set omits model fields
155
- .filter(Boolean);
156
- // fields used for owner auth rules that may or may not also
157
- // be explicit on the model.
158
- var ownerFields = resolveOwnerFields(modelDefinition);
159
- return Array.from(new Set(explicitFields.concat(ownerFields)));
160
- }
161
- var FIELD_IR = '';
162
- /**
163
- * Generates nested Custom Selection Set IR from path
164
- *
165
- * @param modelIntrospection
166
- * @param modelName
167
- * @param selectionSet - array of object paths
168
- * @example
169
- * ### Given
170
- * `selectionSet = ['id', 'comments.post.id']`
171
- * ### Returns
172
- * ```ts
173
- * {
174
- * id: '',
175
- * comments: {
176
- * items: { post: { id: '' } }
177
- * }
178
- * }
179
- * ```
180
- */
181
- export function customSelectionSetToIR(modelIntrospection, modelName, selectionSet) {
182
- var modelDefinition = modelIntrospection[modelName];
183
- var modelFields = modelDefinition.fields;
184
- return selectionSet.reduce(function (resultObj, path) {
185
- var _a, _b, _c;
186
- var _d = __read(path.split('.')), fieldName = _d[0], nested = _d[1], rest = _d.slice(2);
187
- if (nested) {
188
- var relatedModel = (_b = (_a = modelFields[fieldName]) === null || _a === void 0 ? void 0 : _a.type) === null || _b === void 0 ? void 0 : _b.model;
189
- if (!relatedModel) {
190
- // TODO: may need to change this to support custom types
191
- throw Error("".concat(fieldName, " is not a model field"));
192
- }
193
- var relatedModelDefinition = modelIntrospection[relatedModel];
194
- var selectionSet_1 = nested === SELECTION_SET_WILDCARD
195
- ? defaultSelectionSetIR(relatedModelDefinition)
196
- : // if we have a path like 'field.anotherField' recursively build up selection set IR
197
- customSelectionSetToIR(modelIntrospection, relatedModel, [
198
- __spreadArray([nested], __read(rest), false).join('.'),
199
- ]);
200
- if ((_c = modelFields[fieldName]) === null || _c === void 0 ? void 0 : _c.isArray) {
201
- var existing = resultObj[fieldName] || {
202
- items: {},
203
- };
204
- var merged_1 = __assign(__assign({}, existing.items), selectionSet_1);
205
- resultObj[fieldName] = { items: merged_1 };
206
- return resultObj;
207
- }
208
- var existingItems = resultObj[fieldName] || {};
209
- var merged = __assign(__assign({}, existingItems), selectionSet_1);
210
- resultObj[fieldName] = merged;
211
- return resultObj;
212
- }
213
- var exists = Boolean(modelFields[fieldName]);
214
- if (!exists) {
215
- throw Error("".concat(fieldName, " is not a field of model ").concat(modelName));
216
- }
217
- resultObj[fieldName] = FIELD_IR;
218
- return resultObj;
219
- }, {});
220
- }
221
- var defaultSelectionSetIR = function (relatedModelDefinition) {
222
- var defaultSelectionSet = defaultSelectionSetForModel(relatedModelDefinition);
223
- var reduced = defaultSelectionSet.reduce(function (acc, curVal) {
224
- acc[curVal] = FIELD_IR;
225
- return acc;
226
- }, {});
227
- return reduced;
228
- };
229
- /**
230
- * Stringifies selection set IR
231
- * * @example
232
- * ### Given
233
- * ```ts
234
- * {
235
- * id: '',
236
- * comments: {
237
- * items: { post: { id: '' } }
238
- * }
239
- * }
240
- * ```
241
- * ### Returns
242
- * `'id comments { items { post { id } } }'`
243
- */
244
- export function selectionSetIRToString(obj) {
245
- var res = [];
246
- Object.entries(obj).forEach(function (_a) {
247
- var _b = __read(_a, 2), fieldName = _b[0], value = _b[1];
248
- if (value === FIELD_IR) {
249
- res.push(fieldName);
250
- }
251
- else if (typeof value === 'object' && value !== null) {
252
- if (value === null || value === void 0 ? void 0 : value.items) {
253
- res.push(fieldName, '{', 'items', '{', selectionSetIRToString(value.items), '}', '}');
254
- }
255
- else {
256
- res.push(fieldName, '{', selectionSetIRToString(value), '}');
257
- }
258
- }
259
- });
260
- return res.join(' ');
261
- }
262
- export function generateSelectionSet(modelIntrospection, modelName, selectionSet) {
263
- var modelDefinition = modelIntrospection[modelName];
264
- if (!selectionSet) {
265
- return defaultSelectionSetForModel(modelDefinition).join(' ');
266
- }
267
- var selSetIr = customSelectionSetToIR(modelIntrospection, modelName, selectionSet);
268
- var selSetString = selectionSetIRToString(selSetIr);
269
- return selSetString;
270
- }
271
- export function generateGraphQLDocument(modelIntrospection, modelName, modelOperation, listArgs) {
272
- var _a;
273
- var _b, _c;
274
- var modelDefinition = modelIntrospection[modelName];
275
- var name = modelDefinition.name, pluralName = modelDefinition.pluralName, fields = modelDefinition.fields, _d = modelDefinition.primaryKeyInfo, isCustomPrimaryKey = _d.isCustomPrimaryKey, primaryKeyFieldName = _d.primaryKeyFieldName, sortKeyFieldNames = _d.sortKeyFieldNames;
276
- var _e = graphQLOperationsInfo[modelOperation], operationPrefix = _e.operationPrefix, usePlural = _e.usePlural;
277
- var selectionSet = (listArgs || {}).selectionSet;
278
- var fromCache = (_b = graphQLDocumentsCache.get(name)) === null || _b === void 0 ? void 0 : _b.get(modelOperation);
279
- if (fromCache !== undefined) {
280
- return fromCache;
281
- }
282
- if (!graphQLDocumentsCache.has(name)) {
283
- graphQLDocumentsCache.set(name, new Map());
284
- }
285
- var graphQLFieldName = "".concat(operationPrefix).concat(usePlural ? pluralName : name);
286
- var graphQLOperationType;
287
- var graphQLSelectionSet;
288
- var graphQLArguments;
289
- var selectionSetFields = generateSelectionSet(modelIntrospection, modelName, selectionSet);
290
- switch (modelOperation) {
291
- case 'CREATE':
292
- case 'UPDATE':
293
- case 'DELETE':
294
- graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = {
295
- input: "".concat(operationPrefix.charAt(0).toLocaleUpperCase() +
296
- operationPrefix.slice(1)).concat(name, "Input!"),
297
- });
298
- graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'mutation');
299
- case 'READ':
300
- graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = isCustomPrimaryKey
301
- ? __spreadArray([primaryKeyFieldName], __read(sortKeyFieldNames), false).reduce(function (acc, fieldName) {
302
- acc[fieldName] = fields[fieldName].type;
303
- return acc;
304
- }, {})
305
- : (_a = {},
306
- _a[primaryKeyFieldName] = "".concat(fields[primaryKeyFieldName].type, "!"),
307
- _a));
308
- graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = selectionSetFields);
309
- case 'LIST':
310
- graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = {
311
- filter: "Model".concat(name, "FilterInput"),
312
- });
313
- graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'query');
314
- graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = "items { ".concat(selectionSetFields, " }"));
315
- case 'ONCREATE':
316
- case 'ONUPDATE':
317
- case 'ONDELETE':
318
- graphQLArguments !== null && graphQLArguments !== void 0 ? graphQLArguments : (graphQLArguments = {
319
- filter: "ModelSubscription".concat(name, "FilterInput"),
320
- });
321
- graphQLOperationType !== null && graphQLOperationType !== void 0 ? graphQLOperationType : (graphQLOperationType = 'subscription');
322
- graphQLSelectionSet !== null && graphQLSelectionSet !== void 0 ? graphQLSelectionSet : (graphQLSelectionSet = selectionSetFields);
323
- }
324
- var graphQLDocument = "".concat(graphQLOperationType).concat(graphQLArguments
325
- ? "(".concat(Object.entries(graphQLArguments).map(function (_a) {
326
- var _b = __read(_a, 2), fieldName = _b[0], type = _b[1];
327
- return "$".concat(fieldName, ": ").concat(type);
328
- }), ")")
329
- : '', " { ").concat(graphQLFieldName).concat(graphQLArguments
330
- ? "(".concat(Object.keys(graphQLArguments).map(function (fieldName) { return "".concat(fieldName, ": $").concat(fieldName); }), ")")
331
- : '', " { ").concat(graphQLSelectionSet, " } }");
332
- (_c = graphQLDocumentsCache.get(name)) === null || _c === void 0 ? void 0 : _c.set(modelOperation, graphQLDocument);
333
- return graphQLDocument;
334
- }
335
- export function buildGraphQLVariables(modelDefinition, operation, arg, modelIntrospection) {
336
- var _a;
337
- var fields = modelDefinition.fields, _b = modelDefinition.primaryKeyInfo, isCustomPrimaryKey = _b.isCustomPrimaryKey, primaryKeyFieldName = _b.primaryKeyFieldName, sortKeyFieldNames = _b.sortKeyFieldNames;
338
- var variables = {};
339
- // TODO: process input
340
- switch (operation) {
341
- case 'CREATE':
342
- variables = {
343
- input: normalizeMutationInput(arg, modelDefinition, modelIntrospection),
344
- };
345
- break;
346
- case 'UPDATE':
347
- // readonly fields are not updated
348
- variables = {
349
- input: Object.fromEntries(Object.entries(normalizeMutationInput(arg, modelDefinition, modelIntrospection)).filter(function (_a) {
350
- var _b = __read(_a, 1), fieldName = _b[0];
351
- var isReadOnly = fields[fieldName].isReadOnly;
352
- return !isReadOnly;
353
- })),
354
- };
355
- break;
356
- case 'READ':
357
- case 'DELETE':
358
- // only identifiers are sent
359
- variables = isCustomPrimaryKey
360
- ? __spreadArray([primaryKeyFieldName], __read(sortKeyFieldNames), false).reduce(function (acc, fieldName) {
361
- acc[fieldName] = arg[fieldName];
362
- return acc;
363
- }, {})
364
- : (_a = {}, _a[primaryKeyFieldName] = arg[primaryKeyFieldName], _a);
365
- if (operation === 'DELETE') {
366
- variables = { input: variables };
367
- }
368
- break;
369
- case 'LIST':
370
- case 'ONCREATE':
371
- case 'ONUPDATE':
372
- case 'ONDELETE':
373
- if (arg === null || arg === void 0 ? void 0 : arg.filter) {
374
- variables = { filter: arg.filter };
375
- }
376
- break;
377
- default:
378
- var exhaustiveCheck = operation;
379
- throw new Error("Unhandled operation case: ".concat(exhaustiveCheck));
380
- }
381
- return variables;
382
- }
383
- /**
384
- * Iterates over mutation input values and resolves any model inputs to their corresponding join fields/values
385
- *
386
- * @example
387
- * ### Usage
388
- * ```ts
389
- * const result = normalizeMutationInput({ post: post }, model, modelDefinition);
390
- * ```
391
- * ### Result
392
- * ```ts
393
- * { postId: "abc123" }
394
- * ```
395
- *
396
- */
397
- export function normalizeMutationInput(mutationInput, model, modelDefinition) {
398
- var fields = model.fields;
399
- var normalized = {};
400
- Object.entries(mutationInput).forEach(function (_a) {
401
- var _b, _c, _d;
402
- var _e = __read(_a, 2), inputFieldName = _e[0], inputValue = _e[1];
403
- var relatedModelName = (_c = (_b = fields[inputFieldName]) === null || _b === void 0 ? void 0 : _b.type) === null || _c === void 0 ? void 0 : _c.model;
404
- if (relatedModelName) {
405
- var association_1 = (_d = fields[inputFieldName]) === null || _d === void 0 ? void 0 : _d.association;
406
- var relatedModelDef = modelDefinition.models[relatedModelName];
407
- var relatedModelPkInfo_1 = relatedModelDef.primaryKeyInfo;
408
- if (association_1.connectionType === connectionType.HAS_ONE) {
409
- association_1.targetNames.forEach(function (targetName, idx) {
410
- var associatedFieldName = association_1.associatedWith[idx];
411
- normalized[targetName] = inputValue[associatedFieldName];
412
- });
413
- }
414
- if (association_1.connectionType === connectionType.BELONGS_TO) {
415
- association_1.targetNames.forEach(function (targetName, idx) {
416
- if (idx === 0) {
417
- var associatedFieldName = relatedModelPkInfo_1.primaryKeyFieldName;
418
- normalized[targetName] = inputValue[associatedFieldName];
419
- }
420
- else {
421
- var associatedFieldName = relatedModelPkInfo_1.sortKeyFieldNames[idx - 1];
422
- normalized[targetName] = inputValue[associatedFieldName];
423
- }
424
- });
425
- }
426
- }
427
- else {
428
- normalized[inputFieldName] = inputValue;
429
- }
430
- });
431
- return normalized;
432
- }
@@ -1,12 +0,0 @@
1
- import { V6Client } from '../types';
2
- import { ClientGenerationParams } from './types';
3
- /**
4
- * @private
5
- *
6
- * Creates a client that can be used to make GraphQL requests, using a provided `AmplifyClassV6`
7
- * compatible context object for config and auth fetching.
8
- *
9
- * @param params
10
- * @returns
11
- */
12
- export declare function generateClient<T extends Record<any, any> = never>(params: ClientGenerationParams): V6Client<T>;
@@ -1,26 +0,0 @@
1
- // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
- // SPDX-License-Identifier: Apache-2.0
3
- import { graphql, cancel, isCancelError } from './v6';
4
- import { generateModelsProperty } from './generateModelsProperty';
5
- import { __amplify } from '../types';
6
- /**
7
- * @private
8
- *
9
- * Creates a client that can be used to make GraphQL requests, using a provided `AmplifyClassV6`
10
- * compatible context object for config and auth fetching.
11
- *
12
- * @param params
13
- * @returns
14
- */
15
- export function generateClient(params) {
16
- var _a;
17
- var client = (_a = {},
18
- _a[__amplify] = params.amplify,
19
- _a.graphql = graphql,
20
- _a.cancel = cancel,
21
- _a.isCancelError = isCancelError,
22
- _a.models = {},
23
- _a);
24
- client.models = generateModelsProperty(client, params);
25
- return client;
26
- }
@@ -1,4 +0,0 @@
1
- import { ModelTypes } from '@aws-amplify/amplify-api-next-types-alpha';
2
- import { ClientGenerationParams } from './types';
3
- import { V6Client } from '../types';
4
- export declare function generateModelsProperty<T extends Record<any, any> = never>(client: V6Client, params: ClientGenerationParams): ModelTypes<T>;