@aws-amplify/api-graphql 4.0.6-unstable.f2b7a8d.0 → 4.0.7-unstable.53dc717.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 (28) 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 +4 -3
  4. package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -1
  5. package/dist/cjs/internals/InternalGraphQLAPI.js +24 -5
  6. package/dist/cjs/internals/InternalGraphQLAPI.js.map +1 -1
  7. package/dist/cjs/utils/errors/repackageAuthError.js +39 -0
  8. package/dist/cjs/utils/errors/repackageAuthError.js.map +1 -0
  9. package/dist/esm/GraphQLAPI.d.ts +1 -1
  10. package/dist/esm/GraphQLAPI.mjs +1 -1
  11. package/dist/esm/GraphQLAPI.mjs.map +1 -1
  12. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +1 -1
  13. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs +4 -3
  14. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs.map +1 -1
  15. package/dist/esm/internals/InternalGraphQLAPI.d.ts +1 -1
  16. package/dist/esm/internals/InternalGraphQLAPI.mjs +24 -5
  17. package/dist/esm/internals/InternalGraphQLAPI.mjs.map +1 -1
  18. package/dist/esm/types/index.d.ts +1 -1
  19. package/dist/esm/utils/errors/repackageAuthError.d.ts +11 -0
  20. package/dist/esm/utils/errors/repackageAuthError.mjs +36 -0
  21. package/dist/esm/utils/errors/repackageAuthError.mjs.map +1 -0
  22. package/dist/esm/utils/resolveLibraryOptions.d.ts +3 -3
  23. package/package.json +4 -54
  24. package/src/GraphQLAPI.ts +1 -1
  25. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +5 -3
  26. package/src/internals/InternalGraphQLAPI.ts +38 -17
  27. package/src/types/index.ts +1 -1
  28. package/src/utils/errors/repackageAuthError.ts +46 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/api-graphql",
3
- "version": "4.0.6-unstable.f2b7a8d.0+f2b7a8d",
3
+ "version": "4.0.7-unstable.53dc717.0+53dc717",
4
4
  "description": "Api-graphql category of aws-amplify",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.mjs",
@@ -79,8 +79,8 @@
79
79
  "internals"
80
80
  ],
81
81
  "dependencies": {
82
- "@aws-amplify/api-rest": "4.0.6-unstable.f2b7a8d.0+f2b7a8d",
83
- "@aws-amplify/core": "6.0.6-unstable.f2b7a8d.0+f2b7a8d",
82
+ "@aws-amplify/api-rest": "4.0.7-unstable.53dc717.0+53dc717",
83
+ "@aws-amplify/core": "6.0.7-unstable.53dc717.0+53dc717",
84
84
  "@aws-amplify/data-schema-types": "^0.6.6",
85
85
  "@aws-sdk/types": "3.387.0",
86
86
  "graphql": "15.8.0",
@@ -96,55 +96,5 @@
96
96
  "limit": "91.7 kB"
97
97
  }
98
98
  ],
99
- "jest": {
100
- "globals": {
101
- "ts-jest": {
102
- "diagnostics": false,
103
- "tsConfig": {
104
- "lib": [
105
- "dom",
106
- "es2020"
107
- ],
108
- "allowJs": true,
109
- "noEmitOnError": false,
110
- "strictNullChecks": true,
111
- "types": [
112
- "@types/jest"
113
- ]
114
- }
115
- }
116
- },
117
- "transform": {
118
- "^.+\\.(js|jsx|ts|tsx)$": "ts-jest"
119
- },
120
- "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$",
121
- "testPathIgnorePatterns": [
122
- "<rootDir>/__tests__/helpers.ts",
123
- "<rootDir>/__tests__/fixtures/",
124
- "<rootDir>/__tests__/utils/"
125
- ],
126
- "moduleFileExtensions": [
127
- "ts",
128
- "tsx",
129
- "js",
130
- "json",
131
- "jsx"
132
- ],
133
- "testEnvironment": "jsdom",
134
- "testURL": "http://localhost/",
135
- "coverageThreshold": {
136
- "global": {
137
- "branches": 0,
138
- "functions": 0,
139
- "lines": 0,
140
- "statements": 0
141
- }
142
- },
143
- "coveragePathIgnorePatterns": [
144
- "<rootDir>/node_modules/",
145
- "<rootDir>/dist",
146
- "<rootDir>/__tests__"
147
- ]
148
- },
149
- "gitHead": "f2b7a8d78058af2e1c04f7d507525641def667c3"
99
+ "gitHead": "53dc71715e37198fe4c7378de3ccd993d565f8dd"
150
100
  }
