@aws-amplify/api-graphql 4.0.1-console-preview.be08038.0 → 4.0.1-server-generate-client.1e317a1.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 (80) hide show
  1. package/lib/GraphQLAPI.d.ts +2 -2
  2. package/lib/GraphQLAPI.js +4 -6
  3. package/lib/Providers/AWSAppSyncRealTimeProvider/index.d.ts +1 -1
  4. package/lib/Providers/AWSAppSyncRealTimeProvider/index.js +36 -36
  5. package/lib/Providers/constants.js +5 -4
  6. package/lib/index.js +5 -4
  7. package/lib/internals/APIClient.d.ts +101 -0
  8. package/lib/internals/APIClient.js +443 -0
  9. package/lib/internals/InternalGraphQLAPI.d.ts +2 -2
  10. package/lib/internals/InternalGraphQLAPI.js +35 -17
  11. package/lib/internals/generateClient.d.ts +12 -0
  12. package/lib/internals/generateClient.js +30 -0
  13. package/lib/internals/generateModelsProperty.d.ts +4 -0
  14. package/lib/internals/generateModelsProperty.js +169 -0
  15. package/lib/internals/index.d.ts +1 -0
  16. package/lib/internals/index.js +8 -5
  17. package/lib/internals/types.d.ts +10 -0
  18. package/lib/internals/types.js +2 -0
  19. package/lib/internals/v6.js +1 -0
  20. package/lib/tsconfig.tsbuildinfo +1 -6619
  21. package/lib/types/PubSub.d.ts +2 -2
  22. package/lib/types/PubSub.js +3 -2
  23. package/lib/types/index.d.ts +25 -22
  24. package/lib/types/index.js +4 -3
  25. package/lib/utils/ConnectionStateMonitor.d.ts +3 -3
  26. package/lib/utils/ConnectionStateMonitor.js +7 -6
  27. package/lib/utils/ReachabilityMonitor/index.d.ts +1 -1
  28. package/lib/utils/ReachabilityMonitor/index.js +3 -1
  29. package/lib/utils/ReachabilityMonitor/index.native.d.ts +1 -1
  30. package/lib/utils/ReachabilityMonitor/index.native.js +3 -1
  31. package/lib/utils/ReconnectionMonitor.js +2 -1
  32. package/lib/utils/errors/APIError.js +1 -0
  33. package/lib/utils/errors/assertValidationError.js +1 -0
  34. package/lib/utils/errors/index.js +5 -4
  35. package/lib/utils/errors/validation.js +2 -1
  36. package/lib/utils/index.js +3 -2
  37. package/lib/utils/resolveConfig.d.ts +1 -1
  38. package/lib/utils/resolveConfig.js +4 -2
  39. package/lib/utils/resolveLibraryOptions.d.ts +1 -1
  40. package/lib/utils/resolveLibraryOptions.js +3 -1
  41. package/lib/utils/resolveOwnerFields.d.ts +12 -0
  42. package/lib/utils/resolveOwnerFields.js +63 -0
  43. package/lib-esm/GraphQLAPI.d.ts +2 -2
  44. package/lib-esm/GraphQLAPI.js +1 -5
  45. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +1 -1
  46. package/lib-esm/Providers/AWSAppSyncRealTimeProvider/index.js +28 -29
  47. package/lib-esm/internals/APIClient.d.ts +101 -0
  48. package/lib-esm/internals/APIClient.js +432 -0
  49. package/lib-esm/internals/InternalGraphQLAPI.d.ts +2 -2
  50. package/lib-esm/internals/InternalGraphQLAPI.js +21 -5
  51. package/lib-esm/internals/generateClient.d.ts +12 -0
  52. package/lib-esm/internals/generateClient.js +26 -0
  53. package/lib-esm/internals/generateModelsProperty.d.ts +4 -0
  54. package/lib-esm/internals/generateModelsProperty.js +165 -0
  55. package/lib-esm/internals/index.d.ts +1 -0
  56. package/lib-esm/internals/index.js +1 -0
  57. package/lib-esm/internals/types.d.ts +10 -0
  58. package/lib-esm/internals/types.js +1 -0
  59. package/lib-esm/tsconfig.tsbuildinfo +1 -6619
  60. package/lib-esm/types/PubSub.d.ts +2 -2
  61. package/lib-esm/types/index.d.ts +25 -22
  62. package/lib-esm/utils/ConnectionStateMonitor.d.ts +3 -3
  63. package/lib-esm/utils/ConnectionStateMonitor.js +2 -2
  64. package/lib-esm/utils/ReachabilityMonitor/index.d.ts +1 -1
  65. package/lib-esm/utils/ReachabilityMonitor/index.native.d.ts +1 -1
  66. package/lib-esm/utils/resolveConfig.d.ts +1 -1
  67. package/lib-esm/utils/resolveLibraryOptions.d.ts +1 -1
  68. package/lib-esm/utils/resolveOwnerFields.d.ts +12 -0
  69. package/lib-esm/utils/resolveOwnerFields.js +59 -0
  70. package/package.json +12 -6
  71. package/src/GraphQLAPI.ts +2 -6
  72. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +2 -3
  73. package/src/internals/APIClient.ts +583 -0
  74. package/src/internals/InternalGraphQLAPI.ts +35 -9
  75. package/src/internals/generateClient.ts +30 -0
  76. package/src/internals/generateModelsProperty.ts +192 -0
  77. package/src/internals/index.ts +1 -0
  78. package/src/internals/types.ts +15 -0
  79. package/src/types/index.ts +7 -1
  80. package/src/utils/resolveOwnerFields.ts +65 -0
