@aws-amplify/api-graphql 4.0.2-unstable.ff4e8d7.0 → 4.0.2

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 (122) 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 +528 -0
  6. package/dist/cjs/internals/APIClient.js.map +1 -0
  7. package/dist/cjs/internals/InternalGraphQLAPI.js +14 -23
  8. package/dist/cjs/internals/InternalGraphQLAPI.js.map +1 -1
  9. package/dist/cjs/internals/generateClient.js +33 -0
  10. package/dist/cjs/internals/generateClient.js.map +1 -0
  11. package/dist/cjs/internals/generateModelsProperty.js +43 -0
  12. package/dist/cjs/internals/generateModelsProperty.js.map +1 -0
  13. package/dist/cjs/internals/index.js +3 -1
  14. package/dist/cjs/internals/index.js.map +1 -1
  15. package/dist/cjs/internals/operations/get.js +61 -0
  16. package/dist/cjs/internals/operations/get.js.map +1 -0
  17. package/dist/cjs/internals/operations/list.js +73 -0
  18. package/dist/cjs/internals/operations/list.js.map +1 -0
  19. package/dist/cjs/internals/operations/observeQuery.js +121 -0
  20. package/dist/cjs/internals/operations/observeQuery.js.map +1 -0
  21. package/dist/cjs/internals/operations/subscription.js +30 -0
  22. package/dist/cjs/internals/operations/subscription.js.map +1 -0
  23. package/dist/cjs/internals/server/generateClient.js +32 -0
  24. package/dist/cjs/internals/server/generateClient.js.map +1 -0
  25. package/dist/cjs/internals/server/generateModelsProperty.js +47 -0
  26. package/dist/cjs/internals/server/generateModelsProperty.js.map +1 -0
  27. package/dist/cjs/internals/server/index.js +9 -0
  28. package/dist/cjs/internals/server/index.js.map +1 -0
  29. package/dist/cjs/internals/types.js +4 -0
  30. package/dist/cjs/internals/types.js.map +1 -0
  31. package/dist/cjs/types/index.js +3 -1
  32. package/dist/cjs/types/index.js.map +1 -1
  33. package/dist/cjs/utils/findIndexByFields.js +25 -0
  34. package/dist/cjs/utils/findIndexByFields.js.map +1 -0
  35. package/dist/cjs/utils/index.js +5 -1
  36. package/dist/cjs/utils/index.js.map +1 -1
  37. package/dist/cjs/utils/resolveOwnerFields.js +42 -0
  38. package/dist/cjs/utils/resolveOwnerFields.js.map +1 -0
  39. package/dist/cjs/utils/resolvePKFields.js +17 -0
  40. package/dist/cjs/utils/resolvePKFields.js.map +1 -0
  41. package/dist/esm/GraphQLAPI.mjs +1 -1
  42. package/dist/esm/GraphQLAPI.mjs.map +1 -1
  43. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs +1 -1
  44. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs.map +1 -1
  45. package/dist/esm/index.mjs +1 -1
  46. package/dist/esm/internals/APIClient.d.ts +112 -0
  47. package/dist/esm/internals/APIClient.mjs +518 -0
  48. package/dist/esm/internals/APIClient.mjs.map +1 -0
  49. package/dist/esm/internals/InternalGraphQLAPI.d.ts +0 -11
  50. package/dist/esm/internals/InternalGraphQLAPI.mjs +14 -22
  51. package/dist/esm/internals/InternalGraphQLAPI.mjs.map +1 -1
  52. package/dist/esm/internals/generateClient.d.ts +12 -0
  53. package/dist/esm/internals/generateClient.mjs +31 -0
  54. package/dist/esm/internals/generateClient.mjs.map +1 -0
  55. package/dist/esm/internals/generateModelsProperty.d.ts +4 -0
  56. package/dist/esm/internals/generateModelsProperty.mjs +41 -0
  57. package/dist/esm/internals/generateModelsProperty.mjs.map +1 -0
  58. package/dist/esm/internals/index.d.ts +1 -0
  59. package/dist/esm/internals/index.mjs +1 -0
  60. package/dist/esm/internals/index.mjs.map +1 -1
  61. package/dist/esm/internals/operations/get.d.ts +5 -0
  62. package/dist/esm/internals/operations/get.mjs +59 -0
  63. package/dist/esm/internals/operations/get.mjs.map +1 -0
  64. package/dist/esm/internals/operations/list.d.ts +4 -0
  65. package/dist/esm/internals/operations/list.mjs +71 -0
  66. package/dist/esm/internals/operations/list.mjs.map +1 -0
  67. package/dist/esm/internals/operations/observeQuery.d.ts +3 -0
  68. package/dist/esm/internals/operations/observeQuery.mjs +122 -0
  69. package/dist/esm/internals/operations/observeQuery.mjs.map +1 -0
  70. package/dist/esm/internals/operations/subscription.d.ts +3 -0
  71. package/dist/esm/internals/operations/subscription.mjs +28 -0
  72. package/dist/esm/internals/operations/subscription.mjs.map +1 -0
  73. package/dist/esm/internals/server/generateClient.d.ts +11 -0
  74. package/dist/esm/internals/server/generateClient.mjs +34 -0
  75. package/dist/esm/internals/server/generateClient.mjs.map +1 -0
  76. package/dist/esm/internals/server/generateModelsProperty.d.ts +3 -0
  77. package/dist/esm/internals/server/generateModelsProperty.mjs +45 -0
  78. package/dist/esm/internals/server/generateModelsProperty.mjs.map +1 -0
  79. package/dist/esm/internals/server/index.d.ts +1 -0
  80. package/dist/esm/internals/server/index.mjs +2 -0
  81. package/dist/esm/internals/server/index.mjs.map +1 -0
  82. package/dist/esm/internals/types.d.ts +17 -0
  83. package/dist/esm/internals/types.mjs +2 -0
  84. package/dist/esm/internals/types.mjs.map +1 -0
  85. package/dist/esm/types/index.d.ts +32 -2
  86. package/dist/esm/types/index.mjs +3 -1
  87. package/dist/esm/types/index.mjs.map +1 -1
  88. package/dist/esm/utils/findIndexByFields.d.ts +9 -0
  89. package/dist/esm/utils/findIndexByFields.mjs +22 -0
  90. package/dist/esm/utils/findIndexByFields.mjs.map +1 -0
  91. package/dist/esm/utils/index.d.ts +2 -0
  92. package/dist/esm/utils/index.mjs +2 -0
  93. package/dist/esm/utils/index.mjs.map +1 -1
  94. package/dist/esm/utils/resolveOwnerFields.d.ts +12 -0
  95. package/dist/esm/utils/resolveOwnerFields.mjs +39 -0
  96. package/dist/esm/utils/resolveOwnerFields.mjs.map +1 -0
  97. package/dist/esm/utils/resolvePKFields.d.ts +13 -0
  98. package/dist/esm/utils/resolvePKFields.mjs +14 -0
  99. package/dist/esm/utils/resolvePKFields.mjs.map +1 -0
  100. package/internals/server/package.json +8 -0
  101. package/package.json +148 -146
  102. package/src/GraphQLAPI.ts +2 -2
  103. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +9 -4
  104. package/src/internals/APIClient.ts +780 -0
  105. package/src/internals/InternalGraphQLAPI.ts +21 -31
  106. package/src/internals/generateClient.ts +33 -0
  107. package/src/internals/generateModelsProperty.ts +73 -0
  108. package/src/internals/index.ts +1 -0
  109. package/src/internals/operations/get.ts +131 -0
  110. package/src/internals/operations/list.ts +132 -0
  111. package/src/internals/operations/observeQuery.ts +144 -0
  112. package/src/internals/operations/subscription.ts +65 -0
  113. package/src/internals/server/generateClient.ts +45 -0
  114. package/src/internals/server/generateModelsProperty.ts +80 -0
  115. package/src/internals/server/index.ts +4 -0
  116. package/src/internals/types.ts +21 -0
  117. package/src/types/index.ts +42 -2
  118. package/src/utils/ConnectionStateMonitor.ts +3 -1
  119. package/src/utils/findIndexByFields.ts +31 -0
  120. package/src/utils/index.ts +2 -0
  121. package/src/utils/resolveOwnerFields.ts +65 -0
  122. package/src/utils/resolvePKFields.ts +22 -0
