@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";
@@ -31,7 +31,7 @@ export var flattenItems = function (obj) {
31
31
  return res;
32
32
  };
33
33
  // TODO: this should accept single result to support CRUD methods; create helper for array/list
34
- export function initializeModel(client, modelName, result, modelIntrospection) {
34
+ export function initializeModel(client, modelName, result, modelIntrospection, authMode, authToken) {
35
35
  var introModel = modelIntrospection.models[modelName];
36
36
  var introModelFields = introModel.fields;
37
37
  var modelFields = Object.entries(introModelFields)
@@ -64,10 +64,13 @@ export function initializeModel(client, modelName, result, modelIntrospection) {
64
64
  return (acc[curVal] = record[curVal]);
65
65
  }
66
66
  }, {});
67
- initializedRelationalFields[field] = function () {
67
+ initializedRelationalFields[field] = function (options) {
68
68
  var _a;
69
69
  if (record[targetNames[0]]) {
70
- return client.models[relatedModelName].get(__assign((_a = {}, _a[relatedModelPKFieldName] = record[targetNames[0]], _a), sortKeyValues_1));
70
+ return client.models[relatedModelName].get(__assign((_a = {}, _a[relatedModelPKFieldName] = record[targetNames[0]], _a), sortKeyValues_1), {
71
+ authMode: (options === null || options === void 0 ? void 0 : options.authMode) || authMode,
72
+ authToken: (options === null || options === void 0 ? void 0 : options.authToken) || authToken,
73
+ });
71
74
  }
72
75
  return undefined;
73
76
  };
@@ -85,10 +88,12 @@ export function initializeModel(client, modelName, result, modelIntrospection) {
85
88
  }
86
89
  return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
87
90
  });
88
- initializedRelationalFields[field] = function () {
91
+ initializedRelationalFields[field] = function (options) {
89
92
  if (record[parentPk_1]) {
90
93
  return client.models[relatedModelName].list({
91
94
  filter: { and: hasManyFilter_1 },
95
+ authMode: (options === null || options === void 0 ? void 0 : options.authMode) || authMode,
96
+ authToken: (options === null || options === void 0 ? void 0 : options.authToken) || authToken,
92
97
  });
93
98
  }
94
99
  return [];
@@ -102,10 +107,12 @@ export function initializeModel(client, modelName, result, modelIntrospection) {
102
107
  }
103
108
  return _b = {}, _b[field] = { eq: record[parentSK_1[idx - 1]] }, _b;
104
109
  });
