Skip to content

Commit

Permalink
chore(api-graphql): run yarn lint:fix
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Mar 8, 2024
1 parent 362007e commit e40292d
Show file tree
Hide file tree
Showing 31 changed files with 264 additions and 211 deletions.
7 changes: 4 additions & 3 deletions packages/api-graphql/src/GraphQLAPI.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { AmplifyClassV6 } from '@aws-amplify/core';
import { Category, ApiAction } from '@aws-amplify/core/internals/utils';
import { GraphQLOptions, GraphQLResult } from './types';
import { InternalGraphQLAPIClass } from './internals/InternalGraphQLAPI';
import { ApiAction, Category } from '@aws-amplify/core/internals/utils';
import { CustomHeaders } from '@aws-amplify/data-schema-types';
import { Observable } from 'rxjs';

import { GraphQLOptions, GraphQLResult } from './types';
import { InternalGraphQLAPIClass } from './internals/InternalGraphQLAPI';

export const graphqlOperation = (
query: any,
variables = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
import { Observable, SubscriptionLike } from 'rxjs';
import { GraphQLError } from 'graphql';
import {
Hub,
fetchAuthSession,
ConsoleLogger,
Hub,
HubPayload,
fetchAuthSession,
} from '@aws-amplify/core';
import { signRequest } from '@aws-amplify/core/internals/aws-client-utils';
import {
base64Encoder,
GraphQLAuthMode,
AmplifyUrl,
CustomUserAgentDetails,
DocumentType,
GraphQLAuthMode,
NonRetryableError,
USER_AGENT_HEADER,
amplifyUuid,
base64Encoder,
getAmplifyUserAgent,
isNonRetryableError,
jitteredExponentialRetry,
DocumentType,
amplifyUuid,
AmplifyUrl,
} from '@aws-amplify/core/internals/utils';
import { CustomHeaders, RequestOptions } from '@aws-amplify/data-schema-types';

import {
CONTROL_MSG,
Expand All @@ -32,64 +33,63 @@ import {
AMPLIFY_SYMBOL,
AWS_APPSYNC_REALTIME_HEADERS,
CONNECTION_INIT_TIMEOUT,
DEFAULT_KEEP_ALIVE_TIMEOUT,
CONNECTION_STATE_CHANGE,
DEFAULT_KEEP_ALIVE_ALERT_TIMEOUT,
DEFAULT_KEEP_ALIVE_TIMEOUT,
MAX_DELAY_MS,
MESSAGE_TYPES,
NON_RETRYABLE_CODES,
SOCKET_STATUS,
START_ACK_TIMEOUT,
SUBSCRIPTION_STATUS,
CONNECTION_STATE_CHANGE,
} from '../constants';
import {
ConnectionStateMonitor,
CONNECTION_CHANGE,
ConnectionStateMonitor,
} from '../../utils/ConnectionStateMonitor';
import {
ReconnectEvent,
ReconnectionMonitor,
} from '../../utils/ReconnectionMonitor';
import { CustomHeaders, RequestOptions } from '@aws-amplify/data-schema-types';

const logger = new ConsoleLogger('AWSAppSyncRealTimeProvider');

const dispatchApiEvent = (payload: HubPayload) => {
Hub.dispatch('api', payload, 'PubSub', AMPLIFY_SYMBOL);
};

export type ObserverQuery = {
export interface ObserverQuery {
observer: PubSubContentObserver;
query: string;
variables: Record<string, DocumentType>;
subscriptionState: SUBSCRIPTION_STATUS;
subscriptionReadyCallback?: Function;
subscriptionFailedCallback?: Function;
startAckTimeoutId?: ReturnType<typeof setTimeout>;
};
}

const standardDomainPattern =
/^https:\/\/\w{26}\.appsync\-api\.\w{2}(?:(?:\-\w{2,})+)\-\d\.amazonaws.com(?:\.cn)?\/graphql$/i;

const customDomainPath = '/realtime';

type DataObject = {
interface DataObject {
data: Record<string, unknown>;
};
}

type DataPayload = {
interface DataPayload {
id: string;
payload: DataObject;
type: string;
};
}

type ParsedMessagePayload = {
interface ParsedMessagePayload {
type: string;
payload: {
connectionTimeoutMs: number;
errors?: [{ errorType: string; errorCode: number }];
};
};
}

export interface AWSAppSyncRealTimeProviderOptions {
appSyncGraphqlEndpoint?: string;
Expand All @@ -98,7 +98,7 @@ export interface AWSAppSyncRealTimeProviderOptions {
variables?: Record<string, DocumentType>;
apiKey?: string;
region?: string;
libraryConfigHeaders?: () => {} | (() => Promise<{}>);
libraryConfigHeaders?(): {} | (() => Promise<{}>);
additionalHeaders?: CustomHeaders;
additionalCustomHeaders?: Record<string, string>;
authToken?: string;
Expand All @@ -117,8 +117,8 @@ export class AWSAppSyncRealTimeProvider {
private keepAliveTimeoutId?: ReturnType<typeof setTimeout>;
private keepAliveTimeout = DEFAULT_KEEP_ALIVE_TIMEOUT;
private keepAliveAlertTimeoutId?: ReturnType<typeof setTimeout>;
private subscriptionObserverMap: Map<string, ObserverQuery> = new Map();
private promiseArray: Array<{ res: Function; rej: Function }> = [];
private subscriptionObserverMap = new Map<string, ObserverQuery>();
private promiseArray: { res: Function; rej: Function }[] = [];
private connectionState: ConnectionState | undefined;
private readonly connectionStateMonitor = new ConnectionStateMonitor();
private readonly reconnectionMonitor = new ReconnectionMonitor();
Expand Down Expand Up @@ -397,6 +397,7 @@ export class AWSAppSyncRealTimeProvider {
});
} catch (err: any) {
this._logStartSubscriptionError(subscriptionId, observer, err);

return;
}

Expand Down Expand Up @@ -526,6 +527,7 @@ export class AWSAppSyncRealTimeProvider {

if (!this.awsRealTimeSocket) {
this.socketStatus = SOCKET_STATUS.CLOSED;

return;
}

Expand Down Expand Up @@ -582,6 +584,7 @@ export class AWSAppSyncRealTimeProvider {
} else {
logger.debug(`observer not found for id: ${id}`);
}

return;
}

Expand Down Expand Up @@ -621,14 +624,14 @@ export class AWSAppSyncRealTimeProvider {
if (this.keepAliveTimeoutId) clearTimeout(this.keepAliveTimeoutId);
if (this.keepAliveAlertTimeoutId)
clearTimeout(this.keepAliveAlertTimeoutId);
this.keepAliveTimeoutId = setTimeout(
() => this._errorDisconnect(CONTROL_MSG.TIMEOUT_DISCONNECT),
this.keepAliveTimeout,
);
this.keepAliveTimeoutId = setTimeout(() => {
this._errorDisconnect(CONTROL_MSG.TIMEOUT_DISCONNECT);
}, this.keepAliveTimeout);
this.keepAliveAlertTimeoutId = setTimeout(() => {
this.connectionStateMonitor.record(CONNECTION_CHANGE.KEEP_ALIVE_MISSED);
}, DEFAULT_KEEP_ALIVE_ALERT_TIMEOUT);
this.connectionStateMonitor.record(CONNECTION_CHANGE.KEEP_ALIVE);

return;
}

Expand Down Expand Up @@ -712,6 +715,7 @@ export class AWSAppSyncRealTimeProvider {
if (this.socketStatus === SOCKET_STATUS.READY) {
return;
}

return new Promise(async (res, rej) => {
this.promiseArray.push({ res, rej });

Expand Down Expand Up @@ -805,7 +809,8 @@ export class AWSAppSyncRealTimeProvider {
};
newSocket.onopen = () => {
this.awsRealTimeSocket = newSocket;
return res();

res();
};
});
})();
Expand Down Expand Up @@ -852,6 +857,7 @@ export class AWSAppSyncRealTimeProvider {
};
}
res('Cool, connected to AWS AppSyncRealTime');

return;
}

