@aws-amplify/api-graphql 4.0.2-unstable.51ed7b4.0 → 4.0.2-unstable.a5eb335.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 (62) hide show
  1. package/dist/cjs/GraphQLAPI.js +1 -1
  2. package/dist/cjs/GraphQLAPI.js.map +1 -1
  3. package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js +1 -1
  4. package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -1
  5. package/dist/cjs/internals/APIClient.js +70 -45
  6. package/dist/cjs/internals/APIClient.js.map +1 -1
  7. package/dist/cjs/internals/InternalGraphQLAPI.js +13 -22
  8. package/dist/cjs/internals/InternalGraphQLAPI.js.map +1 -1
  9. package/dist/cjs/internals/generateModelsProperty.js +1 -1
  10. package/dist/cjs/internals/generateModelsProperty.js.map +1 -1
  11. package/dist/cjs/internals/operations/get.js +5 -5
  12. package/dist/cjs/internals/operations/get.js.map +1 -1
  13. package/dist/cjs/internals/operations/list.js +5 -5
  14. package/dist/cjs/internals/operations/list.js.map +1 -1
  15. package/dist/cjs/internals/operations/observeQuery.js +2 -2
  16. package/dist/cjs/internals/operations/observeQuery.js.map +1 -1
  17. package/dist/cjs/internals/operations/subscription.js +2 -1
  18. package/dist/cjs/internals/operations/subscription.js.map +1 -1
  19. package/dist/cjs/internals/server/generateModelsProperty.js +4 -4
  20. package/dist/cjs/internals/server/generateModelsProperty.js.map +1 -1
  21. package/dist/esm/GraphQLAPI.mjs +1 -1
  22. package/dist/esm/GraphQLAPI.mjs.map +1 -1
  23. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs +1 -1
  24. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs.map +1 -1
  25. package/dist/esm/internals/APIClient.d.ts +10 -19
  26. package/dist/esm/internals/APIClient.mjs +70 -45
  27. package/dist/esm/internals/APIClient.mjs.map +1 -1
  28. package/dist/esm/internals/InternalGraphQLAPI.d.ts +0 -11
  29. package/dist/esm/internals/InternalGraphQLAPI.mjs +13 -21
  30. package/dist/esm/internals/InternalGraphQLAPI.mjs.map +1 -1
  31. package/dist/esm/internals/generateModelsProperty.d.ts +1 -1
  32. package/dist/esm/internals/generateModelsProperty.mjs +1 -1
  33. package/dist/esm/internals/generateModelsProperty.mjs.map +1 -1
  34. package/dist/esm/internals/operations/get.d.ts +4 -1
  35. package/dist/esm/internals/operations/get.mjs +5 -5
  36. package/dist/esm/internals/operations/get.mjs.map +1 -1
  37. package/dist/esm/internals/operations/list.d.ts +3 -1
  38. package/dist/esm/internals/operations/list.mjs +5 -5
  39. package/dist/esm/internals/operations/list.mjs.map +1 -1
  40. package/dist/esm/internals/operations/observeQuery.d.ts +2 -1
  41. package/dist/esm/internals/operations/subscription.d.ts +3 -1
  42. package/dist/esm/internals/operations/subscription.mjs +2 -1
  43. package/dist/esm/internals/operations/subscription.mjs.map +1 -1
  44. package/dist/esm/internals/server/generateModelsProperty.d.ts +1 -1
  45. package/dist/esm/internals/server/generateModelsProperty.mjs +4 -4
  46. package/dist/esm/internals/server/generateModelsProperty.mjs.map +1 -1
  47. package/dist/esm/types/index.d.ts +10 -0
  48. package/package.json +8 -12
  49. package/src/GraphQLAPI.ts +2 -2
  50. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +9 -4
  51. package/src/internals/APIClient.ts +160 -104
  52. package/src/internals/InternalGraphQLAPI.ts +20 -30
  53. package/src/internals/generateModelsProperty.ts +8 -5
  54. package/src/internals/operations/get.ts +44 -34
  55. package/src/internals/operations/list.ts +38 -17
  56. package/src/internals/operations/observeQuery.ts +14 -10
  57. package/src/internals/operations/subscription.ts +13 -7
  58. package/src/internals/server/generateClient.ts +1 -1
  59. package/src/internals/server/generateModelsProperty.ts +18 -8
  60. package/src/types/index.ts +14 -0
  61. package/src/utils/ConnectionStateMonitor.ts +3 -1
  62. package/src/utils/findIndexByFields.ts +3 -1
