@aws-amplify/api-graphql 4.0.2 → 4.0.3-unstable.407b52e.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 (52) hide show
  1. package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js +32 -12
  2. package/dist/cjs/Providers/AWSAppSyncRealTimeProvider/index.js.map +1 -1
  3. package/dist/cjs/internals/APIClient.js +17 -1
  4. package/dist/cjs/internals/APIClient.js.map +1 -1
  5. package/dist/cjs/internals/InternalGraphQLAPI.js +38 -16
  6. package/dist/cjs/internals/InternalGraphQLAPI.js.map +1 -1
  7. package/dist/cjs/internals/generateClient.js +1 -0
  8. package/dist/cjs/internals/generateClient.js.map +1 -1
  9. package/dist/cjs/internals/operations/get.js +3 -2
  10. package/dist/cjs/internals/operations/get.js.map +1 -1
  11. package/dist/cjs/internals/operations/list.js +3 -2
  12. package/dist/cjs/internals/operations/list.js.map +1 -1
  13. package/dist/cjs/internals/operations/subscription.js +2 -1
  14. package/dist/cjs/internals/operations/subscription.js.map +1 -1
  15. package/dist/cjs/types/index.js +2 -1
  16. package/dist/cjs/types/index.js.map +1 -1
  17. package/dist/esm/GraphQLAPI.d.ts +2 -3
  18. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.d.ts +3 -3
  19. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs +32 -12
  20. package/dist/esm/Providers/AWSAppSyncRealTimeProvider/index.mjs.map +1 -1
  21. package/dist/esm/index.mjs +1 -1
  22. package/dist/esm/internals/APIClient.d.ts +9 -1
  23. package/dist/esm/internals/APIClient.mjs +17 -2
  24. package/dist/esm/internals/APIClient.mjs.map +1 -1
  25. package/dist/esm/internals/InternalGraphQLAPI.d.ts +2 -3
  26. package/dist/esm/internals/InternalGraphQLAPI.mjs +38 -16
  27. package/dist/esm/internals/InternalGraphQLAPI.mjs.map +1 -1
  28. package/dist/esm/internals/generateClient.mjs +2 -1
  29. package/dist/esm/internals/generateClient.mjs.map +1 -1
  30. package/dist/esm/internals/operations/get.mjs +4 -3
  31. package/dist/esm/internals/operations/get.mjs.map +1 -1
  32. package/dist/esm/internals/operations/list.mjs +4 -3
  33. package/dist/esm/internals/operations/list.mjs.map +1 -1
  34. package/dist/esm/internals/operations/subscription.mjs +3 -2
  35. package/dist/esm/internals/operations/subscription.mjs.map +1 -1
  36. package/dist/esm/internals/types.d.ts +2 -0
  37. package/dist/esm/internals/v6.d.ts +2 -3
  38. package/dist/esm/types/index.d.ts +9 -10
  39. package/dist/esm/types/index.mjs +2 -1
  40. package/dist/esm/types/index.mjs.map +1 -1
  41. package/package.json +6 -6
  42. package/src/GraphQLAPI.ts +2 -1
  43. package/src/Providers/AWSAppSyncRealTimeProvider/index.ts +39 -11
  44. package/src/internals/APIClient.ts +25 -0
  45. package/src/internals/InternalGraphQLAPI.ts +61 -21
  46. package/src/internals/generateClient.ts +8 -1
  47. package/src/internals/operations/get.ts +14 -6
  48. package/src/internals/operations/list.ts +13 -6
  49. package/src/internals/operations/subscription.ts +11 -5
  50. package/src/internals/types.ts +2 -0
  51. package/src/internals/v6.ts +2 -1
  52. package/src/types/index.ts +13 -13
@@ -1,5 +1,6 @@
1
1
  import { AmplifyClassV6 } from '@aws-amplify/core';
2
2
  import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
3
+ import { CustomHeaders } from '@aws-amplify/data-schema-types';
3
4
  /**
4
5
  * @private
5
6
  *
@@ -14,4 +15,5 @@ export type ClientGenerationParams = {
14
15
  export type CommonPublicClientOptions = {
15
16
  authMode?: GraphQLAuthMode;
16
17
  authToken?: string;
18
+ headers?: CustomHeaders;
17
19
  };
@@ -1,4 +1,5 @@
1
1
  import { V6Client, GraphQLOptionsV6, GraphQLResponseV6 } from '../types';
2
+ import { CustomHeaders } from '@aws-amplify/data-schema-types';
2
3
  /**
3
4
  * Invokes graphql operations against a graphql service, providing correct input and
4
5
  * output types if Amplify-generated graphql from a recent version of the CLI/codegen
@@ -83,9 +84,7 @@ import { V6Client, GraphQLOptionsV6, GraphQLResponseV6 } from '../types';
83
84
  * @param options
84
85
  * @param additionalHeaders
85
86
  */
