@aws-amplify/api-graphql 4.6.8-unstable.0ecded1.0 → 4.6.8-unstable.ca2e4b8.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 (41) hide show
  1. package/dist/cjs/internals/InternalGraphQLAPI.js +23 -15
  2. package/dist/cjs/internals/InternalGraphQLAPI.js.map +1 -1
  3. package/dist/cjs/internals/generateClient.js +28 -16
  4. package/dist/cjs/internals/generateClient.js.map +1 -1
  5. package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js +11 -1
  6. package/dist/cjs/internals/server/generateClientWithAmplifyInstance.js.map +1 -1
  7. package/dist/cjs/internals/v6.js +32 -2
  8. package/dist/cjs/internals/v6.js.map +1 -1
  9. package/dist/cjs/server/generateClient.js +4 -6
  10. package/dist/cjs/server/generateClient.js.map +1 -1
  11. package/dist/cjs/types/index.js +5 -1
  12. package/dist/cjs/types/index.js.map +1 -1
  13. package/dist/esm/index.mjs +1 -1
  14. package/dist/esm/internals/InternalGraphQLAPI.d.ts +1 -1
  15. package/dist/esm/internals/InternalGraphQLAPI.mjs +23 -15
  16. package/dist/esm/internals/InternalGraphQLAPI.mjs.map +1 -1
  17. package/dist/esm/internals/generateClient.d.ts +1 -1
  18. package/dist/esm/internals/generateClient.mjs +29 -17
  19. package/dist/esm/internals/generateClient.mjs.map +1 -1
  20. package/dist/esm/internals/index.d.ts +1 -0
  21. package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs +25 -3
  22. package/dist/esm/internals/server/generateClientWithAmplifyInstance.mjs.map +1 -1
  23. package/dist/esm/internals/types.d.ts +18 -3
  24. package/dist/esm/internals/v6.d.ts +2 -2
  25. package/dist/esm/internals/v6.mjs +32 -2
  26. package/dist/esm/internals/v6.mjs.map +1 -1
  27. package/dist/esm/server/generateClient.d.ts +4 -1
  28. package/dist/esm/server/generateClient.mjs +4 -6
  29. package/dist/esm/server/generateClient.mjs.map +1 -1
  30. package/dist/esm/types/index.d.ts +63 -16
  31. package/dist/esm/types/index.mjs +5 -1
  32. package/dist/esm/types/index.mjs.map +1 -1
  33. package/package.json +4 -4
  34. package/src/internals/InternalGraphQLAPI.ts +46 -15
  35. package/src/internals/generateClient.ts +39 -19
  36. package/src/internals/index.ts +1 -0
  37. package/src/internals/server/generateClientWithAmplifyInstance.ts +18 -1
  38. package/src/internals/types.ts +23 -4
  39. package/src/internals/v6.ts +44 -5
  40. package/src/server/generateClient.ts +11 -13
  41. package/src/types/index.ts +94 -27
@@ -4,18 +4,21 @@ import { DocumentNode, GraphQLError, Source } from 'graphql';
4
4
  import { Observable } from 'rxjs';
