@aws-amplify/api-graphql 4.0.1-console-preview.d807e55.0 → 4.0.1-console-preview.5f040a2.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.
@@ -1,3 +1,4 @@
1
+ import { AmplifyClassV6 } from '@aws-amplify/core';
1
2
  import { Source, DocumentNode, GraphQLError } from 'graphql';
2
3
  export { OperationTypeNode } from 'graphql';
3
4
  import { Observable } from 'rxjs';
@@ -271,3 +272,18 @@ export declare type GeneratedSubscription<InputType, OutputType> = string & {
271
272
  __generatedSubscriptionInput: InputType;
272
273
  __generatedSubscriptionOutput: OutputType;
273
274
  };
275
+ declare type FilteredKeys<T> = {
276
+ [P in keyof T]: T[P] extends never ? never : P;
277
+ }[keyof T];
278
+ declare type ExcludeNeverFields<O> = {
279
+ [K in FilteredKeys<O>]: O[K];
280
+ };
281
+ export declare const __amplify: unique symbol;
282
+ export declare type V6Client<T extends Record<any, any> = never> = ExcludeNeverFields<{
283
+ [__amplify]: AmplifyClassV6;
284
+ graphql: <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>, additionalHeaders?: {
285
+ [key: string]: string;
286
+ } | undefined) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
287
+ cancel: (promise: Promise<any>, message?: string) => boolean;
288
+ isCancelError: (error: any) => boolean;
289
+ }>;
@@ -7,3 +7,4 @@ export var GraphQLAuthError;
7
7
  GraphQLAuthError["NO_FEDERATED_JWT"] = "No federated jwt";
8
8
  GraphQLAuthError["NO_AUTH_TOKEN"] = "No auth token specified";
9
9
  })(GraphQLAuthError || (GraphQLAuthError = {}));
10
+ export var __amplify = Symbol('amplify');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/api-graphql",
3
- "version": "4.0.1-console-preview.d807e55.0+d807e55",
3
+ "version": "4.0.1-console-preview.5f040a2.0+5f040a2",
4
4
  "description": "Api-graphql category of aws-amplify",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib-esm/index.js",
@@ -47,9 +47,9 @@
47
47
  "internals"
48
48
  ],
49
49
  "dependencies": {
50
- "@aws-amplify/api-rest": "4.0.1-console-preview.d807e55.0+d807e55",
51
- "@aws-amplify/auth": "6.0.1-console-preview.d807e55.0+d807e55",
52
- "@aws-amplify/core": "6.0.1-console-preview.d807e55.0+d807e55",
50
+ "@aws-amplify/api-rest": "4.0.1-console-preview.5f040a2.0+5f040a2",
51
+ "@aws-amplify/auth": "6.0.1-console-preview.5f040a2.0+5f040a2",
52
+ "@aws-amplify/core": "6.0.1-console-preview.5f040a2.0+5f040a2",
53
53
  "@aws-sdk/types": "3.387.0",
54
54
  "graphql": "15.8.0",
55
55
  "rxjs": "^7.8.1",
@@ -122,5 +122,5 @@
122
122
  "<rootDir>/__tests__"
123
123
  ]
124
124
  },
125
- "gitHead": "d807e554bd4c36e287ab3b4a229260fc9b851d40"
125
+ "gitHead": "5f040a29ecebf29374ae9b58abc661f45143ac4f"
126
126
  }
package/src/GraphQLAPI.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+ import { AmplifyClassV6 } from '@aws-amplify/core';
3
4
  import { GraphQLOptions, GraphQLResult } from './types';
4
5
  import { InternalGraphQLAPIClass } from './internals';
5
6
  import { Observable } from 'rxjs';