105
- initializedRelationalFields[field] = function () {
110
+ initializedRelationalFields[field] = function (options) {
106
111
  if (record[parentPk_1]) {
107
112
  return client.models[relatedModelName].list({
108
113
  filter: { and: hasManyFilter_2 },
114
+ authMode: (options === null || options === void 0 ? void 0 : options.authMode) || authMode,
115
+ authToken: (options === null || options === void 0 ? void 0 : options.authToken) || authToken,
109
116
  });
110
117
  }
111
118
  return [];
@@ -303,7 +303,7 @@ var InternalGraphQLAPIClass = /** @class */ (function () {
303
303
  variables: variables,
304
304
  appSyncGraphqlEndpoint: config === null || config === void 0 ? void 0 : config.endpoint,
305
305
  region: config === null || config === void 0 ? void 0 : config.region,
306
- authenticationType: config === null || config === void 0 ? void 0 : config.defaultAuthMode,
306
+ authenticationType: authMode || (config === null || config === void 0 ? void 0 : config.defaultAuthMode),
307
307
  apiKey: config === null || config === void 0 ? void 0 : config.apiKey,
308
308
  additionalHeaders: additionalHeaders,
309
309
  }, customUserAgentDetails);
@@ -20,7 +20,7 @@ export function generateModelsProperty(client, params) {
20
20
  var operation = key;
21
21
  if (operation === 'LIST') {
22
22
  models[name_1][operationPrefix] = function (args) { return __awaiter(_this, void 0, void 0, function () {
23
- var query, variables, _a, data, extensions, _b, key_1, flattenedResult, initialized, error_1;
23
+ var query, variables, options, _a, data, extensions, _b, key_1, flattenedResult, initialized, error_1;
24
24
  return __generator(this, function (_c) {
25
25
  switch (_c.label) {
26
26
  case 0:
@@ -29,10 +29,12 @@ export function generateModelsProperty(client, params) {
29
29
  _c.label = 1;
30
30
  case 1:
31
31
  _c.trys.push([1, 3, , 4]);
32
- return [4 /*yield*/, client.graphql({
33
- query: query,
34
- variables: variables,
35
- })];
32
+ options = {};
33
+ if (args === null || args === void 0 ? void 0 : args.authMode)
34
+ options.authMode = args === null || args === void 0 ? void 0 : args.authMode;
35
+ if (args === null || args === void 0 ? void 0 : args.authToken)
36
+ options.authToken = args === null || args === void 0 ? void 0 : args.authToken;
37
+ return [4 /*yield*/, client.graphql(__assign(__assign({}, options), { query: query, variables: variables }))];
36
38
  case 2:
37
39
  _a = (_c.sent()), data = _a.data, extensions = _a.extensions;
38
40
  // flatten response
@@ -49,7 +51,7 @@ export function generateModelsProperty(client, params) {
49
51
  }];
50
52
  }
51
53
  else {
52
- initialized = initializeModel(client, name_1, flattenedResult, modelIntrospection);
54
+ initialized = initializeModel(client, name_1, flattenedResult, modelIntrospection, options.authMode, options.authToken);
53
55
  return [2 /*return*/, {
54
56
  data: initialized,
55
57
  nextToken: data[key_1].nextToken,
@@ -81,21 +83,24 @@ export function generateModelsProperty(client, params) {
81
83
  }); };
82
84
  }
83
85
  else if (['ONCREATE', 'ONUPDATE', 'ONDELETE'].includes(operation)) {
84
- models[name_1][operationPrefix] = function (arg, options) {
86
+ models[name_1][operationPrefix] = function (args) {
85
87
  var query = generateGraphQLDocument(modelIntrospection.models, name_1, operation);
86
- var variables = buildGraphQLVariables(model, operation, arg, modelIntrospection);
87
- var observable = client.graphql({
88
- query: query,
89
- variables: variables,
90
- });
88
+ var variables = buildGraphQLVariables(model, operation, args, modelIntrospection);
89
+ var options = {};
90
+ if (args === null || args === void 0 ? void 0 : args.authMode)
91
+ options.authMode = args === null || args === void 0 ? void 0 : args.authMode;
92
+ if (args === null || args === void 0 ? void 0 : args.authToken)
93
+ options.authToken = args === null || args === void 0 ? void 0 : args.authToken;
94
+ var observable = client.graphql(__assign(__assign({}, options), { query: query, variables: variables }));
91
95
  return observable.pipe(map(function (value) {
92
96
  var _a = __read(Object.keys(value.data), 1), key = _a[0];
93
- return value.data[key];
97
+ var _b = __read(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];
98
+ return initialized;
94
99
  }));
95
100
  };
96
101
  }
97
102
  else if (operation === 'OBSERVE_QUERY') {
98
- models[name_1][operationPrefix] = function (arg, options) {
103
+ models[name_1][operationPrefix] = function (arg) {
99
104
  return new Observable(function (subscriber) {
100
105
  // what we'll be sending to our subscribers
101
106
  var items = [];
@@ -113,9 +118,7 @@ export function generateModelsProperty(client, params) {
113
118
  return messageQueue.push.apply(messageQueue, __spreadArray([], __read(messages), false));
114
119
  };
115
120
  // start subscriptions
116
- var onCreateSub = models[name_1]
117
- .onCreate(arg, options)
118
- .subscribe({
121
+ var onCreateSub = models[name_1].onCreate(arg).subscribe({
119
122
  next: function (item) {
120
123
  receiveMessages({ item: item, type: 'create' });
121
124
  },
@@ -123,9 +126,7 @@ export function generateModelsProperty(client, params) {
123
126
  subscriber.error({ type: 'onCreate', error: error });
124
127
  },
125
128
  });
126
- var onUpdateSub = models[name_1]
127
- .onUpdate(arg, options)
128
- .subscribe({
129
+ var onUpdateSub = models[name_1].onUpdate(arg).subscribe({
129
130
  next: function (item) {
130
131
  receiveMessages({ item: item, type: 'update' });
131
132
  },
@@ -133,9 +134,7 @@ export function generateModelsProperty(client, params) {
133
134
  subscriber.error({ type: 'onUpdate', error: error });
134
135
  },
135
136
  });
136
- var onDeleteSub = models[name_1]
137
- .onDelete(arg, options)
138
- .subscribe({
137
+ var onDeleteSub = models[name_1].onDelete(arg).subscribe({
139
138
  next: function (item) {
140
139
  receiveMessages({ item: item, type: 'delete' });
141
140
  },
@@ -194,7 +193,7 @@ export function generateModelsProperty(client, params) {
194
193
  case 1:
195
194
  if (!(!subscriber.closed && (firstPage || nextToken))) return [3 /*break*/, 3];
196
195
  firstPage = false;
197
- return [4 /*yield*/, models[name_1].list(__assign(__assign({}, arg), { nextToken: nextToken }), options)];
196
+ return [4 /*yield*/, models[name_1].list(__assign(__assign({}, arg), { nextToken: nextToken }))];
198
197
  case 2:
199
198
  _a = _c.sent(), page = _a.data, errors = _a.errors, _nextToken = _a.nextToken;
200
199
  nextToken = _nextToken;
@@ -262,10 +261,7 @@ export function generateModelsProperty(client, params) {
262
261
  _d.label = 1;
263
262
  case 1:
264
263
  _d.trys.push([1, 3, , 4]);
265
- return [4 /*yield*/, client.graphql({
266
- query: query,
267
- variables: variables,
268
- })];
264
+ return [4 /*yield*/, client.graphql(__assign(__assign({}, options), { query: query, variables: variables }))];
269
265
  case 2:
270
266
  _a = (_d.sent()), data = _a.data, extensions = _a.extensions;
271
267
  // flatten response
@@ -276,7 +272,7 @@ export function generateModelsProperty(client, params) {
276
272
  return [2 /*return*/, { data: flattenedResult, extensions: extensions }];
277
273
  }
278
274
  else {
279
- _c = __read(initializeModel(client, name_1, [flattenedResult], modelIntrospection), 1), initialized = _c[0];
275
+ _c = __read(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];
280
276
  return [2 /*return*/, { data: initialized, extensions: extensions }];
281
277
  }
282
278
  }