@@ -7,17 +7,32 @@ import {
7
7
  buildGraphQLVariables,
8
8
  flattenItems,
9
9
  authModeParams,
10
+ ModelOperation,
10
11
  } from '../APIClient';
12
+ import {
13
+ AuthModeParams,
14
+ ClientWithModels,
15
+ GraphQLOptionsV6,
16
+ GraphQLResult,
17
+ ListArgs,
18
+ QueryArgs,
19
+ V6Client,
20
+ V6ClientSSRRequest,
21
+ } from '../../types';
22
+ import {
23
+ ModelIntrospectionSchema,
24
+ SchemaModel,
25
+ } from '@aws-amplify/core/internals/utils';
11
26
 
12
27
  export function getFactory(
13
- client,
14
- modelIntrospection,
15
- model,
16
- operation,
17
- context = false
28
+ client: ClientWithModels,
29
+ modelIntrospection: ModelIntrospectionSchema,
30
+ model: SchemaModel,
31
+ operation: ModelOperation,
32
+ useContext = false
18
33
  ) {
19
34
  const getWithContext = async (
20
- contextSpec: AmplifyServer.ContextSpec,
35
+ contextSpec: AmplifyServer.ContextSpec & GraphQLOptionsV6<unknown, string>,
21
36
  arg?: any,
22
37
  options?: any
23
38
  ) => {
@@ -33,30 +48,22 @@ export function getFactory(
33
48
  };
34
49
 
35
50
  const get = async (arg?: any, options?: any) => {
36
- return _get(
37
- client,
38
- modelIntrospection,
39
- model,
40
- arg,
41
- options,
42
- operation,
43
- context
44
- );
51
+ return _get(client, modelIntrospection, model, arg, options, operation);
45
52
  };
46
53
 
47
- return context ? getWithContext : get;
54
+ return useContext ? getWithContext : get;
48
55
  }
49
56
 
50
57
  async function _get(
51
- client,
52
- modelIntrospection,
53
- model,
54
- arg,
55
- options,
56
- operation,
57
- context
58
+ client: ClientWithModels,
59
+ modelIntrospection: ModelIntrospectionSchema,
60
+ model: SchemaModel,
61
+ arg: QueryArgs,
62
+ options: AuthModeParams & ListArgs,
63
+ operation: ModelOperation,
64
+ context?: AmplifyServer.ContextSpec
58
65
  ) {
59
- const { name } = model as any;
66
+ const { name } = model;
60
67
 
61
68
  const query = generateGraphQLDocument(
62
69
  modelIntrospection.models,
@@ -74,19 +81,22 @@ async function _get(
74
81
  try {
75
82
  const auth = authModeParams(client, options);
76
83
  const { data, extensions } = context
77
- ? ((await client.graphql(context, {
78
- ...auth,
79
- query,
80
- variables,
81
- })) as any)
82
- : ((await client.graphql({
84
+ ? ((await (client as V6ClientSSRRequest<Record<string, any>>).graphql(
85
+ context,
86
+ {
87
+ ...auth,
88
+ query,
89
+ variables,
90
+ }
91
+ )) as GraphQLResult<any>)
92
+ : ((await (client as V6Client<Record<string, any>>).graphql({
83
93
  ...auth,
84
94
  query,
85
95
  variables,
86
- })) as any);
96
+ })) as GraphQLResult<any>);
87
97
 
88
98
  // flatten response
89
- if (data !== undefined) {
99
+ if (data) {
90
100
  const [key] = Object.keys(data);
91
101
  const flattenedResult = flattenItems(data)[key];
92
102
 
@@ -101,7 +111,7 @@ async function _get(
101
111
  modelIntrospection,
102
112
  auth.authMode,
103
113
  auth.authToken,
104
- context
114
+ !!context
105
115
  );
106
116
 
107
117
  return { data: initialized, extensions };
@@ -109,7 +119,7 @@ async function _get(
109
119
  } else {
110
120
  return { data: null, extensions };
111
121
  }
112
- } catch (error) {
122
+ } catch (error: any) {
113
123
  if (error.errors) {
114
124
  // graphql errors pass through
115
125
  return error as any;
@@ -8,29 +8,47 @@ import {
8
8
  flattenItems,
9
9
  authModeParams,
10
10
  } from '../APIClient';
11
+ import {
12
+ AuthModeParams,
13
+ ClientWithModels,
14
+ ListArgs,
15
+ V6Client,
16
+ V6ClientSSRRequest,
17
+ GraphQLResult,
18
+ } from '../../types';
19
+ import {
20
+ ModelIntrospectionSchema,
21
+ SchemaModel,
22
+ } from '@aws-amplify/core/internals/utils';
11
23
 
12
24
  export function listFactory(
13
- client,
14
- modelIntrospection,
15
- model,
25
+ client: ClientWithModels,
26
+ modelIntrospection: ModelIntrospectionSchema,
27
+ model: SchemaModel,
16
28
  context = false
17
29
  ) {
18
30
  const listWithContext = async (
19
31
  contextSpec: AmplifyServer.ContextSpec,
20
- args?: any
32
+ args?: ListArgs
21
33
  ) => {
22
34
  return _list(client, modelIntrospection, model, args, contextSpec);
23
35
  };
24
36
 
25
37
  const list = async (args?: any) => {
26
- return _list(client, modelIntrospection, model, args, context);
38
+ return _list(client, modelIntrospection, model, args);
27
39
  };
28
40
 
29
41
  return context ? listWithContext : list;
30
42
  }
31
43
 
32
- async function _list(client, modelIntrospection, model, args, context) {
33
- const { name } = model as any;
44
+ async function _list(
45
+ client: ClientWithModels,
46
+ modelIntrospection: ModelIntrospectionSchema,
47
+ model: SchemaModel,
48
+ args?: ListArgs & AuthModeParams,
49
+ contextSpec?: AmplifyServer.ContextSpec
50
+ ) {
51
+ const { name } = model;
34
52
 
35
53
  const query = generateGraphQLDocument(
36
54
  modelIntrospection.models,
@@ -48,17 +66,20 @@ async function _list(client, modelIntrospection, model, args, context) {
48
66
  try {
49
67
  const auth = authModeParams(client, args);
50
68
 
51
- const { data, extensions } = context
52
- ? ((await client.graphql(context, {
53
- ...auth,
54
- query,
55
- variables,
56
- })) as any)
57
- : ((await client.graphql({
69
+ const { data, extensions } = !!contextSpec
70
+ ? ((await (client as V6ClientSSRRequest<Record<string, any>>).graphql(
71
+ contextSpec,
72
+ {
73
+ ...auth,
74
+ query,
75
+ variables,
76
+ }
77
+ )) as GraphQLResult<any>)
78
+ : ((await (client as V6Client<Record<string, any>>).graphql({
58
79
  ...auth,
59
80
  query,
60
81
  variables,
61
- })) as any);
82
+ })) as GraphQLResult<any>);
62
83
 
63
84
  // flatten response
64
85
  if (data !== undefined) {
@@ -82,7 +103,7 @@ async function _list(client, modelIntrospection, model, args, context) {
82
103
  modelIntrospection,
83
104
  auth.authMode,
84
105
  auth.authToken,
85
- context
106
+ !!contextSpec
86
107
  );
87
108
 
88
109
  return {
@@ -99,7 +120,7 @@ async function _list(client, modelIntrospection, model, args, context) {
99
120
  extensions,
100
121
  };
101
122
  }
102
- } catch (error) {
123
+ } catch (error: any) {
103
124
  if (error.errors) {
104
125
  // graphql errors pass through
105
126
  return error as any;
@@ -1,11 +1,11 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
-
4
- import { Observable, map } from 'rxjs';
3
+ import { Observable } from 'rxjs';
5
4
  import { findIndexByFields, resolvePKFields } from '../../utils';
5
+ import { SchemaModel } from '@aws-amplify/core/internals/utils';
6
6
 
7
- export function observeQueryFactory(models, model) {
8
- const { name } = model as any;
7
+ export function observeQueryFactory(models: any, model: SchemaModel) {
8
+ const { name } = model;
9
9
 
10
10
  const observeQuery = (arg?: any) =>
11
11
  new Observable(subscriber => {
@@ -28,26 +28,26 @@ export function observeQueryFactory(models, model) {
28
28
 
29
29
  // start subscriptions
30
30
  const onCreateSub = models[name].onCreate(arg).subscribe({
31
- next(item) {
31
+ next(item: object) {
32
32
  receiveMessages({ item, type: 'create' });
33
33
  },
34
- error(error) {
34
+ error(error: any) {
35
35
  subscriber.error({ type: 'onCreate', error });
36
36
  },
37
37
  });
38
38
  const onUpdateSub = models[name].onUpdate(arg).subscribe({
39
- next(item) {
39
+ next(item: object) {
40
40
  receiveMessages({ item, type: 'update' });
41
41
  },
42
- error(error) {
42
+ error(error: any) {
43
43
  subscriber.error({ type: 'onUpdate', error });
44
44
  },
45
45
  });
46
46
  const onDeleteSub = models[name].onDelete(arg).subscribe({
47
- next(item) {
47
+ next(item: object) {
48
48
  receiveMessages({ item, type: 'delete' });
49
49
  },
50
- error(error) {
50
+ error(error: any) {
51
51
  subscriber.error({ type: 'onDelete', error });
52
52
  },
53
53
  });
@@ -89,6 +89,10 @@ export function observeQueryFactory(models, model) {
89
89
  data: page,
90
90
  errors,
91
91
  nextToken: _nextToken,
92
+ }: {
93
+ data: any;
94
+ errors: any;
95
+ nextToken: string | null;
92
96
  } = await models[name].list({ ...arg, nextToken });
93
97
  nextToken = _nextToken;
94
98
 
@@ -2,19 +2,24 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import { map } from 'rxjs';
5
- import { GraphqlSubscriptionResult } from '../../types';
5
+ import { V6Client, GraphqlSubscriptionResult } from '../../types';
6
6
  import {
7
7
  initializeModel,
8
8
  generateGraphQLDocument,
9
9
  buildGraphQLVariables,
10
10
  authModeParams,
11
+ ModelOperation,
11
12
  } from '../APIClient';
13
+ import {
14
+ ModelIntrospectionSchema,
15
+ SchemaModel,
16
+ } from '@aws-amplify/core/internals/utils';
12
17
 
13
18
  export function subscriptionFactory(
14
- client,
15
- modelIntrospection,
16
- model,
17
- operation
19
+ client: any,
20
+ modelIntrospection: ModelIntrospectionSchema,
21
+ model: SchemaModel,
22
+ operation: ModelOperation
18
23
  ) {
19
24
  const { name } = model as any;
20
25
 
@@ -42,10 +47,11 @@ export function subscriptionFactory(
42
47
  return observable.pipe(
43
48
  map(value => {
44
49
  const [key] = Object.keys(value.data);
50
+ const data = (value.data as any)[key];
45
51
  const [initialized] = initializeModel(
46
- client,
52
+ client as V6Client<Record<string, any>>,
47
53
  name,
48
- [value.data[key]],
54
+ [data],
49
55
  modelIntrospection,
50
56
  auth.authMode,
51
57
  auth.authToken
@@ -39,7 +39,7 @@ export function generateClient<
39
39
  isCancelError,
40
40
  } as any;
41
41
 
42
- client.models = generateModelsProperty<T, ClientType>(client, params);
42
+ client.models = generateModelsProperty<T>(client, params);
43
43
 
44
44
  return client as ClientType;
45
45
  }
@@ -1,9 +1,10 @@
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/data-schema-types';
4
4
  import { graphQLOperationsInfo, ModelOperation } from '../APIClient';
5
5
  import { ServerClientGenerationParams } from '../../types/';
6
6
  import { V6ClientSSRRequest, V6ClientSSRCookies } from '../../types';
7
+ import { ModelIntrospectionSchema } from '@aws-amplify/core/internals/utils';
7
8
 
8
9
  import { listFactory } from '../operations/list';
9
10
  import { getFactory } from '../operations/get';
@@ -11,21 +12,30 @@ import { getFactory } from '../operations/get';
11
12
  export function generateModelsProperty<
12
13
  T extends Record<any, any> = never,
13
14
  ClientType extends
14
- | V6ClientSSRRequest<T>
15
- | V6ClientSSRCookies<T> = V6ClientSSRCookies<T>
15
+ | V6ClientSSRRequest<Record<string, any>>
16
+ | V6ClientSSRCookies<Record<string, any>> = V6ClientSSRCookies<
17
+ Record<string, any>
18
+ >
16
19
  >(client: ClientType, params: ServerClientGenerationParams): ClientType {
17
20
  const models = {} as any;
18
21
  const config = params.config;
19
22
  const useContext = client === null;
20
23
 
21
24
  if (!config) {
22
- // TODO: improve
23
25
  throw new Error('generateModelsProperty cannot retrieve Amplify config');
24
26
  }
25
27
 
26
- const modelIntrospection = config.API?.GraphQL?.modelIntrospection;
28
+ if (!config.API?.GraphQL) {
29
+ throw new Error(
30
+ 'The API configuration is missing. This is likely due to Amplify.configure() not being called prior to generateClient().'
31
+ );
32
+ }
33
+
34
+ const modelIntrospection: ModelIntrospectionSchema | undefined =
35
+ config.API.GraphQL.modelIntrospection;
36
+
27
37
  if (!modelIntrospection) {
28
- return {} as any;
38
+ return {} as ModelTypes<never>;
29
39
  }
30
40
 
31
41
  const SSR_UNSUPORTED_OPS = [
@@ -36,8 +46,8 @@ export function generateModelsProperty<
36
46
  ];
37
47
 
38
48
  for (const model of Object.values(modelIntrospection.models)) {
39
- const { name } = model as any;
40
- models[name] = {} as any;
49
+ const { name } = model;
50
+ models[name] = {} as Record<string, any>;
41
51
 
42
52
  Object.entries(graphQLOperationsInfo).forEach(
43
53
  ([key, { operationPrefix }]) => {
@@ -364,6 +364,11 @@ export const __amplify = Symbol('amplify');
364
364
  export const __authMode = Symbol('authMode');
365
365
  export const __authToken = Symbol('authToken');
366
366
 
367
+ export type ClientWithModels =
368
+ | V6Client<Record<string, any>>
369
+ | V6ClientSSRRequest<Record<string, any>>
370
+ | V6ClientSSRCookies<Record<string, any>>;
371
+
367
372
  export type V6Client<T extends Record<any, any> = never> = ExcludeNeverFields<{
368
373
  [__amplify]: AmplifyClassV6;
369
374
  [__authMode]?: GraphQLAuthMode;
@@ -434,3 +439,12 @@ export type ServerClientGenerationParams = {
434
439
  // global env-sourced config use for retrieving modelIntro
435
440
  config: ResourcesConfig;
436
441
  };
442
+
443
+ export type QueryArgs = Record<string, unknown>;
444
+
445
+ export type ListArgs = { selectionSet?: string[]; filter?: {} };
446
+
447
+ export type AuthModeParams = {
448
+ authMode?: GraphQLAuthMode;
449
+ authToken?: string;
450
+ };
@@ -50,7 +50,9 @@ export class ConnectionStateMonitor {
50
50
  */
51
51
  private _linkedConnectionState: LinkedConnectionStates;
52
52
  private _linkedConnectionStateObservable: Observable<LinkedConnectionStates>;
53
- private _linkedConnectionStateObserver: Observer<LinkedConnectionStates>;
53
+ private _linkedConnectionStateObserver:
54
+ | Observer<LinkedConnectionStates>
55
+ | undefined;
54
56
  private _networkMonitoringSubscription?: SubscriptionLike;
55
57
  private _initialNetworkStateSubscription?: SubscriptionLike;
56
58
 
@@ -19,7 +19,9 @@ export function findIndexByFields<T>(
19
19
 
20
20
  for (let i = 0; i < haystack.length; i++) {
21
21
  if (
22
- Object.keys(searchObject).every(k => searchObject[k] === haystack[i][k])
22
+ Object.keys(searchObject).every(
23
+ k => searchObject[k] === (haystack[i] as any)[k]
24
+ )
23
25
  ) {
24
26
  return i;
25
27
  }