@@ -0,0 +1,165 @@
1
+ import { __awaiter, __generator, __read, __values } from "tslib";
2
+ import { initializeModel, generateGraphQLDocument, buildGraphQLVariables, graphQLOperationsInfo, flattenItems, } from './APIClient';
3
+ import { map } from 'rxjs';
4
+ import { Amplify } from '@aws-amplify/core';
5
+ export function generateModelsProperty(client, params) {
6
+ var e_1, _a;
7
+ var _this = this;
8
+ var _b, _c, _d;
9
+ var models = {};
10
+ // params.config gets populated in SSR only; otherwise we get it from the singleton
11
+ var config = (_b = params.config) !== null && _b !== void 0 ? _b : Amplify.getConfig();
12
+ var modelIntrospection = (_d = (_c = config.API) === null || _c === void 0 ? void 0 : _c.GraphQL) === null || _d === void 0 ? void 0 : _d.modelIntrospection;
13
+ if (!modelIntrospection) {
14
+ return {};
15
+ }
16
+ var _loop_1 = function (model) {
17
+ var name_1 = model.name;
18
+ models[name_1] = {};
19
+ Object.entries(graphQLOperationsInfo).forEach(function (_a) {
20
+ var _b = __read(_a, 2), key = _b[0], operationPrefix = _b[1].operationPrefix;
21
+ var operation = key;
22
+ if (operation === 'LIST') {
23
+ models[name_1][operationPrefix] = function (args) { return __awaiter(_this, void 0, void 0, function () {
24
+ var query, variables, _a, data, nextToken, extensions, _b, key_1, flattenedResult, initialized, error_1;
25
+ return __generator(this, function (_c) {
26
+ switch (_c.label) {
27
+ case 0:
28
+ query = generateGraphQLDocument(modelIntrospection.models, name_1, 'LIST', args);
29
+ variables = buildGraphQLVariables(model, 'LIST', args, modelIntrospection);
30
+ _c.label = 1;
31
+ case 1:
32
+ _c.trys.push([1, 3, , 4]);
33
+ return [4 /*yield*/, client.graphql({
34
+ query: query,
35
+ variables: variables,
36
+ })];
37
+ case 2:
38
+ _a = (_c.sent()), data = _a.data, nextToken = _a.nextToken, extensions = _a.extensions;
39
+ // flatten response
40
+ if (data !== undefined) {
41
+ _b = __read(Object.keys(data), 1), key_1 = _b[0];
42
+ if (data[key_1].items) {
43
+ flattenedResult = flattenItems(data)[key_1];
44
+ // don't init if custom selection set
45
+ if (args === null || args === void 0 ? void 0 : args.selectionSet) {
46
+ return [2 /*return*/, {
47
+ data: flattenedResult,
48
+ nextToken: nextToken,
49
+ extensions: extensions,
50
+ }];
51
+ }
52
+ else {
53
+ initialized = initializeModel(client, name_1, flattenedResult, modelIntrospection);
54
+ return [2 /*return*/, {
55
+ data: initialized,
56
+ nextToken: nextToken,
57
+ extensions: extensions,
58
+ }];
59
+ }
60
+ }
61
+ return [2 /*return*/, {
62
+ data: data[key_1],
63
+ nextToken: nextToken,
64
+ extensions: extensions,
65
+ }];
66
+ }
67
+ return [3 /*break*/, 4];
68
+ case 3:
69
+ error_1 = _c.sent();
70
+ if (error_1.errors) {
71
+ // graphql errors pass through
72
+ return [2 /*return*/, error_1];
73
+ }
74
+ else {
75
+ // non-graphql errors re re-thrown
76
+ throw error_1;
77
+ }
78
+ return [3 /*break*/, 4];
79
+ case 4: return [2 /*return*/];
80
+ }
81
+ });
82
+ }); };
83
+ }
84
+ else if (['ONCREATE', 'ONUPDATE', 'ONDELETE'].includes(operation)) {
85
+ models[name_1][operationPrefix] = function (arg, options) {
86
+ var query = generateGraphQLDocument(modelIntrospection.models, name_1, operation);
87
+ var variables = buildGraphQLVariables(model, operation, arg, modelIntrospection);
88
+ var observable = client.graphql({
89
+ query: query,
90
+ variables: variables,
91
+ });
92
+ return observable.pipe(map(function (value) {
93
+ var _a = __read(Object.keys(value.data), 1), key = _a[0];
94
+ return value.data[key];
95
+ }));
96
+ };
97
+ }
98
+ else {
99
+ models[name_1][operationPrefix] = function (arg, options) { return __awaiter(_this, void 0, void 0, function () {
100
+ var query, variables, _a, data, extensions, _b, key_2, flattenedResult, _c, initialized, error_2;
101
+ return __generator(this, function (_d) {
102
+ switch (_d.label) {
103
+ case 0:
104
+ query = generateGraphQLDocument(modelIntrospection.models, name_1, operation, options);
105
+ variables = buildGraphQLVariables(model, operation, arg, modelIntrospection);
106
+ _d.label = 1;
107
+ case 1:
108
+ _d.trys.push([1, 3, , 4]);
109
+ return [4 /*yield*/, client.graphql({
110
+ query: query,
111
+ variables: variables,
112
+ })];
113
+ case 2:
114
+ _a = (_d.sent()), data = _a.data, extensions = _a.extensions;
115
+ // flatten response
116
+ if (data !== undefined) {
117
+ _b = __read(Object.keys(data), 1), key_2 = _b[0];
118
+ flattenedResult = flattenItems(data)[key_2];
119
+ if (options === null || options === void 0 ? void 0 : options.selectionSet) {
120
+ return [2 /*return*/, { data: flattenedResult, extensions: extensions }];
121
+ }
122
+ else {
123
+ _c = __read(initializeModel(client, name_1, [flattenedResult], modelIntrospection), 1), initialized = _c[0];
124
+ return [2 /*return*/, { data: initialized, extensions: extensions }];
125
+ }
126
+ }
127
+ else {
128
+ return [2 /*return*/, { data: null, extensions: extensions }];
129
+ }
130
+ return [3 /*break*/, 4];
131
+ case 3:
132
+ error_2 = _d.sent();
133
+ if (error_2.errors) {
134
+ // graphql errors pass through
135
+ return [2 /*return*/, error_2];
136
+ }
137
+ else {
138
+ // non-graphql errors re re-thrown
139
+ throw error_2;
140
+ }
141
+ return [3 /*break*/, 4];
142
+ case 4: return [2 /*return*/];
143
+ }
144
+ });
145
+ }); };
146
+ }
147
+ });
148
+ };
149
+ try {
150
+ // TODO: refactor this to use separate methods for each CRUDL.
151
+ // Doesn't make sense to gen the methods dynamically given the different args and return values
152
+ for (var _e = __values(Object.values(modelIntrospection.models)), _f = _e.next(); !_f.done; _f = _e.next()) {
153
+ var model = _f.value;
154
+ _loop_1(model);
155
+ }
156
+ }
157
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
158
+ finally {
159
+ try {
160
+ if (_f && !_f.done && (_a = _e.return)) _a.call(_e);
161
+ }
162
+ finally { if (e_1) throw e_1.error; }
163
+ }
164
+ return models;
165
+ }
@@ -1,2 +1,3 @@
1
1
  export { InternalGraphQLAPI, InternalGraphQLAPIClass, } from './InternalGraphQLAPI';
2
2
  export { graphql, cancel, isCancelError } from './v6';
3
+ export { generateClient } from './generateClient';
@@ -2,3 +2,4 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  export { InternalGraphQLAPI, InternalGraphQLAPIClass, } from './InternalGraphQLAPI';
4
4
  export { graphql, cancel, isCancelError } from './v6';
5
+ export { generateClient } from './generateClient';
@@ -0,0 +1,10 @@
1
+ import { AmplifyClassV6 } from '@aws-amplify/core';
2
+ /**
3
+ * @private
4
+ *
5
+ * The knobs available for configuring `generateClient` internally.
6
+ */
7
+ export type ClientGenerationParams = {
8
+ amplify: AmplifyClassV6 | ((fn: (amplify: any) => Promise<any>) => Promise<AmplifyClassV6>);
9
+ config?: object;
10
+ };
@@ -0,0 +1 @@
1
+ export {};