@@ -30,10 +31,11 @@ export class GraphQLAPIClass extends InternalGraphQLAPIClass {
30
31
  * @returns An Observable if the query is a subscription query, else a promise of the graphql result.
31
32
  */
32
33
  graphql<T = any>(
34
+ amplify: AmplifyClassV6,
33
35
  options: GraphQLOptions,
34
36
  additionalHeaders?: { [key: string]: string }
35
37
  ): Observable<GraphQLResult<T>> | Promise<GraphQLResult<T>> {
36
- return super.graphql(options, additionalHeaders);
38
+ return super.graphql(amplify, options, additionalHeaders);
37
39
  }
38
40
 
39
41
  /**
@@ -9,7 +9,7 @@ import {
9
9
  OperationTypeNode,
10
10
  } from 'graphql';
11
11
  import { Observable } from 'rxjs';
12
- import { Amplify, Cache, fetchAuthSession } from '@aws-amplify/core';
12
+ import { AmplifyClassV6 } from '@aws-amplify/core';
13
13
  import {
14
14
  APIAuthMode,
15
15
  CustomUserAgentDetails,
@@ -54,7 +54,6 @@ export class InternalGraphQLAPIClass {
54
54
  private _options;
55
55
  private appSyncRealTime: AWSAppSyncRealTimeProvider | null;
56
56
 
57
- Cache = Cache;
58
57
  private _api = { post, updateRequestToBeCancellable };
59
58
 
60
59
  /**
@@ -71,10 +70,11 @@ export class InternalGraphQLAPIClass {
71
70
  }
72
71
 
73
72
  private async _headerBasedAuth(
73
+ amplify: AmplifyClassV6,
74
74
  authMode: APIAuthMode,
75
75
  additionalHeaders: { [key: string]: string } = {}
76
76
  ) {
77
- const config = Amplify.getConfig();
77
+ const config = amplify.getConfig();
78
78
  const {
79
79
  region: region,
80
80
  endpoint: appSyncGraphqlEndpoint,
@@ -95,7 +95,7 @@ export class InternalGraphQLAPIClass {
95
95
  };
96
96
  break;
97
97
  case 'iam':
98
- const session = await fetchAuthSession();
98
+ const session = await amplify.Auth.fetchAuthSession();
99
99
  if (session.credentials === undefined) {
100
100
  throw new Error(GraphQLAuthError.NO_CREDENTIALS);
101
101
  }
@@ -105,7 +105,9 @@ export class InternalGraphQLAPIClass {
105
105
  try {
106
106
  let token;
107
107
 
108
- token = (await fetchAuthSession()).tokens?.accessToken.toString();
108
+ token = (
109
+ await amplify.Auth.fetchAuthSession()
110
+ ).tokens?.accessToken.toString();
109
111
 
110
112
  if (!token) {
111
113
  throw new Error(GraphQLAuthError.NO_FEDERATED_JWT);
@@ -158,6 +160,7 @@ export class InternalGraphQLAPIClass {
158
160
  * @returns An Observable if the query is a subscription query, else a promise of the graphql result.
159
161
  */
160
162
  graphql<T = any>(
163
+ amplify: AmplifyClassV6,
161
164
  { query: paramQuery, variables = {}, authMode, authToken }: GraphQLOptions,
162
165
  additionalHeaders?: { [key: string]: string },
163
166
  customUserAgentDetails?: CustomUserAgentDetails
@@ -185,6 +188,7 @@ export class InternalGraphQLAPIClass {
185
188
  case 'mutation':
186
189
  const abortController = new AbortController();
187
190
  const responsePromise = this._graphql<T>(
191
+ amplify,
188
192
  { query, variables, authMode },
189
193
  headers,
190
194
  abortController,
@@ -197,6 +201,7 @@ export class InternalGraphQLAPIClass {
197
201
  return responsePromise;
198
202
  case 'subscription':
199
203
  return this._graphqlSubscribe(
204
+ amplify,
200
205
  { query, variables, authMode },
201
206
  headers,
202
207
  customUserAgentDetails
@@ -207,12 +212,13 @@ export class InternalGraphQLAPIClass {
207
212
  }
208
213
 
209
214
  private async _graphql<T = any>(
215
+ amplify: AmplifyClassV6,
210
216
  { query, variables, authMode }: GraphQLOptions,
211
217
  additionalHeaders = {},
212
218
  abortController: AbortController,
213
219
  customUserAgentDetails?: CustomUserAgentDetails
214
220
  ): Promise<GraphQLResult<T>> {
215
- const config = Amplify.getConfig();
221
+ const config = amplify.getConfig();
216
222
 
217
223
  const { region: region, endpoint: appSyncGraphqlEndpoint } =
218
224
  config.API?.GraphQL || {};
@@ -223,10 +229,10 @@ export class InternalGraphQLAPIClass {
223
229
  // TODO: Figure what we need to do to remove `!`'s.
224
230
  const headers = {
225
231
  ...(!customGraphqlEndpoint &&
226
- (await this._headerBasedAuth(authMode!, additionalHeaders))),
232
+ (await this._headerBasedAuth(amplify, authMode!, additionalHeaders))),
227
233
  ...((customGraphqlEndpoint &&
228
234
  (customEndpointRegion
229
- ? await this._headerBasedAuth(authMode!, additionalHeaders)
235
+ ? await this._headerBasedAuth(amplify, authMode!, additionalHeaders)
230
236
  : { Authorization: null })) ||
231
237
  {}),
232
238
  ...additionalHeaders,
@@ -311,11 +317,12 @@ export class InternalGraphQLAPIClass {
311
317
  }
312
318
 
313
319
  private _graphqlSubscribe(
320
+ amplify: AmplifyClassV6,
314
321
  { query, variables, authMode }: GraphQLOptions,
315
322
  additionalHeaders = {},
316
323
  customUserAgentDetails?: CustomUserAgentDetails
317
324
  ): Observable<any> {
318
- const { GraphQL } = Amplify.getConfig().API ?? {};
325
+ const { GraphQL } = amplify.getConfig().API ?? {};
319
326
  if (!this.appSyncRealTime) {
320
327
  this.appSyncRealTime = new AWSAppSyncRealTimeProvider();
321
328
  }
@@ -1,7 +1,12 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { GraphQLAPI } from '../GraphQLAPI';
4
- import { GraphQLOptionsV6, GraphQLResponseV6 } from '../types';
4
+ import {
5
+ __amplify,
6
+ V6Client,
7
+ GraphQLOptionsV6,
8
+ GraphQLResponseV6,
9
+ } from '../types';
5
10
 
6
11
  /**
7
12
  * Invokes graphql operations against a graphql service, providing correct input and
@@ -91,6 +96,7 @@ export function graphql<
91
96
  FALLBACK_TYPES = unknown,
92
97
  TYPED_GQL_STRING extends string = string
93
98
  >(
99
+ this: V6Client,
94
100
  options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>,
95
101
  additionalHeaders?: { [key: string]: string }
96
102
  ): GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING> {
@@ -99,7 +105,11 @@ export function graphql<
99
105
  * Neither of these can actually be validated at runtime. Hence, we don't perform
100
106
  * any validation or type-guarding here.
101
107
  */
102
- const result = GraphQLAPI.graphql(options, additionalHeaders);
108
+ const result = GraphQLAPI.graphql(
109
+ this[__amplify],
110
+ options,
111
+ additionalHeaders
112
+ );
103
113
  return result as any;
104
114
  }
105
115
 
@@ -108,7 +118,11 @@ export function graphql<
108
118
  * @param {any} request - request to cancel
109
119
  * @returns - A boolean indicating if the request was cancelled
110
120
  */
111
- export function cancel(promise: Promise<any>, message?: string): boolean {
121
+ export function cancel(
122
+ this: V6Client,
123
+ promise: Promise<any>,
124
+ message?: string
125
+ ): boolean {
112
126
  return GraphQLAPI.cancel(promise, message);
113
127
  }
114
128
 
@@ -117,7 +131,7 @@ export function cancel(promise: Promise<any>, message?: string): boolean {
117
131
  * @param {any} error - Any error
118
132
  * @returns - A boolean indicating if the error was from an api request cancellation
119
133
  */
120
- export function isCancelError(error: any): boolean {
134
+ export function isCancelError(this: V6Client, error: any): boolean {
121
135
  return GraphQLAPI.isCancelError(error);
122
136
  }
123
137
 
@@ -1,5 +1,6 @@
1
1
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+ import { AmplifyClassV6 } from '@aws-amplify/core';
3
4
  import { Source, DocumentNode, GraphQLError } from 'graphql';
4
5
  export { OperationTypeNode } from 'graphql';
5
6
  import { Observable } from 'rxjs';
@@ -339,3 +340,26 @@ export type GeneratedSubscription<InputType, OutputType> = string & {
339
340
  __generatedSubscriptionInput: InputType;
340
341
  __generatedSubscriptionOutput: OutputType;
341
342
  };
343
+
344
+ type FilteredKeys<T> = {
345
+ [P in keyof T]: T[P] extends never ? never : P;
346
+ }[keyof T];
347
+ type ExcludeNeverFields<O> = {
348
+ [K in FilteredKeys<O>]: O[K];
349
+ };
350
+
351
+ export const __amplify = Symbol('amplify');
352
+
353
+ export type V6Client<T extends Record<any, any> = never> = ExcludeNeverFields<{
354
+ [__amplify]: AmplifyClassV6;
355
+ graphql: <FALLBACK_TYPES = unknown, TYPED_GQL_STRING extends string = string>(
356
+ options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>,
357
+ additionalHeaders?:
358
+ | {
359
+ [key: string]: string;
360
+ }
361
+ | undefined
362
+ ) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;
363
+ cancel: (promise: Promise<any>, message?: string) => boolean;
364
+ isCancelError: (error: any) => boolean;
365
+ }>;