Expand Down Expand Up @@ -884,7 +890,9 @@ export class AWSAppSyncRealTimeProvider {
}
};

setTimeout(() => checkAckOk(ackOk), CONNECTION_INIT_TIMEOUT);
setTimeout(() => {
checkAckOk(ackOk);
}, CONNECTION_INIT_TIMEOUT);
}
});
})();
Expand Down Expand Up @@ -928,6 +936,7 @@ export class AWSAppSyncRealTimeProvider {

if (!authenticationType || !headerHandler[authenticationType]) {
logger.debug(`Authentication type ${authenticationType} not supported`);

return undefined;
} else {
const handler = headerHandler[authenticationType];
Expand Down Expand Up @@ -1012,6 +1021,7 @@ export class AWSAppSyncRealTimeProvider {
signingService: endpointInfo.service,
},
);

return signed_params.headers;
}

Expand All @@ -1024,7 +1034,7 @@ export class AWSAppSyncRealTimeProvider {
* the headers that are returned by that function will already have been
* provided before this function is called.
*/
if (!additionalCustomHeaders?.['Authorization']) {
if (!additionalCustomHeaders?.Authorization) {
throw new Error('No auth token specified');
}

Expand Down
31 changes: 22 additions & 9 deletions packages/api-graphql/src/internals/APIClient.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { resolveOwnerFields } from '../utils/resolveOwnerFields';
import {
AssociationBelongsTo,
AssociationHasOne,
GraphQLAuthMode,
ModelIntrospectionSchema,
ModelFieldType,
ModelIntrospectionSchema,
NonModelFieldType,
SchemaModel,
AssociationHasOne,
AssociationBelongsTo,
} from '@aws-amplify/core/internals/utils';
import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
import { CustomHeaders } from '@aws-amplify/data-schema-types';
import { SchemaNonModel } from '@aws-amplify/core/dist/esm/singleton/API/types';

import {
AuthModeParams,
ClientWithModels,
Expand All @@ -21,18 +24,17 @@ import {
__authToken,
__headers,
} from '../types';
import { AmplifyServer } from '@aws-amplify/core/internals/adapter-core';
import { CustomHeaders } from '@aws-amplify/data-schema-types';
import { SchemaNonModel } from '@aws-amplify/core/dist/esm/singleton/API/types';
import { resolveOwnerFields } from '../utils/resolveOwnerFields';

import type { IndexMeta } from './operations/indexQuery';

type LazyLoadOptions = {
interface LazyLoadOptions {
authMode?: GraphQLAuthMode;
authToken?: string | undefined;
limit?: number | undefined;
nextToken?: string | undefined | null;
headers?: CustomHeaders | undefined;
};
}

const connectionType = {
HAS_ONE: 'HAS_ONE',
Expand All @@ -54,9 +56,11 @@ export const flattenItems = (obj: Record<string, any>): Record<string, any> => {
res[prop] = value.items.map((item: Record<string, any>) =>
flattenItems(item),
);

return;
}
res[prop] = flattenItems(value);

return;
}

Expand Down Expand Up @@ -146,6 +150,7 @@ export function initializeModel(
},
);
}

return undefined;
};
} else {
Expand All @@ -166,6 +171,7 @@ export function initializeModel(
},
);
}

