@aws-amplify/api-graphql 2.2.18 → 2.2.19-custom-pk.79

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.
package/src/GraphQLAPI.ts CHANGED
@@ -16,19 +16,25 @@ import {
16
16
  print,
17
17
  parse,
18
18
  GraphQLError,
19
+ OperationTypeNode,
19
20
  } from 'graphql';
20
21
  import Observable from 'zen-observable-ts';
21
22
  import {
22
23
  Amplify,
23
24
  ConsoleLogger as Logger,
24
- Constants,
25
25
  Credentials,
26
+ getAmplifyUserAgent,
26
27
  INTERNAL_AWS_APPSYNC_REALTIME_PUBSUB_PROVIDER,
27
28
  } from '@aws-amplify/core';
28
29
  import PubSub from '@aws-amplify/pubsub';
29
30
  import Auth from '@aws-amplify/auth';
30
31
  import Cache from '@aws-amplify/cache';
31
- import { GraphQLAuthError, GraphQLOptions, GraphQLResult } from './types';
32
+ import {
33
+ GraphQLAuthError,
34
+ GraphQLOptions,
35
+ GraphQLResult,
36
+ GraphQLOperation,
37
+ } from './types';
32
38
  import { RestClient } from '@aws-amplify/api-rest';
33
39
  const USER_AGENT_HEADER = 'x-amz-user-agent';
34
40
 
@@ -37,11 +43,13 @@ const logger = new Logger('GraphQLAPI');
37
43
  export const graphqlOperation = (
38
44
  query,
39
45
  variables = {},
40
- authToken?: string
46
+ authToken?: string,
47
+ userAgentSuffix?: string
41
48
  ) => ({
42
49
  query,
43
50
  variables,
44
51
  authToken,
52
+ userAgentSuffix,
45
53
  });
46
54
 
47
55
  /**
@@ -124,10 +132,8 @@ export class GraphQLAPIClass {
124
132
  defaultAuthenticationType?,
125
133
  additionalHeaders: { [key: string]: string } = {}
126
134
  ) {
127
- const {
128
- aws_appsync_authenticationType,
129
- aws_appsync_apiKey: apiKey,
130
- } = this._options;
135
+ const { aws_appsync_authenticationType, aws_appsync_apiKey: apiKey } =
136
+ this._options;
131
137
  const authenticationType =
132
138
  defaultAuthenticationType || aws_appsync_authenticationType || 'AWS_IAM';
133
139
  let headers = {};
@@ -203,11 +209,10 @@ export class GraphQLAPIClass {
203
209
  * to get the operation type
204
210
  * @param operation
205
211
  */