5
5
  import { DocumentType, GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
6
6
  import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
7
+ import { CommonPublicClientOptions } from '../internals/types';
7
8
  export { OperationTypeNode } from 'graphql';
8
9
  export { CONTROL_MSG, ConnectionState } from './PubSub';
9
10
  export { SelectionSet } from '@aws-amplify/data-schema/runtime';
10
- export { CommonPublicClientOptions } from '../internals/types';
11
+ export type { CommonPublicClientOptions };
11
12
  /**
12
13
  * Loose/Unknown options for raw GraphQLAPICategory `graphql()`.
13
14
  */
14
15
  export interface GraphQLOptions {
15
16
  query: string | DocumentNode;
17
+ endpoint?: string;
16
18
  variables?: Record<string, DocumentType>;
17
19
  authMode?: GraphQLAuthMode;
18
20
  authToken?: string;
21
+ apiKey?: string;
19
22
  /**
20
23
  * @deprecated This property should not be used
21
24
  */
@@ -162,16 +165,57 @@ export type GraphQLOperation = Source | string;
162
165
  * API V6 `graphql({options})` type that can leverage branded graphql `query`
163
166
  * objects and fallback types.
164
167
  */
165
- export interface GraphQLOptionsV6<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string> extends Record<string, unknown> {
168
+ export type GraphQLOptionsV6<FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string, Options extends CommonPublicClientOptions = object> = Options['endpoint'] extends string ? Options['apiKey'] extends string ? {
166
169
  query: TYPED_GQL_STRING | DocumentNode;
167
170
  variables?: GraphQLVariablesV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
168
171
  authMode?: GraphQLAuthMode;
172
+ apiKey?: string;
169
173
  authToken?: string;
170
174
  /**
171
175
  * @deprecated This property should not be used
172
176
  */
173
177
  userAgentSuffix?: string;
174
- }
178
+ } : {
179
+ query: TYPED_GQL_STRING | DocumentNode;
180
+ variables?: GraphQLVariablesV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
181
+ authMode?: never;
182
+ apiKey?: never;
183
+ authToken?: string;
184
+ /**
185
+ * @deprecated This property should not be used
186
+ */
187
+ userAgentSuffix?: string;
188
+ } | {
189
+ query: TYPED_GQL_STRING | DocumentNode;
190
+ variables?: GraphQLVariablesV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
191
+ authMode: 'apiKey';
192
+ apiKey: string;
193
+ authToken?: string;
194
+ /**
195
+ * @deprecated This property should not be used
196
+ */
197
+ userAgentSuffix?: string;
198
+ } | {
199
+ query: TYPED_GQL_STRING | DocumentNode;
200
+ variables?: GraphQLVariablesV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
201
+ authMode: Exclude<GraphQLAuthMode, 'apiKey'>;
202
+ apiKey?: never;
203
+ authToken?: string;
204
+ /**
205
+ * @deprecated This property should not be used
206
+ */
207
+ userAgentSuffix?: string;
208
+ } : {
209
+ query: TYPED_GQL_STRING | DocumentNode;
210
+ variables?: GraphQLVariablesV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
211
+ authMode?: GraphQLAuthMode;
212
+ apiKey?: string;
213
+ authToken?: string;
214
+ /**
215
+ * @deprecated This property should not be used
216
+ */
217
+ userAgentSuffix?: string;
218
+ };
175
219
  /**
176
220
  * Result type for `graphql()` operations that don't include any specific
177
221
  * type information. The result could be either a `Promise` or `Subscription`.
@@ -275,26 +319,31 @@ export type GeneratedSubscription<InputType, OutputType> = string & {
275
319
  export declare const __amplify: unique symbol;
276
320
  export declare const __authMode: unique symbol;
277
321
  export declare const __authToken: unique symbol;
322
+ export declare const __apiKey: unique symbol;
278
323
  export declare const __headers: unique symbol;
279
- export declare function getInternals(client: BaseClient): ClientInternals;
324
+ export declare const __endpoint: unique symbol;
325
+ export declare function getInternals(client: BaseClient): ClientInternals & {
326
+ apiKey?: string;
327
+ endpoint?: string;
328
+ };
280
329
  export type ClientWithModels = V6Client | V6ClientSSRRequest | V6ClientSSRCookies;
281
- export type V6Client<T extends Record<any, any> = never> = {
282
- graphql: GraphQLMethod;
330
+ export type V6Client<T extends Record<any, any> = never, Options extends CommonPublicClientOptions = object> = {
331
+ graphql: GraphQLMethod<Options>;
283
332
  cancel(promise: Promise<any>, message?: string): boolean;
284
333
  isCancelError(error: any): boolean;
285
334
  } & ClientExtensions<T>;
286
- export type V6ClientSSRRequest<T extends Record<any, any> = never> = {
287
- graphql: GraphQLMethodSSR;
335
+ export type V6ClientSSRRequest<T extends Record<any, any> = never, Options extends CommonPublicClientOptions = object> = {
336
+ graphql: GraphQLMethodSSR<Options>;
288
337
  cancel(promise: Promise<any>, message?: string): boolean;
289
338
  isCancelError(error: any): boolean;
290
339
  } & ClientExtensionsSSRRequest<T>;
291
- export type V6ClientSSRCookies<T extends Record<any, any> = never> = {
292
- graphql: GraphQLMethod;
340
+ export type V6ClientSSRCookies<T extends Record<any, any> = never, Options extends CommonPublicClientOptions = object> = {
341
+ graphql: GraphQLMethod<Options>;
293
342
  cancel(promise: Promise<any>, message?: string): boolean;
294
343
  isCancelError(error: any): boolean;
295
344
  } & ClientExtensionsSSRCookies<T>;
296
- 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>;
297
- 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>;
345
+ export type GraphQLMethod<Options extends CommonPublicClientOptions> = <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING, Options>, additionalHeaders?: CustomHeaders | undefined) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
346
+ export type GraphQLMethodSSR<Options extends CommonPublicClientOptions> = <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(contextSpec: AmplifyServer.ContextSpec, options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING, Options>, additionalHeaders?: CustomHeaders | undefined) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
298
347
  /**
299
348
  * @private
300
349
  *
@@ -315,8 +364,6 @@ export interface AuthModeParams extends Record<string, unknown> {
315
364
  authMode?: GraphQLAuthMode;
316
365
  authToken?: string;
317
366
  }
318
- export interface GenerateServerClientParams {
367
+ export type GenerateServerClientParams = {
319
368
  config: ResourcesConfig;
320
- authMode?: GraphQLAuthMode;
321
- authToken?: string;
322
- }
369
+ } & CommonPublicClientOptions;
@@ -11,16 +11,20 @@ var GraphQLAuthError;
11
11
  const __amplify = Symbol('amplify');
12
12
  const __authMode = Symbol('authMode');
13
13
  const __authToken = Symbol('authToken');
14
+ const __apiKey = Symbol('apiKey');
14
15
  const __headers = Symbol('headers');
16
+ const __endpoint = Symbol('endpoint');
15
17
  function getInternals(client) {
16
18
  const c = client;
17
19
  return {
18
20
  amplify: c[__amplify],
21
+ apiKey: c[__apiKey],
19
22
  authMode: c[__authMode],
20
23
  authToken: c[__authToken],
24
+ endpoint: c[__endpoint],
21
25
  headers: c[__headers],
22
26
  };
23
27
  }
24
28
 
25
- export { GraphQLAuthError, __amplify, __authMode, __authToken, __headers, getInternals };
29
+ export { GraphQLAuthError, __amplify, __apiKey, __authMode, __authToken, __endpoint, __headers, getInternals };
26
30
  //# 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');\nexport const __headers = Symbol('headers');\nexport function getInternals(client) {\n const c = client;\n return {\n amplify: c[__amplify],\n authMode: c[__authMode],\n authToken: c[__authToken],\n headers: c[__headers],\n };\n}\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,EAAE;AACpC,SAAS,YAAY,CAAC,MAAM,EAAE;AACrC,IAAI,MAAM,CAAC,GAAG,MAAM,CAAC;AACrB,IAAI,OAAO;AACX,QAAQ,OAAO,EAAE,CAAC,CAAC,SAAS,CAAC;AAC7B,QAAQ,QAAQ,EAAE,CAAC,CAAC,UAAU,CAAC;AAC/B,QAAQ,SAAS,EAAE,CAAC,CAAC,WAAW,CAAC;AACjC,QAAQ,OAAO,EAAE,CAAC,CAAC,SAAS,CAAC;AAC7B,KAAK,CAAC;AACN;;;;"}
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 __apiKey = Symbol('apiKey');\nexport const __headers = Symbol('headers');\nexport const __endpoint = Symbol('endpoint');\nexport function getInternals(client) {\n const c = client;\n return {\n amplify: c[__amplify],\n apiKey: c[__apiKey],\n authMode: c[__authMode],\n authToken: c[__authToken],\n endpoint: c[__endpoint],\n headers: c[__headers],\n };\n}\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,QAAQ,GAAG,MAAM,CAAC,QAAQ,EAAE;AAC7B,MAAC,SAAS,GAAG,MAAM,CAAC,SAAS,EAAE;AAC/B,MAAC,UAAU,GAAG,MAAM,CAAC,UAAU,EAAE;AACtC,SAAS,YAAY,CAAC,MAAM,EAAE;AACrC,IAAI,MAAM,CAAC,GAAG,MAAM,CAAC;AACrB,IAAI,OAAO;AACX,QAAQ,OAAO,EAAE,CAAC,CAAC,SAAS,CAAC;AAC7B,QAAQ,MAAM,EAAE,CAAC,CAAC,QAAQ,CAAC;AAC3B,QAAQ,QAAQ,EAAE,CAAC,CAAC,UAAU,CAAC;AAC/B,QAAQ,SAAS,EAAE,CAAC,CAAC,WAAW,CAAC;AACjC,QAAQ,QAAQ,EAAE,CAAC,CAAC,UAAU,CAAC;AAC/B,QAAQ,OAAO,EAAE,CAAC,CAAC,SAAS,CAAC;AAC7B,KAAK,CAAC;AACN;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/api-graphql",
3
- "version": "4.6.8-unstable.0ecded1.0+0ecded1",
3
+ "version": "4.6.8-unstable.ca2e4b8.0+ca2e4b8",
4
4
  "description": "Api-graphql category of aws-amplify",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "module": "./dist/esm/index.mjs",
@@ -84,8 +84,8 @@
84
84
  "server"
85
85
  ],
86
86
  "dependencies": {
87
- "@aws-amplify/api-rest": "4.0.65-unstable.0ecded1.0+0ecded1",
88
- "@aws-amplify/core": "6.8.1-unstable.0ecded1.0+0ecded1",
87
+ "@aws-amplify/api-rest": "4.0.65-unstable.ca2e4b8.0+ca2e4b8",
88
+ "@aws-amplify/core": "6.8.1-unstable.ca2e4b8.0+ca2e4b8",
89
89
  "@aws-amplify/data-schema": "^1.7.0",
90
90
  "@aws-sdk/types": "3.387.0",
91
91
  "graphql": "15.8.0",
@@ -101,5 +101,5 @@
101
101
  "limit": "91.7 kB"
102
102
  }
103
103
  ],
104
- "gitHead": "0ecded15a4d1318fe10b16dbe534a111a12bdc21"
104
+ "gitHead": "ca2e4b87a456dfe9a027e49bdcd912a2cba8e59d"
105
105
  }
@@ -52,7 +52,7 @@ export class InternalGraphQLAPIClass {
52
52
  /**
53
53
  * @private
54
54
  */
55
- private appSyncRealTime = new AWSAppSyncRealTimeProvider();
55
+ private appSyncRealTime = new Map<string, AWSAppSyncRealTimeProvider>();
56
56
 
57
57
  private _api = {
58
58
  post,
@@ -88,7 +88,14 @@ export class InternalGraphQLAPIClass {
88
88
  amplify:
89
89
  | AmplifyClassV6
90
90
  | ((fn: (amplify: any) => Promise<any>) => Promise<AmplifyClassV6>),
91
- { query: paramQuery, variables = {}, authMode, authToken }: GraphQLOptions,
91
+ {
92
+ query: paramQuery,
93
+ variables = {},
94
+ authMode,
95
+ authToken,
96
+ endpoint,
97
+ apiKey,
98
+ }: GraphQLOptions,
92
99
  additionalHeaders?: CustomHeaders,
93
100
  customUserAgentDetails?: CustomUserAgentDetails,
94
101
  ): Observable<GraphQLResult<T>> | Promise<GraphQLResult<T>> {
@@ -115,7 +122,7 @@ export class InternalGraphQLAPIClass {
115
122
  if (isAmplifyInstance(amplify)) {
116
123
  responsePromise = this._graphql<T>(
117
124
  amplify,
118
- { query, variables, authMode },
125
+ { query, variables, authMode, apiKey, endpoint },
119
126
  headers,
120
127
  abortController,
121
128
  customUserAgentDetails,
@@ -127,7 +134,7 @@ export class InternalGraphQLAPIClass {
127
134
  const wrapper = async (amplifyInstance: AmplifyClassV6) => {
128
135
  const result = await this._graphql<T>(
129
136
  amplifyInstance,
130
- { query, variables, authMode },
137
+ { query, variables, authMode, apiKey, endpoint },
131
138
  headers,
132
139
  abortController,
133
140
  customUserAgentDetails,
@@ -152,7 +159,7 @@ export class InternalGraphQLAPIClass {
152
159
  case 'subscription':
153
160
  return this._graphqlSubscribe(
154
161
  amplify as AmplifyClassV6,
155
- { query, variables, authMode },
162
+ { query, variables, authMode, apiKey, endpoint },
156
163
  headers,
157
164
  customUserAgentDetails,
158
165
  authToken,
@@ -164,7 +171,13 @@ export class InternalGraphQLAPIClass {
164
171
 
165
172
  private async _graphql<T = any>(
166
173
  amplify: AmplifyClassV6,
167
- { query, variables, authMode: explicitAuthMode }: GraphQLOptions,
174
+ {
175
+ query,
176
+ variables,
177
+ authMode: authModeOverride,
178
+ endpoint: endpointOverride,
179
+ apiKey: apiKeyOverride,
180
+ }: GraphQLOptions,
168
181
  additionalHeaders: CustomHeaders = {},
169
182
  abortController: AbortController,
170
183
  customUserAgentDetails?: CustomUserAgentDetails,
@@ -179,7 +192,7 @@ export class InternalGraphQLAPIClass {
179
192
  defaultAuthMode,
180
193
  } = resolveConfig(amplify);
181
194
 
182
- const initialAuthMode = explicitAuthMode || defaultAuthMode || 'iam';
195
+ const initialAuthMode = authModeOverride || defaultAuthMode || 'iam';
183
196
  // identityPool is an alias for iam. TODO: remove 'iam' in v7
184
197
  const authMode =
185
198
  initialAuthMode === 'identityPool' ? 'iam' : initialAuthMode;
@@ -205,7 +218,7 @@ export class InternalGraphQLAPIClass {
205
218
  const requestOptions: RequestOptions = {
206
219
  method: 'POST',
207
220
  url: new AmplifyUrl(
208
- customEndpoint || appSyncGraphqlEndpoint || '',
221
+ endpointOverride || customEndpoint || appSyncGraphqlEndpoint || '',
209
222
  ).toString(),
210
223
  queryString: print(query as DocumentNode),
211
224
  };
@@ -226,7 +239,7 @@ export class InternalGraphQLAPIClass {
226
239
  const authHeaders = await headerBasedAuth(
227
240
  amplify,
228
241
  authMode,
229
- apiKey,
242
+ apiKeyOverride ?? apiKey,
230
243
  additionalCustomHeaders,
231
244
  );
232
245
 
@@ -282,7 +295,8 @@ export class InternalGraphQLAPIClass {
282
295
  };
283
296
  }
284
297
 
285
- const endpoint = customEndpoint || appSyncGraphqlEndpoint;
298
+ const endpoint =
299
+ endpointOverride || customEndpoint || appSyncGraphqlEndpoint;
286
300
 
287
301
  if (!endpoint) {
288
302
  throw createGraphQLResultWithError<T>(new GraphQLApiError(NO_ENDPOINT));
@@ -341,7 +355,13 @@ export class InternalGraphQLAPIClass {
341
355
 
342
356
  private _graphqlSubscribe(
343
357
  amplify: AmplifyClassV6,
344
- { query, variables, authMode: explicitAuthMode }: GraphQLOptions,
358
+ {
359
+ query,
360
+ variables,
361
+ authMode: authModeOverride,
362
+ apiKey: apiKeyOverride,
363
+ endpoint,
364
+ }: GraphQLOptions,
345
365
  additionalHeaders: CustomHeaders = {},
346
366
  customUserAgentDetails?: CustomUserAgentDetails,
347
367
  authToken?: string,
@@ -349,7 +369,7 @@ export class InternalGraphQLAPIClass {
349
369
  const config = resolveConfig(amplify);
350
370
 
351
371
  const initialAuthMode =
352
- explicitAuthMode || config?.defaultAuthMode || 'iam';
372
+ authModeOverride || config?.defaultAuthMode || 'iam';
353
373
  // identityPool is an alias for iam. TODO: remove 'iam' in v7
354
374
  const authMode =
355
375
  initialAuthMode === 'identityPool' ? 'iam' : initialAuthMode;
@@ -364,15 +384,26 @@ export class InternalGraphQLAPIClass {
364
384
  */
365
385
  const { headers: libraryConfigHeaders } = resolveLibraryOptions(amplify);
366
386
 
367
- return this.appSyncRealTime
387
+ const appSyncGraphqlEndpoint = endpoint ?? config?.endpoint;
388
+
389
+ // TODO: This could probably be an exception. But, lots of tests rely on
390
+ // attempting to connect to nowhere. So, I'm treating as the opposite of
391
+ // a Chesterton's fence for now. (A fence I shouldn't build, because I don't
392
+ // know why somethings depends on its absence!)
393
+ const memoKey = appSyncGraphqlEndpoint ?? 'none';
394
+ const realtimeProvider =
395
+ this.appSyncRealTime.get(memoKey) ?? new AWSAppSyncRealTimeProvider();
396
+ this.appSyncRealTime.set(memoKey, realtimeProvider);
397
+
398
+ return realtimeProvider
368
399
  .subscribe(
369
400
  {
370
401
  query: print(query as DocumentNode),
371
402
  variables,
372
- appSyncGraphqlEndpoint: config?.endpoint,
403
+ appSyncGraphqlEndpoint,
373
404
  region: config?.region,
374
405
  authenticationType: authMode,
375
- apiKey: config?.apiKey,
406
+ apiKey: apiKeyOverride ?? config?.apiKey,
376
407
  additionalHeaders,
377
408
  authToken,
378
409
  libraryConfigHeaders,
@@ -13,8 +13,10 @@ import {
13
13
  import {
14
14
  V6Client,
15
15
  __amplify,
16
+ __apiKey,
16
17
  __authMode,
17
18
  __authToken,
19
+ __endpoint,
18
20
  __headers,
19
21
  getInternals,
20
22
  } from '../types';
@@ -33,13 +35,16 @@ import { ClientGenerationParams } from './types';
33
35
  * @param params
34
36
  * @returns
35
37
  */
36
- export function generateClient<T extends Record<any, any> = never>(
37
- params: ClientGenerationParams,
38
- ): V6Client<T> {
38
+ export function generateClient<
39
+ T extends Record<any, any> = never,
40
+ Options extends ClientGenerationParams = ClientGenerationParams,
41
+ >(params: Options): V6Client<T, Options> {
39
42
  const client = {
40
43
  [__amplify]: params.amplify,
41
44
  [__authMode]: params.authMode,
42
45
  [__authToken]: params.authToken,
46
+ [__apiKey]: 'apiKey' in params ? params.apiKey : undefined,
47
+ [__endpoint]: 'endpoint' in params ? params.endpoint : undefined,
43
48
  [__headers]: params.headers,
44
49
  graphql,
45
50
  cancel,
@@ -53,22 +58,37 @@ export function generateClient<T extends Record<any, any> = never>(
53
58
 
54
59
  const apiGraphqlConfig = params.amplify.getConfig().API?.GraphQL;
55
60
 
56
- if (isApiGraphQLConfig(apiGraphqlConfig)) {
57
- addSchemaToClient(client, apiGraphqlConfig, getInternals);
58
- } else {
59
- // This happens when the `Amplify.configure()` call gets evaluated after the `generateClient()` call.
60
- //
61
- // Cause: when the `generateClient()` and the `Amplify.configure()` calls are located in
62
- // different source files, script bundlers may randomly arrange their orders in the production
63
- // bundle.
64
- //
65
- // With the current implementation, the `client.models` instance created by `generateClient()`
66
- // will be rebuilt on every `Amplify.configure()` call that's provided with a valid GraphQL
67
- // provider configuration.
68
- //
69
- // TODO: revisit, and reverify this approach when enabling multiple clients for multi-endpoints
70
- // configuration.
71
- generateModelsPropertyOnAmplifyConfigure(client);
61
+ if (client[__endpoint]) {
62
+ if (!client[__authMode]) {
63
+ throw new Error(
64
+ 'generateClient() requires an explicit `authMode` when `endpoint` is provided.',
65
+ );
66
+ }
67
+ if (client[__authMode] === 'apiKey' && !client[__apiKey]) {
68
+ throw new Error(
69
+ "generateClient() requires an explicit `apiKey` when `endpoint` is provided and `authMode = 'apiKey'`.",
70
+ );
71
+ }
72
+ }
73
+
74
+ if (!client[__endpoint]) {
75
+ if (isApiGraphQLConfig(apiGraphqlConfig)) {
76
+ addSchemaToClient(client, apiGraphqlConfig, getInternals);
77
+ } else {
78
+ // This happens when the `Amplify.configure()` call gets evaluated after the `generateClient()` call.
79
+ //
80
+ // Cause: when the `generateClient()` and the `Amplify.configure()` calls are located in
81
+ // different source files, script bundlers may randomly arrange their orders in the production
82
+ // bundle.
83
+ //
84
+ // With the current implementation, the `client.models` instance created by `generateClient()`
85
+ // will be rebuilt on every `Amplify.configure()` call that's provided with a valid GraphQL
86
+ // provider configuration.
87
+ //
88
+ // TODO: revisit, and reverify this approach when enabling multiple clients for multi-endpoints
89
+ // configuration.
90
+ generateModelsPropertyOnAmplifyConfigure(client);
91
+ }
72
92
  }
73
93
 
74
94
  return client as any;
@@ -7,3 +7,4 @@ export {
7
7
 
8
8
  export { graphql, cancel, isCancelError } from './v6';
9
9
  export { generateClient } from './generateClient';
10
+ export { CommonPublicClientOptions, DefaultCommonClientOptions } from './types';
@@ -9,8 +9,10 @@ import {
9
9
  V6ClientSSRCookies,
10
10
  V6ClientSSRRequest,
11
11
  __amplify,
12
+ __apiKey,
12
13
  __authMode,
13
14
  __authToken,
15
+ __endpoint,
14
16
  __headers,
15
17
  getInternals,
16
18
  } from '../../types';
@@ -40,6 +42,8 @@ export function generateClientWithAmplifyInstance<
40
42
  [__amplify]: params.amplify,
41
43
  [__authMode]: params.authMode,
42
44
  [__authToken]: params.authToken,
45
+ [__apiKey]: 'apiKey' in params ? params.apiKey : undefined,
46
+ [__endpoint]: 'endpoint' in params ? params.endpoint : undefined,
43
47
  [__headers]: params.headers,
44
48
  graphql,
45
49
  cancel,
@@ -48,7 +52,20 @@ export function generateClientWithAmplifyInstance<
48
52
 
49
53
  const apiGraphqlConfig = params.config?.API?.GraphQL;
50
54
 
51
- if (isApiGraphQLConfig(apiGraphqlConfig)) {
55
+ if (client[__endpoint]) {
56
+ if (!client[__authMode]) {
57
+ throw new Error(
58
+ 'generateClient() requires an explicit `authMode` when `endpoint` is provided.',
59
+ );
60
+ }
61
+ if (client[__authMode] === 'apiKey' && !client[__apiKey]) {
62
+ throw new Error(
63
+ "generateClient() requires an explicit `apiKey` when `endpoint` is provided and `authMode = 'apiKey'`.",
64
+ );
65
+ }
66
+ }
67
+
68
+ if (!client[__endpoint] && isApiGraphQLConfig(apiGraphqlConfig)) {
52
69
  addSchemaToClientWithInstance<T>(client, params, getInternals);
53
70
  }
54
71
 
@@ -13,11 +13,30 @@ export type ClientGenerationParams = {
13
13
  amplify: AmplifyClassV6;
14
14
  } & CommonPublicClientOptions;
15
15
 
16
- /**
17
- * Common options that can be used on public `generateClient()` interfaces.
18
- */
19
- export interface CommonPublicClientOptions {
16
+ export interface DefaultCommonClientOptions {
17
+ endpoint?: never;
20
18
  authMode?: GraphQLAuthMode;
19
+ apiKey?: string;
21
20
  authToken?: string;
22
21
  headers?: CustomHeaders;
23
22
  }
23
+
24
+ /**
25
+ * Common options that can be used on public `generateClient()` interfaces.
26
+ */
27
+ export type CommonPublicClientOptions =
28
+ | DefaultCommonClientOptions
29
+ | {
30
+ endpoint: string;
31
+ authMode: 'apiKey';
32
+ apiKey: string;
33
+ authToken?: string;
34
+ headers?: CustomHeaders;
35
+ }
36
+ | {
37
+ endpoint: string;
38
+ authMode: Exclude<GraphQLAuthMode, 'apiKey'>;
39
+ apiKey?: string;
40
+ authToken?: string;
41
+ headers?: CustomHeaders;
42
+ };
@@ -4,6 +4,7 @@ import { CustomHeaders } from '@aws-amplify/data-schema/runtime';
4
4
 
5
5
  import { GraphQLAPI } from '../GraphQLAPI';
6
6
  import {
7
+ CommonPublicClientOptions,
7
8
  GraphQLOptions,
8
9
  GraphQLOptionsV6,
9
10
  GraphQLResponseV6,
@@ -98,15 +99,51 @@ import {
98
99
  export function graphql<
99
100
  FALLBACK_TYPES = unknown,
100
101
  TYPED_GQL_STRING extends string = string,
102
+ Options extends CommonPublicClientOptions = object,
101
103
  >(
102
104
  this: V6Client,
103
- options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>,
105
+ options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING, Options>,
104
106
  additionalHeaders?: CustomHeaders,
105
107
  ): GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING> {
106
108
  // inject client-level auth
107
- const internals = getInternals(this as any);
108
- options.authMode = options.authMode || internals.authMode;
109
+ const internals = getInternals(this);
110
+
111
+ /**
112
+ * The custom `endpoint` specific to the client
113
+ */
114
+ const clientEndpoint = internals.endpoint;
115
+
116
+ /**
117
+ * The `authMode` specific to the client.
118
+ */
119
+ const clientAuthMode = internals.authMode;
120
+
121
+ /**
122
+ * The `apiKey` specific to the client.
123
+ */
124
+ const clientApiKey = internals.apiKey;
125
+
126
+ /**
127
+ * The most specific `authMode` wins. Setting an `endpoint` value without also
128
+ * setting an `authMode` value is invalid. This helps to prevent customers apps
129
+ * from unexpectedly sending auth details to endpoints the auth details do not
130
+ * belong to.
131
+ *
132
+ * This is especially pronounced for `apiKey`. When both an `endpoint` *and*
133
+ * `authMode: 'apiKey'` are provided, an explicit `apiKey` override is required
134
+ * to prevent inadvertent sending of an API's `apiKey` to an endpoint is does
135
+ * not belong to.
136
+ */
137
+ options.authMode = options.authMode || clientAuthMode;
138
+ options.apiKey = options.apiKey ?? clientApiKey;
109
139
  options.authToken = options.authToken || internals.authToken;
140
+
141
+ if (clientEndpoint && options.authMode === 'apiKey' && !options.apiKey) {
142
+ throw new Error(
143
+ "graphql() requires an explicit `apiKey` for a custom `endpoint` when `authMode = 'apiKey'`.",
144
+ );
145
+ }
146
+
110
147
  const headers = additionalHeaders || internals.headers;
111
148
 
112
149
  /**
@@ -114,11 +151,13 @@ export function graphql<
114
151
  * Neither of these can actually be validated at runtime. Hence, we don't perform
115
152
  * any validation or type-guarding here.
116
153
  */
117
-
118
154
  const result = GraphQLAPI.graphql(
119
155
  // TODO: move V6Client back into this package?
120
156
  internals.amplify as any,
121
- options as GraphQLOptions,
157
+ {
158
+ ...options,
159
+ endpoint: clientEndpoint,
160
+ } as GraphQLOptions,
122
161
  headers,
123
162
  );
124
163
 
@@ -5,6 +5,7 @@ import {
5
5
  AmplifyServer,
6
6
  getAmplifyServerContext,
7
7
  } from '@aws-amplify/core/internals/adapter-core';
8
+ import { ResourcesConfig } from '@aws-amplify/core';
8
9
  import { CustomHeaders } from '@aws-amplify/data-schema/runtime';
9
10
 
10
11
  import { generateClientWithAmplifyInstance } from '../internals/server';
@@ -33,38 +34,35 @@ import {
33
34
  * }),
34
35
  * });
35
36
  */
36
- export function generateClient<T extends Record<any, any> = never>({
37
- config,
38
- authMode,
39
- authToken,
40
- }: GenerateServerClientParams): V6ClientSSRRequest<T> {
37
+ export function generateClient<
38
+ T extends Record<any, any> = never,
39
+ Options extends GenerateServerClientParams = { config: ResourcesConfig },
40
+ >(options: Options): V6ClientSSRRequest<T, Options> {
41
41
  // passing `null` instance because each (future model) method must retrieve a valid instance
42
42
  // from server context
43
43
  const client = generateClientWithAmplifyInstance<T, V6ClientSSRRequest<T>>({
44
44
  amplify: null,
45
- config,
46
- authMode,
47
- authToken,
45
+ ...options,
48
46
  });
49
47
 
50
48
  // TODO: improve this and the next type
51
- const prevGraphql = client.graphql as unknown as GraphQLMethod;
49
+ const prevGraphql = client.graphql as unknown as GraphQLMethod<Options>;
52
50
 
53
51
  const wrappedGraphql = (
54
52
  contextSpec: AmplifyServer.ContextSpec,
55
- options: GraphQLOptionsV6,
53
+ innerOptions: GraphQLOptionsV6<unknown, string, Options>,
56
54
  additionalHeaders?: CustomHeaders,
57
55
  ) => {
58
56
  const amplifyInstance = getAmplifyServerContext(contextSpec).amplify;
59
57
 
60
58
  return prevGraphql.call(
61
59
  { [__amplify]: amplifyInstance },
62
- options,
63
- additionalHeaders as any,
60
+ innerOptions,
61
+ additionalHeaders,
64
62
  );
65
63
  };
66
64
 
67
- client.graphql = wrappedGraphql as unknown as GraphQLMethodSSR;
65
+ client.graphql = wrappedGraphql as unknown as GraphQLMethodSSR<Options>;
68
66
 
69
67
  return client;
70
68
  }