package/src/GraphQLAPI.ts CHANGED
@@ -29,7 +29,7 @@ export class GraphQLAPIClass extends InternalGraphQLAPIClass {
29
29
  * Executes a GraphQL operation
30
30
  *
31
31
  * @param options - GraphQL Options
32
- * @param [additionalHeaders] - headers to merge in after any `graphql_headers` set in the config
32
+ * @param [additionalHeaders] - headers to merge in after any `libraryConfigHeaders` set in the config
33
33
  * @returns An Observable if the query is a subscription query, else a promise of the graphql result.
34
34
  */
35
35
  graphql<T = any>(
@@ -97,7 +97,7 @@ export interface AWSAppSyncRealTimeProviderOptions {
97
97
  variables?: Record<string, DocumentType>;
98
98
  apiKey?: string;
99
99
  region?: string;
100
- graphql_headers?: () => {} | (() => Promise<{}>);
100
+ libraryConfigHeaders?: () => {} | (() => Promise<{}>);
101
101
  additionalHeaders?:
102
102
  | Record<string, string>
103
103
  | (() => Promise<Record<string, string>>);
@@ -203,6 +203,7 @@ export class AWSAppSyncRealTimeProvider {
203
203
  additionalHeaders,
204
204
  apiKey,
205
205
  authToken,
206
+ libraryConfigHeaders,
206
207
  } = options || {};
207
208
 
208
209
  return new Observable(observer => {
@@ -235,6 +236,7 @@ export class AWSAppSyncRealTimeProvider {
235
236
  additionalHeaders,
236
237
  apiKey,
237
238
  authToken,
239
+ libraryConfigHeaders,
238
240
  },
239
241
  observer,
240
242
  subscriptionId,
@@ -313,7 +315,7 @@ export class AWSAppSyncRealTimeProvider {
313
315
  variables,
314
316
  apiKey,
315
317
  region,
316
- graphql_headers = () => ({}),
318
+ libraryConfigHeaders = () => ({}),
317
319
  additionalHeaders = {},
318
320
  authToken,
319
321
  } = options;
@@ -361,7 +363,7 @@ export class AWSAppSyncRealTimeProvider {
361
363
  region,
362
364
  additionalCustomHeaders,
363
365
  })),
364
- ...(await graphql_headers()),
366
+ ...(await libraryConfigHeaders()),
365
367
  ...additionalCustomHeaders,
366
368
  [USER_AGENT_HEADER]: getAmplifyUserAgent(customUserAgentDetails),
367
369
  };
@@ -8,7 +8,7 @@ import {
8
8
  GraphQLError,
9
9
  OperationTypeNode,
10
10
  } from 'graphql';
11
- import { Observable } from 'rxjs';
11
+ import { Observable, catchError } from 'rxjs';
12
12
  import { AmplifyClassV6, ConsoleLogger } from '@aws-amplify/core';
13
13
  import {
14
14
  GraphQLAuthMode,
@@ -31,6 +31,7 @@ import {
31
31
  import { AWSAppSyncRealTimeProvider } from '../Providers/AWSAppSyncRealTimeProvider';
32
32
  import { CustomHeaders } from '@aws-amplify/data-schema-types';
33
33
  import { resolveConfig, resolveLibraryOptions } from '../utils';
34
+ import { repackageUnauthError } from '../utils/errors/repackageAuthError';
34
35
 
35
36
  const USER_AGENT_HEADER = 'x-amz-user-agent';
36
37
 
@@ -149,7 +150,7 @@ export class InternalGraphQLAPIClass {
149
150
  * Executes a GraphQL operation
150
151
  *
151
152
  * @param options - GraphQL Options
152
- * @param [additionalHeaders] - headers to merge in after any `graphql_headers` set in the config
153
+ * @param [additionalHeaders] - headers to merge in after any `libraryConfigHeaders` set in the config
153
154
  * @returns An Observable if the query is a subscription query, else a promise of the graphql result.
154
155
  */
155
156
  graphql<T = any>(
@@ -236,7 +237,7 @@ export class InternalGraphQLAPIClass {
236
237
  endpoint: appSyncGraphqlEndpoint,
237
238
  customEndpoint,
238
239
  customEndpointRegion,
239
- defaultAuthMode
240
+ defaultAuthMode,
240
241
  } = resolveConfig(amplify);
241
242
 
242
243
  const authMode = explicitAuthMode || defaultAuthMode || 'iam';
@@ -391,7 +392,7 @@ export class InternalGraphQLAPIClass {
391
392
  const { errors } = response;
392
393
 
393
394
  if (errors && errors.length) {
394
- throw response;
395
+ throw repackageUnauthError(response);
395
396
  }
396
397
 
397
398
  return response;
@@ -424,19 +425,39 @@ export class InternalGraphQLAPIClass {
424
425
  ): Observable<any> {
425
426
  const config = resolveConfig(amplify);
426
427
 
427
- return this.appSyncRealTime.subscribe(
428
- {
429
- query: print(query as DocumentNode),
430
- variables,
431
- appSyncGraphqlEndpoint: config?.endpoint,
432
- region: config?.region,
433
- authenticationType: authMode || config?.defaultAuthMode,
434
- apiKey: config?.apiKey,
435
- additionalHeaders,
436
- authToken,
437
- },
438
- customUserAgentDetails
439
- );
428
+ /**
429
+ * Retrieve library options from Amplify configuration.
430
+ * `libraryConfigHeaders` are from the Amplify configuration options,
431
+ * and will not be overwritten by other custom headers. These are *not*
432
+ * the same as `additionalHeaders`, which are custom headers that are
433
+ * either 1)included when configuring the API client or 2) passed along
434
+ * with individual requests.
435
+ */
436
+ const { headers: libraryConfigHeaders } = resolveLibraryOptions(amplify);
437
+
438
+ return this.appSyncRealTime
439
+ .subscribe(
440
+ {
441
+ query: print(query as DocumentNode),
442
+ variables,
443
+ appSyncGraphqlEndpoint: config?.endpoint,
444
+ region: config?.region,
445
+ authenticationType: authMode || config?.defaultAuthMode,
446
+ apiKey: config?.apiKey,
447
+ additionalHeaders,
448
+ authToken,
449
+ libraryConfigHeaders,
450
+ },
451
+ customUserAgentDetails
452
+ )
453
+ .pipe(
454
+ catchError(e => {
455
+ if (e.errors) {
456
+ throw repackageUnauthError(e);
457
+ }
458
+ throw e;
459
+ })
460
+ );
440
461
  }
441
462
  }
442
463
 
@@ -175,7 +175,7 @@ export interface AWSAppSyncRealTimeProviderOptions {
175
175
  variables?: Record<string, unknown>;
176
176
  apiKey?: string;
177
177
  region?: string;
178
- graphql_headers?: () => {} | (() => Promise<{}>);
178
+ libraryConfigHeaders?: () => {} | (() => Promise<{}>);
179
179
  additionalHeaders?: CustomHeaders;
180
180
  }
181
181
 
@@ -0,0 +1,46 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import {
5
+ AmplifyErrorParams,
6
+ GraphQLAuthMode,
7
+ } from '@aws-amplify/core/internals/utils';
8
+
9
+ type ErrorObject = {
10
+ errors: AmplifyErrorParams[];
11
+ };
12
+
13
+ /**
14
+ * Checks to see if the given response or subscription message contains an
15
+ * unauth error. If it does, it changes the error message to include instructions
16
+ * for the app developer.
17
+ */
18
+ export function repackageUnauthError<T extends ErrorObject>(content: T): T {
19
+ if (content.errors && Array.isArray(content.errors)) {
20
+ content.errors.forEach(e => {
21
+ if (isUnauthError(e)) {
22
+ e.message = 'Unauthorized';
23
+ e.recoverySuggestion =
24
+ `If you're calling an Amplify-generated API, make sure ` +
25
+ `to set the "authMode" in generateClient({ authMode: '...' }) to the backend authorization ` +
26
+ `rule's auth provider ('apiKey', 'userPool', 'iam', 'oidc', 'lambda')`;
27
+ }
28
+ });
29
+ }
30
+ return content;
31
+ }
32
+
33
+ function isUnauthError(error: any): boolean {
34
+ // Error pattern corresponding to appsync calls
35
+ if (error?.['originalError']?.['name']?.startsWith('UnauthorizedException')) {
36
+ return true;
37
+ }
38
+ // Error pattern corresponding to appsync subscriptions
39
+ if (
40
+ error.message?.startsWith('Connection failed:') &&
41
+ error.message?.includes('Permission denied')
42
+ ) {
43
+ return true;
44
+ }
45
+ return false;
46
+ }