86
- export declare function graphql<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(this: V6Client, options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: {
87
- [key: string]: string;
88
- }): GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
87
+ export declare function graphql<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(this: V6Client, options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: CustomHeaders): GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
89
88
  /**
90
89
  * Cancels an inflight request. Only applicable for graphql queries and mutations
91
90
  * @param {any} request - request to cancel
@@ -1,5 +1,5 @@
1
1
  import { AmplifyClassV6, ResourcesConfig } from '@aws-amplify/core';
2
- import { ModelTypes } from '@aws-amplify/data-schema-types';
2
+ import { ModelTypes, CustomHeaders } from '@aws-amplify/data-schema-types';
3
3
  import { Source, DocumentNode, GraphQLError } from 'graphql';
4
4
  export { OperationTypeNode } from 'graphql';
5
5
  import { Observable } from 'rxjs';
@@ -143,9 +143,7 @@ export interface AWSAppSyncRealTimeProviderOptions {
143
143
  apiKey?: string;
144
144
  region?: string;
145
145
  graphql_headers?: () => {} | (() => Promise<{}>);
146
- additionalHeaders?: {
147
- [key: string]: string;
148
- };
146
+ additionalHeaders?: CustomHeaders;
149
147
  }
150
148
  export type AWSAppSyncRealTimeProvider = {
151
149
  subscribe(options?: AWSAppSyncRealTimeProviderOptions): Observable<Record<string, unknown>>;
@@ -285,11 +283,13 @@ type ExcludeNeverFields<O> = {
285
283
  export declare const __amplify: unique symbol;
286
284
  export declare const __authMode: unique symbol;
287
285
  export declare const __authToken: unique symbol;
286
+ export declare const __headers: unique symbol;
288
287
  export type ClientWithModels = V6Client<Record<string, any>> | V6ClientSSRRequest<Record<string, any>> | V6ClientSSRCookies<Record<string, any>>;
289
288
  export type V6Client<T extends Record<any, any> = never> = ExcludeNeverFields<{
290
289
  [__amplify]: AmplifyClassV6;
291
290
  [__authMode]?: GraphQLAuthMode;
292
291
  [__authToken]?: string;
292
+ [__headers]?: CustomHeaders;
293
293
  graphql: GraphQLMethod;
294
294
  cancel: (promise: Promise<any>, message?: string) => boolean;
295
295
  isCancelError: (error: any) => boolean;
@@ -299,6 +299,7 @@ export type V6ClientSSRRequest<T extends Record<any, any> = never> = ExcludeNeve
299
299
  [__amplify]: AmplifyClassV6;
300
300
  [__authMode]?: GraphQLAuthMode;
301
301
  [__authToken]?: string;
302
+ [__headers]?: CustomHeaders;
302
303
  graphql: GraphQLMethodSSR;
303
304
  cancel: (promise: Promise<any>, message?: string) => boolean;
304
305
  isCancelError: (error: any) => boolean;
@@ -308,17 +309,14 @@ export type V6ClientSSRCookies<T extends Record<any, any> = never> = ExcludeNeve
308
309
  [__amplify]: AmplifyClassV6;
309
310
  [__authMode]?: GraphQLAuthMode;
310
311
  [__authToken]?: string;
312
+ [__headers]?: CustomHeaders;
311
313
  graphql: GraphQLMethod;
312
314
  cancel: (promise: Promise<any>, message?: string) => boolean;
313
315
  isCancelError: (error: any) => boolean;
314
316
  models: ModelTypes<T, 'COOKIES'>;
315
317
  }>;
316
- export type GraphQLMethod = <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: {
317
- [key: string]: string;
318
- } | undefined) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
319
- export type GraphQLMethodSSR = <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(contextSpec: AmplifyServer.ContextSpec, options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: {
320
- [key: string]: string;
321
- } | undefined) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
318
+ export type GraphQLMethod = <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: CustomHeaders | undefined) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
319
+ export type GraphQLMethodSSR = <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(contextSpec: AmplifyServer.ContextSpec, options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: CustomHeaders | undefined) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
322
320
  /**
323
321
  * @private
324
322
  *
@@ -332,6 +330,7 @@ export type QueryArgs = Record<string, unknown>;
332
330
  export type ListArgs = {
333
331
  selectionSet?: string[];
334
332
  filter?: {};
333
+ headers?: CustomHeaders;
335
334
  };
336
335
  export type AuthModeParams = {
337
336
  authMode?: GraphQLAuthMode;
@@ -11,6 +11,7 @@ var GraphQLAuthError;
11
11
  const __amplify = Symbol('amplify');
12
12
  const __authMode = Symbol('authMode');
13
13
  const __authToken = Symbol('authToken');
14
+ const __headers = Symbol('headers');
14
15
 
15
- export { GraphQLAuthError, __amplify, __authMode, __authToken };
16
+ export { GraphQLAuthError, __amplify, __authMode, __authToken, __headers };
16
17
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../../../src/types/index.ts"],"sourcesContent":["export { CONTROL_MSG, ConnectionState } from './PubSub';\nexport var GraphQLAuthError;\n(function (GraphQLAuthError) {\n GraphQLAuthError[\"NO_API_KEY\"] = \"No api-key configured\";\n GraphQLAuthError[\"NO_CURRENT_USER\"] = \"No current user\";\n GraphQLAuthError[\"NO_CREDENTIALS\"] = \"No credentials\";\n GraphQLAuthError[\"NO_FEDERATED_JWT\"] = \"No federated jwt\";\n GraphQLAuthError[\"NO_AUTH_TOKEN\"] = \"No auth token specified\";\n})(GraphQLAuthError || (GraphQLAuthError = {}));\nexport const __amplify = Symbol('amplify');\nexport const __authMode = Symbol('authMode');\nexport const __authToken = Symbol('authToken');\n"],"names":[],"mappings":";;AACU,IAAC,iBAAiB;AAC5B,CAAC,UAAU,gBAAgB,EAAE;AAC7B,IAAI,gBAAgB,CAAC,YAAY,CAAC,GAAG,uBAAuB,CAAC;AAC7D,IAAI,gBAAgB,CAAC,iBAAiB,CAAC,GAAG,iBAAiB,CAAC;AAC5D,IAAI,gBAAgB,CAAC,gBAAgB,CAAC,GAAG,gBAAgB,CAAC;AAC1D,IAAI,gBAAgB,CAAC,kBAAkB,CAAC,GAAG,kBAAkB,CAAC;AAC9D,IAAI,gBAAgB,CAAC,eAAe,CAAC,GAAG,yBAAyB,CAAC;AAClE,CAAC,EAAE,gBAAgB,KAAK,gBAAgB,GAAG,EAAE,CAAC,CAAC,CAAC;AACpC,MAAC,SAAS,GAAG,MAAM,CAAC,SAAS,EAAE;AAC/B,MAAC,UAAU,GAAG,MAAM,CAAC,UAAU,EAAE;AACjC,MAAC,WAAW,GAAG,MAAM,CAAC,WAAW;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":["../../../src/types/index.ts"],"sourcesContent":["export { CONTROL_MSG, ConnectionState } from './PubSub';\nexport var GraphQLAuthError;\n(function (GraphQLAuthError) {\n GraphQLAuthError[\"NO_API_KEY\"] = \"No api-key configured\";\n GraphQLAuthError[\"NO_CURRENT_USER\"] = \"No current user\";\n GraphQLAuthError[\"NO_CREDENTIALS\"] = \"No credentials\";\n GraphQLAuthError[\"NO_FEDERATED_JWT\"] = \"No federated jwt\";\n GraphQLAuthError[\"NO_AUTH_TOKEN\"] = \"No auth token specified\";\n})(GraphQLAuthError || (GraphQLAuthError = {}));\nexport const __amplify = Symbol('amplify');\nexport const __authMode = Symbol('authMode');\nexport const __authToken = Symbol('authToken');\nexport const __headers = Symbol('headers');\n"],"names":[],"mappings":";;AACU,IAAC,iBAAiB;AAC5B,CAAC,UAAU,gBAAgB,EAAE;AAC7B,IAAI,gBAAgB,CAAC,YAAY,CAAC,GAAG,uBAAuB,CAAC;AAC7D,IAAI,gBAAgB,CAAC,iBAAiB,CAAC,GAAG,iBAAiB,CAAC;AAC5D,IAAI,gBAAgB,CAAC,gBAAgB,CAAC,GAAG,gBAAgB,CAAC;AAC1D,IAAI,gBAAgB,CAAC,kBAAkB,CAAC,GAAG,kBAAkB,CAAC;AAC9D,IAAI,gBAAgB,CAAC,eAAe,CAAC,GAAG,yBAAyB,CAAC;AAClE,CAAC,EAAE,gBAAgB,KAAK,gBAAgB,GAAG,EAAE,CAAC,CAAC,CAAC;AACpC,MAAC,SAAS,GAAG,MAAM,CAAC,SAAS,EAAE;AAC/B,MAAC,UAAU,GAAG,MAAM,CAAC,UAAU,EAAE;AACjC,MAAC,WAAW,GAAG,MAAM,CAAC,WAAW,EAAE;AACnC,MAAC,SAAS,GAAG,MAAM,CAAC,SAAS;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/api-graphql",
3
- "version": "4.0.2",
3
+ "version": "4.0.3-unstable.407b52e.0+407b52e",
4
4
  "description": "Api-graphql category of aws-amplify",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.mjs",
@@ -67,7 +67,7 @@
67
67
  },
68
68
  "homepage": "https://aws-amplify.github.io/",
69
69
  "devDependencies": {
70
- "@aws-amplify/data-schema": "^0.11.0",
70
+ "@aws-amplify/data-schema": "^0.12.2",
71
71
  "@rollup/plugin-typescript": "11.1.5",
72
72
  "rollup": "3.29.4",
73
73
  "typescript": "5.0.2"
@@ -79,9 +79,9 @@
79
79
  "internals"
80
80
  ],
81
81
  "dependencies": {
82
- "@aws-amplify/api-rest": "4.0.2",
83
- "@aws-amplify/core": "6.0.2",
84
- "@aws-amplify/data-schema-types": "^0.4.2",
82
+ "@aws-amplify/api-rest": "4.0.3-unstable.407b52e.0+407b52e",
83
+ "@aws-amplify/core": "6.0.3-unstable.407b52e.0+407b52e",
84
+ "@aws-amplify/data-schema-types": "^0.6.2",
85
85
  "@aws-sdk/types": "3.387.0",
86
86
  "graphql": "15.8.0",
87
87
  "rxjs": "^7.8.1",
@@ -146,5 +146,5 @@
146
146
  "<rootDir>/__tests__"
147
147
  ]
148
148
  },
149
- "gitHead": "e9c04a6d233f812b2888aa51a71b66a4040af8c0"
149
+ "gitHead": "407b52ef88164023d37e2fd63cbdfde26918a2e8"
150
150
  }
package/src/GraphQLAPI.ts CHANGED
@@ -4,6 +4,7 @@ import { AmplifyClassV6 } from '@aws-amplify/core';
4
4
  import { Category, ApiAction } from '@aws-amplify/core/internals/utils';
5
5
  import { GraphQLOptions, GraphQLResult } from './types';
6
6
  import { InternalGraphQLAPIClass } from './internals/InternalGraphQLAPI';
7
+ import { CustomHeaders } from '@aws-amplify/data-schema-types';
7
8
  import { Observable } from 'rxjs';
8
9
 
9
10
  export const graphqlOperation = (
@@ -34,7 +35,7 @@ export class GraphQLAPIClass extends InternalGraphQLAPIClass {
34
35
  graphql<T = any>(
35
36
  amplify: AmplifyClassV6 | (() => Promise<AmplifyClassV6>),
36
37
  options: GraphQLOptions,
37
- additionalHeaders?: { [key: string]: string }
38
+ additionalHeaders?: CustomHeaders
38
39
  ): Observable<GraphQLResult<T>> | Promise<GraphQLResult<T>> {
39
40
  return super.graphql(amplify, options, additionalHeaders, {
40
41
  category: Category.API,
@@ -98,7 +98,11 @@ export interface AWSAppSyncRealTimeProviderOptions {
98
98
  apiKey?: string;
99
99
  region?: string;
100
100
  graphql_headers?: () => {} | (() => Promise<{}>);
101
- additionalHeaders?: { [key: string]: string };
101
+ additionalHeaders?:
102
+ | Record<string, string>
103
+ | (() => Promise<Record<string, string>>);
104
+ additionalCustomHeaders?: Record<string, string>;
105
+ authToken?: string;
102
106
  }
103
107
 
104
108
  type AWSAppSyncRealTimeAuthInput =
@@ -198,6 +202,7 @@ export class AWSAppSyncRealTimeProvider {
198
202
  authenticationType,
199
203
  additionalHeaders,
200
204
  apiKey,
205
+ authToken,
201
206
  } = options || {};
202
207
 
203
208
  return new Observable(observer => {
@@ -229,6 +234,7 @@ export class AWSAppSyncRealTimeProvider {
229
234
  appSyncGraphqlEndpoint,
230
235
  additionalHeaders,
231
236
  apiKey,
237
+ authToken,
232
238
  },
233
239
  observer,
234
240
  subscriptionId,
@@ -309,8 +315,25 @@ export class AWSAppSyncRealTimeProvider {
309
315
  region,
310
316
  graphql_headers = () => ({}),
311
317
  additionalHeaders = {},
318
+ authToken,
312
319
  } = options;
313
320
 
321
+ let additionalCustomHeaders: Record<string, string> = {};
322
+
323
+ if (typeof additionalHeaders === 'function') {
324
+ additionalCustomHeaders = await additionalHeaders();
325
+ } else {
326
+ additionalCustomHeaders = additionalHeaders;
327
+ }
328
+
329
+ // if an authorization header is set, have the explicit authToken take precedence
330
+ if (authToken) {
331
+ additionalCustomHeaders = {
332
+ ...additionalCustomHeaders,
333
+ Authorization: authToken,
334
+ };
335
+ }
336
+
314
337
  const subscriptionState: SUBSCRIPTION_STATUS = SUBSCRIPTION_STATUS.PENDING;
315
338
  const data = {
316
339
  query,
@@ -336,10 +359,10 @@ export class AWSAppSyncRealTimeProvider {
336
359
  payload: dataString,
337
360
  canonicalUri: '',
338
361
  region,
339
- additionalHeaders,
362
+ additionalCustomHeaders,
340
363
  })),
341
364
  ...(await graphql_headers()),
342
- ...additionalHeaders,
365
+ ...additionalCustomHeaders,
343
366
  [USER_AGENT_HEADER]: getAmplifyUserAgent(customUserAgentDetails),
344
367
  };
345
368
 
@@ -365,7 +388,7 @@ export class AWSAppSyncRealTimeProvider {
365
388
  appSyncGraphqlEndpoint,
366
389
  authenticationType,
367
390
  region,
368
- additionalHeaders,
391
+ additionalCustomHeaders,
369
392
  });
370
393
  } catch (err: any) {
371
394
  this._logStartSubscriptionError(subscriptionId, observer, err);
@@ -679,7 +702,7 @@ export class AWSAppSyncRealTimeProvider {
679
702
  authenticationType,
680
703
  apiKey,
681
704
  region,
682
- additionalHeaders,
705
+ additionalCustomHeaders,
683
706
  }: AWSAppSyncRealTimeProviderOptions) {
684
707
  if (this.socketStatus === SOCKET_STATUS.READY) {
685
708
  return;
@@ -700,7 +723,7 @@ export class AWSAppSyncRealTimeProvider {
700
723
  apiKey,
701
724
  appSyncGraphqlEndpoint,
702
725
  region,
703
- additionalHeaders,
726
+ additionalCustomHeaders,
704
727
  });
705
728
 
706
729
  const headerString = authHeader ? JSON.stringify(authHeader) : '';
@@ -883,7 +906,7 @@ export class AWSAppSyncRealTimeProvider {
883
906
  canonicalUri,
884
907
  appSyncGraphqlEndpoint,
885
908
  region,
886
- additionalHeaders,
909
+ additionalCustomHeaders,
887
910
  }: AWSAppSyncRealTimeAuthInput): Promise<
888
911
  Record<string, unknown> | undefined
889
912
  > {
@@ -920,7 +943,7 @@ export class AWSAppSyncRealTimeProvider {
920
943
  apiKey: resolvedApiKey,
921
944
  region,
922
945
  host,
923
- additionalHeaders,
946
+ additionalCustomHeaders,
924
947
  });
925
948
 
926
949
  return result;
@@ -989,14 +1012,19 @@ export class AWSAppSyncRealTimeProvider {
989
1012
 
990
1013
  private _customAuthHeader({
991
1014
  host,
992
- additionalHeaders,
1015
+ additionalCustomHeaders,
993
1016
  }: AWSAppSyncRealTimeAuthInput) {
994
- if (!additionalHeaders?.['Authorization']) {
1017
+ /**
1018
+ * If `additionalHeaders` was provided to the subscription as a function,
1019
+ * the headers that are returned by that function will already have been
1020
+ * provided before this function is called.
1021
+ */
1022
+ if (!additionalCustomHeaders?.['Authorization']) {
995
1023
  throw new Error('No auth token specified');
996
1024
  }