@@ -0,0 +1,45 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { graphql, cancel, isCancelError } from '../';
5
+ import { generateModelsProperty } from './generateModelsProperty';
6
+ import {
7
+ __amplify,
8
+ __authMode,
9
+ __authToken,
10
+ V6ClientSSRRequest,
11
+ V6ClientSSRCookies,
12
+ ServerClientGenerationParams,
13
+ CommonPublicClientOptions,
14
+ } from '../../types';
15
+
16
+ /**
17
+ * @private
18
+ *
19
+ * Creates a client that can be used to make GraphQL requests, using a provided `AmplifyClassV6`
20
+ * compatible context object for config and auth fetching.
21
+ *
22
+ * @param params
23
+ * @returns
24
+ */
25
+ export function generateClient<
26
+ T extends Record<any, any> = never,
27
+ ClientType extends
28
+ | V6ClientSSRRequest<T>
29
+ | V6ClientSSRCookies<T> = V6ClientSSRCookies<T>
30
+ >(
31
+ params: ServerClientGenerationParams & CommonPublicClientOptions
32
+ ): ClientType {
33
+ const client = {
34
+ [__amplify]: params.amplify,
35
+ [__authMode]: params.authMode,
36
+ [__authToken]: params.authToken,
37
+ graphql,
38
+ cancel,
39
+ isCancelError,
40
+ } as any;
41
+
42
+ client.models = generateModelsProperty<T>(client, params);
43
+
44
+ return client as ClientType;
45
+ }
@@ -0,0 +1,80 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { ModelTypes } from '@aws-amplify/data-schema-types';
4
+ import { graphQLOperationsInfo, ModelOperation } from '../APIClient';
5
+ import { ServerClientGenerationParams } from '../../types/';
6
+ import { V6ClientSSRRequest, V6ClientSSRCookies } from '../../types';
7
+ import { ModelIntrospectionSchema } from '@aws-amplify/core/internals/utils';
8
+
9
+ import { listFactory } from '../operations/list';
10
+ import { getFactory } from '../operations/get';
11
+
12
+ export function generateModelsProperty<
13
+ T extends Record<any, any> = never,
14
+ ClientType extends
15
+ | V6ClientSSRRequest<Record<string, any>>
16
+ | V6ClientSSRCookies<Record<string, any>> = V6ClientSSRCookies<
17
+ Record<string, any>
18
+ >
19
+ >(client: ClientType, params: ServerClientGenerationParams): ClientType {
20
+ const models = {} as any;
21
+ const config = params.config;
22
+ const useContext = client === null;
23
+
24
+ if (!config) {
25
+ throw new Error('generateModelsProperty cannot retrieve Amplify config');
26
+ }
27
+
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
+
37
+ if (!modelIntrospection) {
38
+ return {} as ModelTypes<never>;
39
+ }
40
+
41
+ const SSR_UNSUPORTED_OPS = [
42
+ 'ONCREATE',
43
+ 'ONUPDATE',
44
+ 'ONDELETE',
45
+ 'OBSERVE_QUERY',
46
+ ];
47
+
48
+ for (const model of Object.values(modelIntrospection.models)) {
49
+ const { name } = model;
50
+ models[name] = {} as Record<string, any>;
51
+
52
+ Object.entries(graphQLOperationsInfo).forEach(
53
+ ([key, { operationPrefix }]) => {
54
+ const operation = key as ModelOperation;
55
+
56
+ // subscriptions are not supported in SSR
57
+ if (SSR_UNSUPORTED_OPS.includes(operation)) return;
58
+
59
+ if (operation === 'LIST') {
60
+ models[name][operationPrefix] = listFactory(
61
+ client,
62
+ modelIntrospection,
63
+ model,
64
+ useContext
65
+ );
66
+ } else {
67
+ models[name][operationPrefix] = getFactory(
68
+ client,
69
+ modelIntrospection,
70
+ model,
71
+ operation,
72
+ useContext
73
+ );
74
+ }
75
+ }
76
+ );
77
+ }
78
+
79
+ return models;
80
+ }
@@ -0,0 +1,4 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ export { generateClient } from './generateClient';
@@ -0,0 +1,21 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { AmplifyClassV6 } from '@aws-amplify/core';
4
+ import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
5
+
6
+ /**
7
+ * @private
8
+ *
9
+ * The knobs available for configuring `generateClient` internally.
10
+ */
11
+ export type ClientGenerationParams = {
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;
21
+ };
@@ -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, ResourcesConfig } from '@aws-amplify/core';
4
+ import { ModelTypes } from '@aws-amplify/data-schema-types';
4
5
  import { Source, DocumentNode, GraphQLError } from 'graphql';