return undefined;
};
}
Expand Down Expand Up @@ -214,6 +220,7 @@ export function initializeModel(
authToken: options?.authToken || authToken,
});
}

return [];
};
} else {
Expand All @@ -231,6 +238,7 @@ export function initializeModel(
authToken: options?.authToken || authToken,
});
}

return [];
};
}
Expand Down Expand Up @@ -264,6 +272,7 @@ export function initializeModel(
authToken: options?.authToken || authToken,
});
}

return [];
};
} else {
Expand All @@ -281,6 +290,7 @@ export function initializeModel(
authToken: options?.authToken || authToken,
});
}

return [];
};
}
Expand Down Expand Up @@ -346,6 +356,7 @@ export function defaultSelectionSetForNonModelWithIR(
pair: (string | Record<string, unknown>)[] | undefined,
): pair is (string | Record<string, unknown>)[] => pair !== undefined,
);

return Object.fromEntries(mappedFields);
}

Expand Down Expand Up @@ -527,6 +538,7 @@ const modelsDefaultSelectionSetIR = (relatedModelDefinition: SchemaModel) => {
const reduced = defaultSelectionSet.reduce(
(acc: Record<string, any>, curVal) => {
acc[curVal] = FIELD_IR;

return acc;
},
{},
Expand Down Expand Up @@ -659,6 +671,7 @@ export function generateGraphQLDocument(
const skQueryArgs = sk.reduce((acc: Record<string, any>, fieldName) => {
const fieldType = fields[fieldName].type;
acc[fieldName] = `Model${fieldType}KeyConditionInput`;

return acc;
}, {});

Expand Down
Loading

0 comments on commit e40292d

Please sign in to comment.