206
- getGraphqlOperationType(operation) {
212
+ getGraphqlOperationType(operation: GraphQLOperation): OperationTypeNode {
207
213
  const doc = parse(operation);
208
- const definitions = doc.definitions as ReadonlyArray<
209
- OperationDefinitionNode
210
- >;
214
+ const definitions =
215
+ doc.definitions as ReadonlyArray<OperationDefinitionNode>;
211
216
  const [{ operation: operationType }] = definitions;
212
217
 
213
218
  return operationType;
@@ -216,14 +221,20 @@ export class GraphQLAPIClass {
216
221
  /**
217
222
  * Executes a GraphQL operation
218
223
  *
219
- * @param {GraphQLOptions} GraphQL Options
220
- * @param {object} additionalHeaders headers to merge in after any `graphql_headers` set in the config
221
- * @returns {Promise<GraphQLResult> | Observable<object>}
224
+ * @param options - GraphQL Options
225
+ * @param [additionalHeaders] - headers to merge in after any `graphql_headers` set in the config
226
+ * @returns An Observable if the query is a subscription query, else a promise of the graphql result.
222
227
  */
223
- graphql(
224
- { query: paramQuery, variables = {}, authMode, authToken }: GraphQLOptions,
228
+ graphql<T = any>(
229
+ {
230
+ query: paramQuery,
231
+ variables = {},
232
+ authMode,
233
+ authToken,
234
+ userAgentSuffix,
235
+ }: GraphQLOptions,
225
236
  additionalHeaders?: { [key: string]: string }
226
- ) {
237
+ ): Observable<GraphQLResult<T>> | Promise<GraphQLResult<T>> {
227
238
  const query =
228
239
  typeof paramQuery === 'string'
229
240
  ? parse(paramQuery)
@@ -232,9 +243,8 @@ export class GraphQLAPIClass {
232
243
  const [operationDef = {}] = query.definitions.filter(
233
244
  def => def.kind === 'OperationDefinition'
234
245
  );
235
- const {
236
- operation: operationType,
237
- } = operationDef as OperationDefinitionNode;
246
+ const { operation: operationType } =
247
+ operationDef as OperationDefinitionNode;
238
248
 
239
249
  const headers = additionalHeaders || {};
240
250
 
@@ -248,8 +258,8 @@ export class GraphQLAPIClass {
248
258
  case 'mutation':
249
259
  const cancellableToken = this._api.getCancellableToken();
250
260
  const initParams = { cancellableToken };
251
- const responsePromise = this._graphql(
252
- { query, variables, authMode },
261
+ const responsePromise = this._graphql<T>(
262
+ { query, variables, authMode, userAgentSuffix },
253
263
  headers,
254
264
  initParams
255
265
  );
@@ -260,16 +270,16 @@ export class GraphQLAPIClass {
260
270
  return responsePromise;
261
271
  case 'subscription':
262
272
  return this._graphqlSubscribe({ query, variables, authMode }, headers);
273
+ default:
274
+ throw new Error(`invalid operation type: ${operationType}`);
263
275
  }
264
-
265
- throw new Error(`invalid operation type: ${operationType}`);
266
276
  }
267
277
 
268
- private async _graphql(
269
- { query, variables, authMode }: GraphQLOptions,
278
+ private async _graphql<T = any>(
279
+ { query, variables, authMode, userAgentSuffix }: GraphQLOptions,
270
280
  additionalHeaders = {},
271
281
  initParams = {}
272
- ): Promise<GraphQLResult> {
282
+ ): Promise<GraphQLResult<T>> {
273
283
  if (!this._api) {
274
284
  await this.createInstance();
275
285
  }
@@ -292,7 +302,7 @@ export class GraphQLAPIClass {
292
302
  ...(await graphql_headers({ query, variables })),
293
303
  ...additionalHeaders,
294
304
  ...(!customGraphqlEndpoint && {
295
- [USER_AGENT_HEADER]: Constants.userAgent,
305
+ [USER_AGENT_HEADER]: getAmplifyUserAgent(userAgentSuffix),
296
306
  }),
297
307
  };
298
308
 
@@ -367,6 +377,15 @@ export class GraphQLAPIClass {
367
377
  return this._api.cancel(request, message);
368
378
  }
369
379
 
380
+ /**
381
+ * Check if the request has a corresponding cancel token in the WeakMap.
382
+ * @params request - The request promise
383
+ * @return if the request has a corresponding cancel token.
384
+ */
385
+ hasCancelToken(request: Promise<any>) {
386
+ return this._api.hasCancelToken(request);
387
+ }
388
+
370
389
  private _graphqlSubscribe(
371
390
  {
372
391
  query,
@@ -10,23 +10,17 @@
10
10
  * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
11
  * and limitations under the License.
12
12
  */
13
- // @ts-ignore
14
- import { GraphQLError } from 'graphql/error/GraphQLError';
15
- import { DocumentNode } from 'graphql/language/ast';
13
+ import { Source, DocumentNode, GraphQLError } from 'graphql';
14
+ export { OperationTypeNode } from 'graphql';
15
+ import { GRAPHQL_AUTH_MODE } from '@aws-amplify/auth';
16
+ export { GRAPHQL_AUTH_MODE };
16
17
 
17
18
  export interface GraphQLOptions {
18
19
  query: string | DocumentNode;
19
20
  variables?: object;
20
21
  authMode?: keyof typeof GRAPHQL_AUTH_MODE;
21
22
  authToken?: string;
22
- }
23
-
24
- export enum GRAPHQL_AUTH_MODE {
25
- API_KEY = 'API_KEY',
26
- AWS_IAM = 'AWS_IAM',
27
- OPENID_CONNECT = 'OPENID_CONNECT',
28
- AMAZON_COGNITO_USER_POOLS = 'AMAZON_COGNITO_USER_POOLS',
29
- AWS_LAMBDA = 'AWS_LAMBDA',
23
+ userAgentSuffix?: string;
30
24
  }
31
25
 
32
26
  export interface GraphQLResult<T = object> {
@@ -44,3 +38,9 @@ export enum GraphQLAuthError {
44
38
  NO_FEDERATED_JWT = 'No federated jwt',
45
39
  NO_AUTH_TOKEN = 'No auth token specified',
46
40
  }
41
+
42
+ /**
43
+ * GraphQLSource or string, the type of the parameter for calling graphql.parse
44
+ * @see: https://graphql.org/graphql-js/language/#parse
45
+ */
46
+ export type GraphQLOperation = Source | string;