@aws-amplify/api-graphql 4.0.1-api-v6-models.01b2bb8.0 → 4.0.1-api-v6-models.c1977f8.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.
@@ -1,3 +1,4 @@
1
+ import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
1
2
  type ListArgs = {
2
3
  selectionSet?: string[];
3
4
  filter?: {};
@@ -8,7 +9,7 @@ type ListArgs = {
8
9
  * @returns response object with `items` properties flattened
9
10
  */
10
11
  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 function initializeModel(client: any, modelName: string, result: any[], modelIntrospection: any, authMode: GraphQLAuthMode, authToken: string | undefined): any[];
12
13
  export declare const graphQLOperationsInfo: {
13
14
  CREATE: {
14
15
  operationPrefix: "create";
@@ -35,7 +35,7 @@ var flattenItems = function (obj) {
35
35
  };
36
36
  exports.flattenItems = flattenItems;
37
37
  // TODO: this should accept single result to support CRUD methods; create helper for array/list
38
- function initializeModel(client, modelName, result, modelIntrospection) {
38
+ function initializeModel(client, modelName, result, modelIntrospection, authMode, authToken) {
39
39
  var introModel = modelIntrospection.models[modelName];
40
40
  var introModelFields = introModel.fields;
41
41
  var modelFields = Object.entries(introModelFields)
@@ -68,10 +68,13 @@ function initializeModel(client, modelName, result, modelIntrospection) {
68
68
  return (acc[curVal] = record[curVal]);
69
69
  }
70
70
  }, {});
71
- initializedRelationalFields[field] = function () {
71
+ initializedRelationalFields[field] = function (options) {
72
72
  var _a;
73
73
  if (record[targetNames[0]]) {
74
- return client.models[relatedModelName].get(tslib_1.__assign((_a = {}, _a[relatedModelPKFieldName] = record[targetNames[0]], _a), sortKeyValues_1));
74
+ return client.models[relatedModelName].get(tslib_1.__assign((_a = {}, _a[relatedModelPKFieldName] = record[targetNames[0]], _a), sortKeyValues_1), {
75
+ authMode: (options === null || options === void 0 ? void 0 : options.authMode) || authMode,
76
+ authToken: (options === null || options === void 0 ? void 0 : options.authToken) || authToken,
77
+ });
75
78
  }
76
79
  return undefined;
77
80
  };
@@ -89,10 +92,12 @@ function initializeModel(client, modelName, result, modelIntrospection) {
89
92
  }
90
93
  return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
91
94
  });
92
- initializedRelationalFields[field] = function () {
95
+ initializedRelationalFields[field] = function (options) {
93
96
  if (record[parentPk_1]) {
94
97
  return client.models[relatedModelName].list({
95
98
  filter: { and: hasManyFilter_1 },
99
+ authMode: (options === null || options === void 0 ? void 0 : options.authMode) || authMode,
100
+ authToken: (options === null || options === void 0 ? void 0 : options.authToken) || authToken,
96
101
  });
97
102
  }
98
103
  return [];
@@ -106,10 +111,12 @@ function initializeModel(client, modelName, result, modelIntrospection) {
106
111
  }
107
112
  return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
108
113
  });
109
- initializedRelationalFields[field] = function () {
114
+ initializedRelationalFields[field] = function (options) {
110
115
  if (record[parentPk_1]) {
111
116
  return client.models[relatedModelName].list({
112
117
  filter: { and: hasManyFilter_2 },
118
+ authMode: (options === null || options === void 0 ? void 0 : options.authMode) || authMode,
119
+ authToken: (options === null || options === void 0 ? void 0 : options.authToken) || authToken,
113
120
  });
114
121
  }
115
122
  return [];
@@ -307,7 +307,7 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
307
307
  variables: variables,
308
308
  appSyncGraphqlEndpoint: config === null || config === void 0 ? void 0 : config.endpoint,
309
309
  region: config === null || config === void 0 ? void 0 : config.region,
310
- authenticationType: config === null || config === void 0 ? void 0 : config.defaultAuthMode,
310
+ authenticationType: authMode || (config === null || config === void 0 ? void 0 : config.defaultAuthMode),
311
311
  apiKey: config === null || config === void 0 ? void 0 : config.apiKey,
312
312
  additionalHeaders: additionalHeaders,
313
313
  }, customUserAgentDetails);
@@ -23,7 +23,7 @@ function generateModelsProperty(client, params) {
23
23
  var operation = key;
24
24
  if (operation === 'LIST') {
25
25
  models[name_1][operationPrefix] = function (args) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
26
- var query, variables, _a, data, extensions, _b, key_1, flattenedResult, initialized, error_1;
26
+ var query, variables, options, _a, data, extensions, _b, key_1, flattenedResult, initialized, error_1;
27
27
  return tslib_1.__generator(this, function (_c) {
28
28
  switch (_c.label) {
29
29
  case 0:
@@ -32,10 +32,12 @@ function generateModelsProperty(client, params) {
32
32
  _c.label = 1;
33
33
  case 1:
34
34
  _c.trys.push([1, 3, , 4]);
35
- return [4 /*yield*/, client.graphql({
36
- query: query,
37
- variables: variables,
38
- })];
35
+ options = {};
36
+ if (args === null || args === void 0 ? void 0 : args.authMode)
37
+ options.authMode = args === null || args === void 0 ? void 0 : args.authMode;
38
+ if (args === null || args === void 0 ? void 0 : args.authToken)
39
+ options.authToken = args === null || args === void 0 ? void 0 : args.authToken;
40
+ return [4 /*yield*/, client.graphql(tslib_1.__assign(tslib_1.__assign({}, options), { query: query, variables: variables }))];
39
41
  case 2:
40
42
  _a = (_c.sent()), data = _a.data, extensions = _a.extensions;
41
43
  // flatten response
@@ -52,7 +54,7 @@ function generateModelsProperty(client, params) {
52
54
  }];
53
55
  }
54
56
  else {
55
- initialized = (0, APIClient_1.initializeModel)(client, name_1, flattenedResult, modelIntrospection);
57
+ initialized = (0, APIClient_1.initializeModel)(client, name_1, flattenedResult, modelIntrospection, options.authMode, options.authToken);
56
58
  return [2 /*return*/, {
57
59
  data: initialized,
58
60
  nextToken: data[key_1].nextToken,
@@ -84,21 +86,24 @@ function generateModelsProperty(client, params) {
84
86
  }); };
85
87
  }
86
88
  else if (['ONCREATE', 'ONUPDATE', 'ONDELETE'].includes(operation)) {
87
- models[name_1][operationPrefix] = function (arg, options) {
89
+ models[name_1][operationPrefix] = function (args) {
88
90
  var query = (0, APIClient_1.generateGraphQLDocument)(modelIntrospection.models, name_1, operation);
89
- var variables = (0, APIClient_1.buildGraphQLVariables)(model, operation, arg, modelIntrospection);
90
- var observable = client.graphql({
91
- query: query,
92
- variables: variables,
93
- });
91
+ var variables = (0, APIClient_1.buildGraphQLVariables)(model, operation, args, modelIntrospection);
92
+ var options = {};
93
+ if (args === null || args === void 0 ? void 0 : args.authMode)
94
+ options.authMode = args === null || args === void 0 ? void 0 : args.authMode;
95
+ if (args === null || args === void 0 ? void 0 : args.authToken)
96
+ options.authToken = args === null || args === void 0 ? void 0 : args.authToken;
97
+ var observable = client.graphql(tslib_1.__assign(tslib_1.__assign({}, options), { query: query, variables: variables }));
94
98
  return observable.pipe((0, rxjs_1.map)(function (value) {
95
99
  var _a = tslib_1.__read(Object.keys(value.data), 1), key = _a[0];
96
- return value.data[key];
100
+ var _b = tslib_1.__read((0, APIClient_1.initializeModel)(client, name_1, [value.data[key]], modelIntrospection, options === null || options === void 0 ? void 0 : options.authMode, options === null || options === void 0 ? void 0 : options.authToken), 1), initialized = _b[0];
101
+ return initialized;
97
102
  }));
98
103
  };
99
104
  }
100
105
  else if (operation === 'OBSERVE_QUERY') {
101
- models[name_1][operationPrefix] = function (arg, options) {
106
+ models[name_1][operationPrefix] = function (arg) {
102
107
  return new rxjs_1.Observable(function (subscriber) {
103
108
  // what we'll be sending to our subscribers
104
109
  var items = [];
@@ -116,9 +121,7 @@ function generateModelsProperty(client, params) {
116
121
  return messageQueue.push.apply(messageQueue, tslib_1.__spreadArray([], tslib_1.__read(messages), false));
117
122
  };
118
123
  // start subscriptions
119
- var onCreateSub = models[name_1]
120
- .onCreate(arg, options)
121
- .subscribe({
124
+ var onCreateSub = models[name_1].onCreate(arg).subscribe({
122
125
  next: function (item) {
123
126
  receiveMessages({ item: item, type: 'create' });
124
127
  },
@@ -126,9 +129,7 @@ function generateModelsProperty(client, params) {
126
129
  subscriber.error({ type: 'onCreate', error: error });
127
130
  },
128
131
  });
129
- var onUpdateSub = models[name_1]
130
- .onUpdate(arg, options)
131
- .subscribe({
132
+ var onUpdateSub = models[name_1].onUpdate(arg).subscribe({
132
133
  next: function (item) {
133
134
  receiveMessages({ item: item, type: 'update' });
134
135
  },
@@ -136,9 +137,7 @@ function generateModelsProperty(client, params) {
136
137
  subscriber.error({ type: 'onUpdate', error: error });
137
138
  },
138
139
  });
139
- var onDeleteSub = models[name_1]
140
- .onDelete(arg, options)
141
- .subscribe({
140
+ var onDeleteSub = models[name_1].onDelete(arg).subscribe({
142
141
  next: function (item) {
143
142
  receiveMessages({ item: item, type: 'delete' });
144
143
  },
@@ -197,7 +196,7 @@ function generateModelsProperty(client, params) {
197
196
  case 1:
198
197
  if (!(!subscriber.closed && (firstPage || nextToken))) return [3 /*break*/, 3];
199
198
  firstPage = false;
200
- return [4 /*yield*/, models[name_1].list(tslib_1.__assign(tslib_1.__assign({}, arg), { nextToken: nextToken }), options)];
199
+ return [4 /*yield*/, models[name_1].list(tslib_1.__assign(tslib_1.__assign({}, arg), { nextToken: nextToken }))];
201
200
  case 2:
202
201
  _a = _c.sent(), page = _a.data, errors = _a.errors, _nextToken = _a.nextToken;
203
202
  nextToken = _nextToken;
@@ -265,10 +264,7 @@ function generateModelsProperty(client, params) {
265
264
  _d.label = 1;
266
265
  case 1:
267
266
  _d.trys.push([1, 3, , 4]);
268
- return [4 /*yield*/, client.graphql({
269
- query: query,
270
- variables: variables,
271
- })];
267
+ return [4 /*yield*/, client.graphql(tslib_1.__assign(tslib_1.__assign({}, options), { query: query, variables: variables }))];
272
268
  case 2:
273
269
  _a = (_d.sent()), data = _a.data, extensions = _a.extensions;
274
270
  // flatten response
@@ -279,7 +275,7 @@ function generateModelsProperty(client, params) {
279
275
  return [2 /*return*/, { data: flattenedResult, extensions: extensions }];
280
276
  }
281
277
  else {
282
- _c = tslib_1.__read((0, APIClient_1.initializeModel)(client, name_1, [flattenedResult], modelIntrospection), 1), initialized = _c[0];
278
+ _c = tslib_1.__read((0, APIClient_1.initializeModel)(client, name_1, [flattenedResult], modelIntrospection, options === null || options === void 0 ? void 0 : options.authMode, options === null || options === void 0 ? void 0 : options.authToken), 1), initialized = _c[0];
283
279
  return [2 /*return*/, { data: initialized, extensions: extensions }];
284
280
  }
285
281
  }