997
1025
 
998
1026
  return {
999
- Authorization: additionalHeaders.Authorization,
1027
+ Authorization: additionalCustomHeaders.Authorization,
1000
1028
  host,
1001
1029
  };
1002
1030
  }
@@ -16,17 +16,21 @@ import {
16
16
  ListArgs,
17
17
  QueryArgs,
18
18
  V6Client,
19
+ V6ClientSSRCookies,
19
20
  V6ClientSSRRequest,
20
21
  __authMode,
21
22
  __authToken,
23
+ __headers,
22
24
  } from '../types';
23
25
  import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
26
+ import { CustomHeaders } from '@aws-amplify/data-schema-types';
24
27
 
25
28
  type LazyLoadOptions = {
26
29
  authMode?: GraphQLAuthMode;
27
30
  authToken?: string | undefined;
28
31
  limit?: number | undefined;
29
32
  nextToken?: string | undefined | null;
33
+ headers?: CustomHeaders | undefined;
30
34
  };
31
35
 
32
36
  const connectionType = {
@@ -778,3 +782,24 @@ export function authModeParams(
778
782
  authToken: options.authToken || client[__authToken],
779
783
  };
780
784
  }
785
+
786
+ /**
787
+ * Retrieves custom headers from either the client or request options.
788
+ * @param {client} V6Client | V6ClientSSRRequest | V6ClientSSRCookies - for extracting client headers
789
+ * @param {requestHeaders} [CustomHeaders] - request headers
790
+ * @returns {CustomHeaders} - custom headers
791
+ */
792
+ export function getCustomHeaders(
793
+ client: V6Client | ClientWithModels,
794
+ requestHeaders?: CustomHeaders
795
+ ): CustomHeaders {
796
+ let headers: CustomHeaders = client[__headers] || {};
797
+
798
+ // Individual request headers will take precedence over client headers.
799
+ // We intentionally do *not* merge client and request headers.
800
+ if (requestHeaders) {
801
+ headers = requestHeaders;
802
+ }
803
+
804
+ return headers;
805
+ }
@@ -29,6 +29,7 @@ import {
29
29
  updateRequestToBeCancellable,
30
30
  } from '@aws-amplify/api-rest/internals';
