@aws-amplify/api-graphql 4.0.1-api-v6-models.01b2bb8.0 → 4.0.1-api-v6-models.9351bcf.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.
@@ -6,6 +6,7 @@ import { Observable } from 'rxjs';
6
6
  import { GraphQLAuthMode, DocumentType } from '@aws-amplify/core/internals/utils';
7
7
  export { CONTROL_MSG, ConnectionState } from './PubSub';
8
8
  export { SelectionSet } from '@aws-amplify/amplify-api-next-types-alpha';
9
+ export { CommonPublicClientOptions } from '../internals/types';
9
10
  /**
10
11
  * Loose/Unknown options for raw GraphQLAPICategory `graphql()`.
11
12
  */
@@ -281,8 +282,12 @@ type ExcludeNeverFields<O> = {
281
282
  [K in FilteredKeys<O>]: O[K];
282
283
  };
283
284
  export declare const __amplify: unique symbol;
285
+ export declare const __authMode: unique symbol;
286
+ export declare const __authToken: unique symbol;
284
287
  export type V6Client<T extends Record<any, any> = never> = ExcludeNeverFields<{
285
288
  [__amplify]: AmplifyClassV6;
289
+ [__authMode]?: GraphQLAuthMode;
290
+ [__authToken]?: string;
286
291
  graphql: <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: {
287
292
  [key: string]: string;
288
293
  } | undefined) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
@@ -8,3 +8,5 @@ export var GraphQLAuthError;
8
8
  GraphQLAuthError["NO_AUTH_TOKEN"] = "No auth token specified";
9
9
  })(GraphQLAuthError || (GraphQLAuthError = {}));
10
10
  export var __amplify = Symbol('amplify');
11
+ export var __authMode = Symbol('authMode');
12
+ export var __authToken = Symbol('authToken');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/api-graphql",
3
- "version": "4.0.1-api-v6-models.01b2bb8.0+01b2bb8",
3
+ "version": "4.0.1-api-v6-models.9351bcf.0+9351bcf",
4
4
  "description": "Api-graphql category of aws-amplify",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib-esm/index.js",
@@ -51,10 +51,10 @@
51
51
  "internals"
52
52
  ],
53
53
  "dependencies": {
54
- "@aws-amplify/amplify-api-next-types-alpha": "^0.2.0",
55
- "@aws-amplify/api-rest": "4.0.1-api-v6-models.01b2bb8.0+01b2bb8",
56
- "@aws-amplify/auth": "6.0.1-api-v6-models.01b2bb8.0+01b2bb8",
57
- "@aws-amplify/core": "6.0.1-api-v6-models.01b2bb8.0+01b2bb8",
54
+ "@aws-amplify/amplify-api-next-types-alpha": "^0.2.4",
55
+ "@aws-amplify/api-rest": "4.0.1-api-v6-models.9351bcf.0+9351bcf",
56
+ "@aws-amplify/auth": "6.0.1-api-v6-models.9351bcf.0+9351bcf",
57
+ "@aws-amplify/core": "6.0.1-api-v6-models.9351bcf.0+9351bcf",
58
58
  "@aws-sdk/types": "3.387.0",
59
59
  "graphql": "15.8.0",
60
60
  "rxjs": "^7.8.1",
@@ -126,5 +126,5 @@
126
126
  "<rootDir>/__tests__"
127
127
  ]
128
128
  },
129
- "gitHead": "01b2bb8503ffd202c858da1ff6afeb7c008a4563"
129
+ "gitHead": "9351bcfbeb9ec15f67fc6404ca192689a398d260"
130
130
  }
@@ -1,9 +1,15 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { resolveOwnerFields } from '../utils/resolveOwnerFields';
4
+ import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
4
5
 
5
6
  type ListArgs = { selectionSet?: string[]; filter?: {} };
6
7
 