5
6
  export { OperationTypeNode } from 'graphql';
6
7
  import { Observable } from 'rxjs';
@@ -12,6 +13,11 @@ import {
12
13
  import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
13
14
 
14
15
  export { CONTROL_MSG, ConnectionState } from './PubSub';
16
+
17
+ export { SelectionSet } from '@aws-amplify/data-schema-types';
18
+
19
+ export { CommonPublicClientOptions } from '../internals/types';
20
+
15
21
  /**
16
22
  * Loose/Unknown options for raw GraphQLAPICategory `graphql()`.
17
23
  */
@@ -349,25 +355,50 @@ export type GeneratedSubscription<InputType, OutputType> = string & {
349
355
  type FilteredKeys<T> = {
350
356
  [P in keyof T]: T[P] extends never ? never : P;
351
357
  }[keyof T];
358
+
352
359
  type ExcludeNeverFields<O> = {
353
360
  [K in FilteredKeys<O>]: O[K];
354
361
  };
355
362
 
356
363
  export const __amplify = Symbol('amplify');
364
+ export const __authMode = Symbol('authMode');
365
+ export const __authToken = Symbol('authToken');
366
+
367
+ export type ClientWithModels =
368
+ | V6Client<Record<string, any>>
369
+ | V6ClientSSRRequest<Record<string, any>>
370
+ | V6ClientSSRCookies<Record<string, any>>;
357
371
 
358
372
  export type V6Client<T extends Record<any, any> = never> = ExcludeNeverFields<{
359
373
  [__amplify]: AmplifyClassV6;
374
+ [__authMode]?: GraphQLAuthMode;
375
+ [__authToken]?: string;
360
376
  graphql: GraphQLMethod;
361
377
  cancel: (promise: Promise<any>, message?: string) => boolean;
362
378
  isCancelError: (error: any) => boolean;
379
+ models: ModelTypes<T>;
363
380
  }>;
364
381
 
365
- export type V6ClientSSR<T extends Record<any, any> = never> =
382
+ export type V6ClientSSRRequest<T extends Record<any, any> = never> =
366
383
  ExcludeNeverFields<{
367
384
  [__amplify]: AmplifyClassV6;
385
+ [__authMode]?: GraphQLAuthMode;
386
+ [__authToken]?: string;
368
387
  graphql: GraphQLMethodSSR;
369
388
  cancel: (promise: Promise<any>, message?: string) => boolean;
370
389
  isCancelError: (error: any) => boolean;
390
+ models: ModelTypes<T, 'REQUEST'>;
391
+ }>;
392
+
393
+ export type V6ClientSSRCookies<T extends Record<any, any> = never> =
394
+ ExcludeNeverFields<{
395
+ [__amplify]: AmplifyClassV6;
396
+ [__authMode]?: GraphQLAuthMode;
397
+ [__authToken]?: string;
398
+ graphql: GraphQLMethod;
399
+ cancel: (promise: Promise<any>, message?: string) => boolean;
400
+ isCancelError: (error: any) => boolean;
401
+ models: ModelTypes<T, 'COOKIES'>;
371
402
  }>;
372
403
 
373
404
  export type GraphQLMethod = <
@@ -404,7 +435,16 @@ export type ServerClientGenerationParams = {
404
435
  amplify:
405
436
  | null // null expected when used with `generateServerClient`
406
437
  // closure expected with `generateServerClientUsingCookies`
407
- | ((fn: (amplify: any) => Promise<any>) => Promise<AmplifyClassV6>);
438
+ | ((fn: (amplify: AmplifyClassV6) => Promise<any>) => Promise<any>);
408
439
  // global env-sourced config use for retrieving modelIntro
409
440
  config: ResourcesConfig;
410
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
 
@@ -0,0 +1,31 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ /**
4
+ * Iterates through a collection to find a matching item and returns the index.
5
+ *
6
+ * @param needle The item to search for
7
+ * @param haystack The collection to search
8
+ * @param keyFields The fields used to indicate a match
9
+ * @returns Index of `needle` in `haystack`, otherwise -1 if not found.
10
+ */
11
+ export function findIndexByFields<T>(
12
+ needle: T,
13
+ haystack: T[],
14
+ keyFields: Array<keyof T>
15
+ ): number {
16
+ const searchObject = Object.fromEntries(
17
+ keyFields.map(fieldName => [fieldName, needle[fieldName]])
18
+ );
19
+
20
+ for (let i = 0; i < haystack.length; i++) {
21
+ if (
22
+ Object.keys(searchObject).every(
23
+ k => searchObject[k] === (haystack[i] as any)[k]
24
+ )
25
+ ) {
26
+ return i;
27
+ }
28
+ }
29
+
30
+ return -1;
31
+ }
@@ -3,3 +3,5 @@
3
3
 
4
4
  export { resolveConfig } from './resolveConfig';
5
5
  export { resolveLibraryOptions } from './resolveLibraryOptions';
6
+ export { resolvePKFields } from './resolvePKFields';
7
+ export { findIndexByFields } from './findIndexByFields';
@@ -0,0 +1,65 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { ResourcesConfig } from '@aws-amplify/core';
4
+
5
+ type GraphQLConfig = Exclude<ResourcesConfig['API'], undefined>['GraphQL'];
6
+ type ModelIntrospectionSchema = Exclude<
7
+ Exclude<GraphQLConfig, undefined>['modelIntrospection'],
8
+ undefined
9
+ >;
10
+ type Model = ModelIntrospectionSchema['models'][string];
11
+
12
+ type AuthAttribute = {
13
+ type: 'auth';
14
+ properties: {
15
+ rules: AuthRule[];
16
+ };
17
+ };
18
+
19
+ /**
20
+ * Only the portions of an Auth rule we care about.
21
+ */
22
+ type AuthRule = {
23
+ allow: string;
24
+ ownerField?: string;
25
+ };
26
+
27
+ /**
28
+ * Given an introspection schema model, returns all owner fields.
29
+ *
30
+ * @param model Model from an introspection schema
31
+ * @returns List of owner field names
32
+ */
33
+ export function resolveOwnerFields(model: Model): string[] {
34
+ const ownerFields = new Set<string>();
35
+ for (const attr of model.attributes || []) {
36
+ if (isAuthAttribute(attr)) {
37
+ for (const rule of attr.properties.rules) {
38
+ if (rule.allow === 'owner') {
39
+ ownerFields.add(rule.ownerField || 'owner');
40
+ }
41
+ }
42
+ }
43
+ }
44
+ return Array.from(ownerFields);
45
+ }
46
+
47
+ /**
48
+ * Type guard that identifies an auth attribute with an attached rules list that
49
+ * specifies an `allow` attribute at a minimum.
50
+ *
51
+ * @param attribute Any object. Ideally a model introspection schema model attribute
52
+ * @returns True if given object is an auth attribute
53
+ */
54
+ function isAuthAttribute(attribute: any): attribute is AuthAttribute {
55
+ if (attribute?.type === 'auth') {
56
+ if (typeof attribute?.properties === 'object') {
57
+ if (Array.isArray(attribute?.properties?.rules)) {
58
+ return (attribute?.properties?.rules as Array<any>).every(
59
+ rule => !!rule.allow
60
+ );
61
+ }
62
+ }
63
+ }
64
+ return false;
65
+ }
@@ -0,0 +1,22 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { ResourcesConfig } from '@aws-amplify/core';
4
+
5
+ type GraphQLConfig = Exclude<ResourcesConfig['API'], undefined>['GraphQL'];
6
+ type ModelIntrospectionSchema = Exclude<
7
+ Exclude<GraphQLConfig, undefined>['modelIntrospection'],
8
+ undefined
9
+ >;
10
+ type SchemaModel = ModelIntrospectionSchema['models'][string];
11
+
12
+ /**
13
+ * Given a SchemaModel from a ModelIntrospectionSchema, returns the primary key
14
+ * as an array of field names.
15
+ *
16
+ * @param model The model object
17
+ * @returns Array of field names
18
+ */
19
+ export function resolvePKFields(model: SchemaModel) {
20
+ const { primaryKeyFieldName, sortKeyFieldNames } = model.primaryKeyInfo;
21
+ return [primaryKeyFieldName, ...sortKeyFieldNames];
22
+ }