31
31
  import { AWSAppSyncRealTimeProvider } from '../Providers/AWSAppSyncRealTimeProvider';
32
+ import { CustomHeaders } from '@aws-amplify/data-schema-types';
32
33
  import { resolveConfig, resolveLibraryOptions } from '../utils';
33
34
 
34
35
  const USER_AGENT_HEADER = 'x-amz-user-agent';
@@ -66,9 +67,8 @@ export class InternalGraphQLAPIClass {
66
67
  private async _headerBasedAuth(
67
68
  amplify: AmplifyClassV6,
68
69
  authMode: GraphQLAuthMode,
69
- additionalHeaders: { [key: string]: string } = {}
70
+ additionalHeaders: Record<string, string> = {}
70
71
  ) {
71
- const config = amplify.getConfig();
72
72
  const {
73
73
  region: region,
74
74
  endpoint: appSyncGraphqlEndpoint,
@@ -114,9 +114,13 @@ export class InternalGraphQLAPIClass {
114
114
  }
115
115
  break;
116
116
  case 'lambda':
117
- if (!additionalHeaders.Authorization) {
117
+ if (
118
+ typeof additionalHeaders === 'object' &&
119
+ !additionalHeaders.Authorization
120
+ ) {
118
121
  throw new Error(GraphQLAuthError.NO_AUTH_TOKEN);
119
122
  }
123
+
120
124
  headers = {
121
125
  Authorization: additionalHeaders.Authorization,
122
126
  };
@@ -155,7 +159,7 @@ export class InternalGraphQLAPIClass {
155
159
  | AmplifyClassV6
156
160
  | ((fn: (amplify: any) => Promise<any>) => Promise<AmplifyClassV6>),
157
161
  { query: paramQuery, variables = {}, authMode, authToken }: GraphQLOptions,
158
- additionalHeaders?: { [key: string]: string },
162
+ additionalHeaders?: CustomHeaders,
159
163
  customUserAgentDetails?: CustomUserAgentDetails
160
164
  ): Observable<GraphQLResult<T>> | Promise<GraphQLResult<T>> {
161
165
  const query =
@@ -171,11 +175,6 @@ export class InternalGraphQLAPIClass {
171
175
 
172
176
  const headers = additionalHeaders || {};
173
177
 
174
- // if an authorization header is set, have the explicit authToken take precedence
175
- if (authToken) {
176
- headers.Authorization = authToken;
177
- }
178
-
179
178
  switch (operationType) {
180
179
  case 'query':
181
180
  case 'mutation':
@@ -189,7 +188,8 @@ export class InternalGraphQLAPIClass {
189
188
  { query, variables, authMode },
190
189
  headers,
191
190
  abortController,
192
- customUserAgentDetails
191
+ customUserAgentDetails,
192
+ authToken
193
193
  );
194
194
  } else {
195
195
  const wrapper = (amplifyInstance: AmplifyClassV6) =>
@@ -198,7 +198,8 @@ export class InternalGraphQLAPIClass {
198
198
  { query, variables, authMode },
199
199
  headers,
200
200
  abortController,
201
- customUserAgentDetails
201
+ customUserAgentDetails,
202
+ authToken
202
203
  );
203
204
 
204
205
  responsePromise = amplify(wrapper) as unknown as Promise<
@@ -216,7 +217,8 @@ export class InternalGraphQLAPIClass {
216
217
  amplify as AmplifyClassV6,
217
218
  { query, variables, authMode },
218
219
  headers,
219
- customUserAgentDetails
220
+ customUserAgentDetails,
221
+ authToken
220
222
  );
221
223
  default:
222
224
  throw new Error(`invalid operation type: ${operationType}`);
@@ -226,9 +228,10 @@ export class InternalGraphQLAPIClass {
226
228
  private async _graphql<T = any>(
227
229
  amplify: AmplifyClassV6,
228
230
  { query, variables, authMode }: GraphQLOptions,
229
- additionalHeaders = {},
231
+ additionalHeaders: CustomHeaders = {},
230
232
  abortController: AbortController,
231
- customUserAgentDetails?: CustomUserAgentDetails
233
+ customUserAgentDetails?: CustomUserAgentDetails,
234
+ authToken?: string
232
235
  ): Promise<GraphQLResult<T>> {
233
236
  const {
234
237
  region: region,
@@ -237,14 +240,45 @@ export class InternalGraphQLAPIClass {
237
240
  customEndpointRegion,
238
241
  } = resolveConfig(amplify);
239
242
 
240
- // Retrieve library options from Amplify configuration
243
+ /**
244
+ * Retrieve library options from Amplify configuration.
245
+ * `customHeaders` here are from the Amplify configuration options,
246
+ * and are for non-AppSync endpoints only. These are *not* the same as
247
+ * `additionalHeaders`, which are custom headers that are either 1)
248
+ * included when configuring the API client or 2) passed along with
249
+ * individual requests.
250
+ */
241
251
  const { headers: customHeaders, withCredentials } =
242
252
  resolveLibraryOptions(amplify);
243
253
 
254
+ /**
255
+ * Client or request-specific custom headers that may or may not be
256
+ * returned by a function:
257
+ */
258
+ let additionalCustomHeaders: Record<string, string>;
259
+
260
+ if (typeof additionalHeaders === 'function') {
261
+ additionalCustomHeaders = await additionalHeaders();
262
+ } else {
263
+ additionalCustomHeaders = additionalHeaders;
264
+ }
265
+
266
+ // if an authorization header is set, have the explicit authToken take precedence
267
+ if (authToken) {
268
+ additionalCustomHeaders = {
269
+ ...additionalCustomHeaders,
270
+ Authorization: authToken,
271
+ };
272
+ }
273
+
244
274
  // TODO: Figure what we need to do to remove `!`'s.
245
275
  const headers = {
246
276
  ...(!customEndpoint &&
247
- (await this._headerBasedAuth(amplify, authMode!, additionalHeaders))),
277
+ (await this._headerBasedAuth(
278
+ amplify,
279
+ authMode!,
280
+ additionalCustomHeaders
281
+ ))),
248
282
  /**
249
283
  * Custom endpoint headers.
250
284
  * If there is both a custom endpoint and custom region present, we get the headers.
@@ -253,7 +287,11 @@ export class InternalGraphQLAPIClass {
253
287
  */
254
288
  ...((customEndpoint &&
255
289
  (customEndpointRegion
256
- ? await this._headerBasedAuth(amplify, authMode!, additionalHeaders)
290
+ ? await this._headerBasedAuth(
291
+ amplify,
292
+ authMode!,
293
+ additionalCustomHeaders
294
+ )
257
295
  : {})) ||
258
296
  {}),
259
297
  // Custom headers included in Amplify configuration options:
@@ -262,8 +300,8 @@ export class InternalGraphQLAPIClass {
262
300
  query: print(query as DocumentNode),
263
301
  variables,
264
302
  }))),
265
- // Headers from individual calls to `graphql`:
266
- ...additionalHeaders,
303
+ // Custom headers from individual requests or API client configuration:
304
+ ...additionalCustomHeaders,
267
305
  // User agent headers:
268
306
  ...(!customEndpoint && {
269
307
  [USER_AGENT_HEADER]: getAmplifyUserAgent(customUserAgentDetails),
@@ -379,8 +417,9 @@ export class InternalGraphQLAPIClass {
379
417
  private _graphqlSubscribe(
380
418
  amplify: AmplifyClassV6,
381
419
  { query, variables, authMode }: GraphQLOptions,
382
- additionalHeaders = {},
383
- customUserAgentDetails?: CustomUserAgentDetails
420
+ additionalHeaders: CustomHeaders = {},
421
+ customUserAgentDetails?: CustomUserAgentDetails,
422
+ authToken?: string
384
423
  ): Observable<any> {
385
424
  const config = resolveConfig(amplify);
386
425
 
@@ -393,6 +432,7 @@ export class InternalGraphQLAPIClass {
393
432
  authenticationType: authMode || config?.defaultAuthMode,
394
433
  apiKey: config?.apiKey,
395
434
  additionalHeaders,
435
+ authToken,
396
436
  },
397
437
  customUserAgentDetails
398
438
  );
@@ -2,7 +2,13 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { graphql, cancel, isCancelError } from './v6';
4
4
  import { generateModelsProperty } from './generateModelsProperty';
5
- import { V6Client, __amplify, __authMode, __authToken } from '../types';
5
+ import {
6
+ V6Client,
7
+ __amplify,
8
+ __authMode,
9
+ __authToken,
10
+ __headers,
11
+ } from '../types';
6
12
  import { ClientGenerationParams } from './types';
7
13
 
8
14
  /**
@@ -21,6 +27,7 @@ export function generateClient<T extends Record<any, any> = never>(
21
27
  [__amplify]: params.amplify,
22
28
  [__authMode]: params.authMode,
23
29
  [__authToken]: params.authToken,
30
+ [__headers]: params.headers,
24
31
  graphql,
25
32
  cancel,
26
33
  isCancelError,
@@ -8,6 +8,7 @@ import {
8
8
  flattenItems,
9
9
  authModeParams,
10
10
  ModelOperation,
11
+ getCustomHeaders,
11
12
  } from '../APIClient';
12
13
  import {
13
14
  AuthModeParams,
@@ -80,6 +81,9 @@ async function _get(
80
81
 
81
82
  try {
82
83
  const auth = authModeParams(client, options);
84
+
85
+ const headers = getCustomHeaders(client, options?.headers);
86
+
83
87
  const { data, extensions } = context
84
88
  ? ((await (client as V6ClientSSRRequest<Record<string, any>>).graphql(
85
89
  context,
@@ -87,13 +91,17 @@ async function _get(
87
91
  ...auth,
88
92
  query,
89
93
  variables,
90
- }
94
+ },
95
+ headers
91
96
  )) as GraphQLResult<any>)
92
- : ((await (client as V6Client<Record<string, any>>).graphql({
93
- ...auth,
94
- query,
95
- variables,
96
- })) as GraphQLResult<any>);
97
+ : ((await (client as V6Client<Record<string, any>>).graphql(
98
+ {
99
+ ...auth,
100
+ query,
101
+ variables,
102
+ },
103
+ headers
104
+ )) as GraphQLResult<any>);
97
105
 
98
106
  // flatten response
99
107
  if (data) {
@@ -7,6 +7,7 @@ import {
7
7
  buildGraphQLVariables,
8
8
  flattenItems,
9
9
  authModeParams,
10
+ getCustomHeaders,
10
11
  } from '../APIClient';
11
12
  import {
12
13
  AuthModeParams,
@@ -66,6 +67,8 @@ async function _list(
66
67
  try {
67
68
  const auth = authModeParams(client, args);
68
69
 
70
+ const headers = getCustomHeaders(client, args?.headers);
71
+
69
72
  const { data, extensions } = !!contextSpec
70
73
  ? ((await (client as V6ClientSSRRequest<Record<string, any>>).graphql(
71
74
  contextSpec,
@@ -73,13 +76,17 @@ async function _list(
73
76
  ...auth,
74
77
  query,
75
78
  variables,
76
- }
79
+ },
80
+ headers
77
81
  )) as GraphQLResult<any>)
78
- : ((await (client as V6Client<Record<string, any>>).graphql({
79
- ...auth,
80
- query,
81
- variables,
82
- })) as GraphQLResult<any>);
82
+ : ((await (client as V6Client<Record<string, any>>).graphql(
83
+ {
84
+ ...auth,
85
+ query,
86
+ variables,
87
+ },
88
+ headers
89
+ )) as GraphQLResult<any>);
83
90
 
84
91
  // flatten response
85
92
  if (data !== undefined) {
@@ -9,6 +9,7 @@ import {
9
9
  buildGraphQLVariables,
10
10
  authModeParams,
11
11
  ModelOperation,
12
+ getCustomHeaders,
12
13
  } from '../APIClient';
13
14
  import {
14
15
  ModelIntrospectionSchema,
@@ -38,11 +39,16 @@ export function subscriptionFactory(
38
39
 
39
40
  const auth = authModeParams(client, args);
40
41
 
41
- const observable = client.graphql({
42
- ...auth,
43
- query,
44
- variables,
45
- }) as GraphqlSubscriptionResult<object>;
42
+ const headers = getCustomHeaders(client, args?.headers);
43
+
44
+ const observable = client.graphql(
45
+ {
46
+ ...auth,
47
+ query,
48
+ variables,
49
+ },
50
+ headers
51
+ ) as GraphqlSubscriptionResult<object>;
46
52
 
47
53
  return observable.pipe(
48
54
  map(value => {