8
+ type LazyLoadOptions = {
9
+ authMode?: GraphQLAuthMode;
10
+ authToken?: string | undefined;
11
+ };
12
+
7
13
  const connectionType = {
8
14
  HAS_ONE: 'HAS_ONE',
9
15
  HAS_MANY: 'HAS_MANY',
@@ -41,7 +47,9 @@ export function initializeModel(
41
47
  client: any,
42
48
  modelName: string,
43
49
  result: any[],
44
- modelIntrospection: any
50
+ modelIntrospection: any,
51
+ authMode: GraphQLAuthMode | undefined,
52
+ authToken: string | undefined
45
53
  ): any[] {
46
54
  const introModel = modelIntrospection.models[modelName];
47
55
  const introModelFields = introModel.fields;
@@ -83,12 +91,18 @@ export function initializeModel(
83
91
  {}
84
92
  );
85
93
 
86
- initializedRelationalFields[field] = () => {
94
+ initializedRelationalFields[field] = (options?: LazyLoadOptions) => {
87
95
  if (record[targetNames[0]]) {
88
- return client.models[relatedModelName].get({
89
- [relatedModelPKFieldName]: record[targetNames[0]],
90
- ...sortKeyValues,
91
- });
96
+ return client.models[relatedModelName].get(
97
+ {
98
+ [relatedModelPKFieldName]: record[targetNames[0]],
99
+ ...sortKeyValues,
100
+ },
101
+ {
102
+ authMode: options?.authMode || authMode,
103
+ authToken: options?.authToken || authToken,
104
+ }
105
+ );
92
106
  }
93
107
  return undefined;
94
108
  };
@@ -111,10 +125,14 @@ export function initializeModel(
111
125
  return { [field]: { eq: record[parentSK[idx - 1]] } };
112
126
  });
113
127
 
114
- initializedRelationalFields[field] = () => {
128
+ initializedRelationalFields[field] = (
129
+ options?: LazyLoadOptions
130
+ ) => {
115
131
  if (record[parentPk]) {
116
132
  return client.models[relatedModelName].list({
117
133
  filter: { and: hasManyFilter },
134
+ authMode: options?.authMode || authMode,
135
+ authToken: options?.authToken || authToken,
118
136
  });
119
137
  }
120
138
  return [];
@@ -130,10 +148,12 @@ export function initializeModel(
130
148
  return { [field]: { eq: record[parentSK[idx - 1]] } };
131
149
  });
132
150
 
133
- initializedRelationalFields[field] = () => {
151
+ initializedRelationalFields[field] = (options?: LazyLoadOptions) => {
134
152
  if (record[parentPk]) {
135
153
  return client.models[relatedModelName].list({
136
154
  filter: { and: hasManyFilter },
155
+ authMode: options?.authMode || authMode,
156
+ authToken: options?.authToken || authToken,
137
157
  });
138
158
  }
139
159
  return [];
@@ -375,7 +375,7 @@ export class InternalGraphQLAPIClass {
375
375
  variables,
376
376
  appSyncGraphqlEndpoint: config?.endpoint,
377
377
  region: config?.region,
378
- authenticationType: config?.defaultAuthMode,
378
+ authenticationType: authMode || config?.defaultAuthMode,
379
379
  apiKey: config?.apiKey,
380
380
  additionalHeaders,
381
381
  },
@@ -3,7 +3,7 @@
3
3
  import { AmplifyClassV6 } from '@aws-amplify/core';
4
4
  import { graphql, cancel, isCancelError } from './v6';
5
5
  import { generateModelsProperty } from './generateModelsProperty';
6
- import { V6Client, __amplify } from '../types';
6
+ import { V6Client, __amplify, __authMode, __authToken } from '../types';
7
7
  import { ClientGenerationParams } from './types';
8
8
 
9
9
  /**
@@ -20,6 +20,8 @@ export function generateClient<T extends Record<any, any> = never>(
20
20
  ): V6Client<T> {
21
21
  const client = {
22
22
  [__amplify]: params.amplify,
23
+ [__authMode]: params.authMode,
24
+ [__authToken]: params.authToken,
23
25
  graphql,
24
26
  cancel,
25
27
  isCancelError,
@@ -1,6 +1,7 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { ModelTypes } from '@aws-amplify/amplify-api-next-types-alpha';
4
+ import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
4
5
  import {
5
6
  initializeModel,
6
7
  generateGraphQLDocument,
@@ -10,10 +11,39 @@ import {
10
11
  flattenItems,
11
12
  } from './APIClient';
12
13
  import { ClientGenerationParams } from './types';
13
- import { V6Client, GraphqlSubscriptionResult } from '../types';
14
+ import {
15
+ V6Client,
16
+ GraphqlSubscriptionResult,
17
+ __authMode,
18
+ __authToken,
19
+ } from '../types';
14
20
  import { findIndexByFields, resolvePKFields } from '../utils';
15
21
  import { Observable, map } from 'rxjs';
16
22
 
23
+ type AuthModeParams = {
24
+ authMode?: GraphQLAuthMode;
25
+ authToken?: string;
26
+ };
27
+
28
+ /**
29
+ * Produces a parameter object that can contains auth mode/token overrides
30
+ * only if present in either `options` (first) or configured on the `client`
31
+ * as a fallback.
32
+ *
33
+ * @param client Configured client from `generateClient`
34
+ * @param options Args/Options obect from call site.
35
+ * @returns
36
+ */
37
+ function authModeParams(
38
+ client: V6Client,
39
+ options: AuthModeParams = {}
40
+ ): AuthModeParams {
41
+ return {
42
+ authMode: options.authMode || client[__authMode],
43
+ authToken: options.authToken || client[__authToken],
44
+ };
45
+ }
46
+
17
47
  export function generateModelsProperty<T extends Record<any, any> = never>(
18
48
  client: V6Client,
19
49
  params: ClientGenerationParams
@@ -53,7 +83,10 @@ export function generateModelsProperty<T extends Record<any, any> = never>(
53
83
  );
54
84
 
55
85
  try {
86
+ const auth = authModeParams(client, args);
87
+
56
88
  const { data, extensions } = (await client.graphql({
89
+ ...auth,
57
90
  query,
58
91
  variables,
59
92
  })) as any;
@@ -77,7 +110,9 @@ export function generateModelsProperty<T extends Record<any, any> = never>(
77
110
  client,
78
111
  name,
79
112
  flattenedResult,
80
- modelIntrospection
113
+ modelIntrospection,
114
+ auth.authMode,
115
+ auth.authToken
81
116
  );
82
117
 
83
118
  return {
@@ -105,7 +140,7 @@ export function generateModelsProperty<T extends Record<any, any> = never>(
105
140
  }
106
141
  };
107
142
  } else if (['ONCREATE', 'ONUPDATE', 'ONDELETE'].includes(operation)) {
108
- models[name][operationPrefix] = (arg?: any, options?: any) => {
143
+ models[name][operationPrefix] = (args?: any) => {
109
144
  const query = generateGraphQLDocument(
110
145
  modelIntrospection.models,
111
146
  name,
@@ -114,11 +149,14 @@ export function generateModelsProperty<T extends Record<any, any> = never>(
114
149
  const variables = buildGraphQLVariables(
115
150
  model,
116
151
  operation,
117
- arg,
152
+ args,
118
153
  modelIntrospection
119
154
  );
120
155
 
156
+ const auth = authModeParams(client, args);
157
+
121
158
  const observable = client.graphql({
159
+ ...auth,
122
160
  query,
123
161
  variables,
124
162
  }) as GraphqlSubscriptionResult<object>;
@@ -126,12 +164,20 @@ export function generateModelsProperty<T extends Record<any, any> = never>(
126
164
  return observable.pipe(
127
165
  map(value => {
128
166
  const [key] = Object.keys(value.data);
129
- return value.data[key];
167
+ const [initialized] = initializeModel(
168
+ client,
169
+ name,
170
+ [value.data[key]],
171
+ modelIntrospection,
172
+ auth.authMode,
173
+ auth.authToken
174
+ );
175
+ return initialized;
130
176
  })
131
177
  );
132
178
  };
133
179
  } else if (operation === 'OBSERVE_QUERY') {
134
- models[name][operationPrefix] = (arg?: any, options?: any) =>
180
+ models[name][operationPrefix] = (arg?: any) =>
135
181
  new Observable(subscriber => {
136
182
  // what we'll be sending to our subscribers
137
183
  const items: object[] = [];
@@ -151,36 +197,30 @@ export function generateModelsProperty<T extends Record<any, any> = never>(
151
197
  };
152
198
 
153
199
  // start subscriptions
154
- const onCreateSub = models[name]
155
- .onCreate(arg, options)
156
- .subscribe({
157
- next(item) {
158
- receiveMessages({ item, type: 'create' });
159
- },
160
- error(error) {
161
- subscriber.error({ type: 'onCreate', error });
162
- },
163
- });
164
- const onUpdateSub = models[name]
165
- .onUpdate(arg, options)
166
- .subscribe({
167
- next(item) {
168
- receiveMessages({ item, type: 'update' });
169
- },
170
- error(error) {
171
- subscriber.error({ type: 'onUpdate', error });
172
- },
173
- });
174
- const onDeleteSub = models[name]
175
- .onDelete(arg, options)
176
- .subscribe({
177
- next(item) {
178
- receiveMessages({ item, type: 'delete' });
179
- },
180
- error(error) {
181
- subscriber.error({ type: 'onDelete', error });
182
- },
183
- });
200
+ const onCreateSub = models[name].onCreate(arg).subscribe({
201
+ next(item) {
202
+ receiveMessages({ item, type: 'create' });
203
+ },
204
+ error(error) {
205
+ subscriber.error({ type: 'onCreate', error });
206
+ },
207
+ });
208
+ const onUpdateSub = models[name].onUpdate(arg).subscribe({
209
+ next(item) {
210
+ receiveMessages({ item, type: 'update' });
211
+ },
212
+ error(error) {
213
+ subscriber.error({ type: 'onUpdate', error });
214
+ },
215
+ });
216
+ const onDeleteSub = models[name].onDelete(arg).subscribe({
217
+ next(item) {
218
+ receiveMessages({ item, type: 'delete' });
219
+ },
220
+ error(error) {
221
+ subscriber.error({ type: 'onDelete', error });
222
+ },
223
+ });
184
224
 
185
225
  // consumes a list of messages and sends a snapshot
186
226
  function ingestMessages(messages: typeof messageQueue) {
@@ -226,7 +266,7 @@ export function generateModelsProperty<T extends Record<any, any> = never>(
226
266
  data: page,
227
267
  errors,
228
268
  nextToken: _nextToken,
229
- } = await models[name].list({ ...arg, nextToken }, options);
269
+ } = await models[name].list({ ...arg, nextToken });
230
270
  nextToken = _nextToken;
231
271
 
232
272
  items.push(...page);
@@ -288,7 +328,9 @@ export function generateModelsProperty<T extends Record<any, any> = never>(
288
328
  );
289
329
 
290
330
  try {
331
+ const auth = authModeParams(client, options);
291
332
  const { data, extensions } = (await client.graphql({
333
+ ...auth,
292
334
  query,
293
335
  variables,
294
336
  })) as any;
@@ -306,7 +348,9 @@ export function generateModelsProperty<T extends Record<any, any> = never>(
306
348
  client,
307
349
  name,
308
350
  [flattenedResult],
309
- modelIntrospection
351
+ modelIntrospection,
352
+ auth.authMode,
353
+ auth.authToken
310
354
  );
311
355
 
312
356
  return { data: initialized, extensions };
@@ -1,6 +1,7 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { AmplifyClassV6 } from '@aws-amplify/core';
4
+ import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
4
5
 
5
6
  /**
6
7
  * @private
@@ -9,4 +10,12 @@ import { AmplifyClassV6 } from '@aws-amplify/core';
9
10
  */
10
11
  export type ClientGenerationParams = {
11
12
  amplify: AmplifyClassV6;
13
+ } & CommonPublicClientOptions;
14
+
15
+ /**
16
+ * Common options that can be used on public `generateClient()` interfaces.
17
+ */
18
+ export type CommonPublicClientOptions = {
19
+ authMode?: GraphQLAuthMode;
20
+ authToken?: string;
12
21
  };
@@ -14,6 +14,8 @@ export { CONTROL_MSG, ConnectionState } from './PubSub';
14
14
 
15
15
  export { SelectionSet } from '@aws-amplify/amplify-api-next-types-alpha';
16
16
 
17
+ export { CommonPublicClientOptions } from '../internals/types';
18
+
17
19
  /**
18
20
  * Loose/Unknown options for raw GraphQLAPICategory `graphql()`.
19
21
  */
@@ -357,9 +359,13 @@ type ExcludeNeverFields<O> = {
357
359
  };
358
360
 
359
361
  export const __amplify = Symbol('amplify');
362
+ export const __authMode = Symbol('authMode');
363
+ export const __authToken = Symbol('authToken');
360
364
 
361
365
  export type V6Client<T extends Record<any, any> = never> = ExcludeNeverFields<{
362
366
  [__amplify]: AmplifyClassV6;
367
+ [__authMode]?: GraphQLAuthMode;
368
+ [__authToken]?: string;
363
369
  graphql: <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(
364
370
  options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>,
365
371
  additionalHeaders?: