Skip to content

Commit

Permalink
tested subscriptions for apikey
Browse files Browse the repository at this point in the history
  • Loading branch information
elorzafe committed Sep 5, 2023
1 parent f8d3a0f commit d03d711
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ConnectionState,
PubSubContent,
PubSubContentObserver,
} from '../../../../api-graphql/src/types/PubSub';
} from '../../types/PubSub';

import { signRequest } from '@aws-amplify/core/internals/aws-client-utils';

Expand Down Expand Up @@ -180,29 +180,22 @@ export class AWSAppSyncRealTimeProvider {
return 'AWSAppSyncRealTimeProvider';
}

newClient(): Promise<any> {
throw new Error('Not used here');
}

public async publish(
_topics: string[] | string,
_msg: PubSubContent,
_options?: AWSAppSyncRealTimeProviderOptions
) {
throw new Error('Operation not supported');
}

// Check if url matches standard domain pattern
private isCustomDomain(url: string): boolean {
return url.match(standardDomainPattern) === null;
}

subscribe(
_topics: string[] | string,
options?: AWSAppSyncRealTimeProviderOptions,
customUserAgentDetails?: CustomUserAgentDetails
): Observable<Record<string, unknown>> {
const appSyncGraphqlEndpoint = options?.appSyncGraphqlEndpoint;
const {
appSyncGraphqlEndpoint,
region,
query,
variables,
authenticationType,
} = options;

return new Observable(observer => {
if (!options || !appSyncGraphqlEndpoint) {
Expand All @@ -225,7 +218,13 @@ export class AWSAppSyncRealTimeProvider {

const startSubscriptionPromise =
this._startSubscriptionWithAWSAppSyncRealTime({
options,
options: {
query,
variables,
region,
authenticationType,
appSyncGraphqlEndpoint,
},
observer,
subscriptionId,
customUserAgentDetails,
Expand Down Expand Up @@ -877,20 +876,19 @@ export class AWSAppSyncRealTimeProvider {
payload,
canonicalUri,
appSyncGraphqlEndpoint,
apiKey,
region,
additionalHeaders,
}: AWSAppSyncRealTimeAuthInput): Promise<
Record<string, unknown> | undefined
> {
debugger;
const headerHandler: {
[key: string]: (AWSAppSyncRealTimeAuthInput) => {};
} = {
API_KEY: this._awsRealTimeApiKeyHeader.bind(this),
AWS_IAM: this._awsRealTimeIAMHeader.bind(this),
OPENID_CONNECT: this._awsRealTimeOPENIDHeader.bind(this),
AMAZON_COGNITO_USER_POOLS: this._awsRealTimeCUPHeader.bind(this),
AWS_LAMBDA: this._customAuthHeader,
apiKey: this._awsRealTimeApiKeyHeader.bind(this),
iam: this._awsRealTimeIAMHeader.bind(this),
jwt: this._awsRealTimeOPENIDHeader.bind(this),
custom: this._customAuthHeader,
};

if (!authenticationType || !headerHandler[authenticationType.type]) {
Expand All @@ -902,7 +900,10 @@ export class AWSAppSyncRealTimeProvider {
const { host } = url.parse(appSyncGraphqlEndpoint ?? '');

logger.debug(`Authenticating with ${authenticationType}`);

let apiKey;
if (authenticationType.type === 'apiKey') {
apiKey = authenticationType.apiKey;
}
const result = await handler({
payload,
canonicalUri,
Expand Down
2 changes: 1 addition & 1 deletion packages/api-graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// import { graphqlSubscription } from './GraphQLAPI';

// export { GraphQLResult, GraphQLAuthError, GRAPHQL_AUTH_MODE } from './types';
// export { GraphQLAPI, GraphQLAPIClass, graphqlOperation } from './GraphQLAPI';
export { GraphQLAPI, GraphQLAPIClass, graphqlOperation } from './GraphQLAPI';
// export * from './types';

// export { graphqlSubscription };
24 changes: 14 additions & 10 deletions packages/api-graphql/src/internals/InternalGraphQLAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
} from '../types';
// import { RestClient } from '@aws-amplify/api-rest';
import { post } from '@aws-amplify/api-rest';
import { AWSAppSyncRealTimeProvider } from '../Providers/AWSAppSyncRealTimeProvider';

const USER_AGENT_HEADER = 'x-amz-user-agent';

Expand All @@ -69,6 +70,7 @@ export class InternalGraphQLAPIClass {
*/
private _options;
private _api = null;
private appSyncRealTime: AWSAppSyncRealTimeProvider | null;

// TODO V6: can be removed
// InternalAuth = InternalAuth;
Expand Down Expand Up @@ -459,17 +461,19 @@ export class InternalGraphQLAPIClass {
additionalHeaders = {},
customUserAgentDetails?: CustomUserAgentDetails
): Observable<any> {
const {
aws_appsync_region: region,
aws_appsync_graphqlEndpoint: appSyncGraphqlEndpoint,
aws_appsync_authenticationType,
aws_appsync_apiKey: apiKey,
graphql_headers = () => ({}),
} = this._options;
const authenticationType =
defaultAuthenticationType || aws_appsync_authenticationType || 'AWS_IAM';
if (!this.appSyncRealTime) {
const { AppSync } = Amplify.getConfig().API ?? {};

this.appSyncRealTime = new AWSAppSyncRealTimeProvider();

throw new Error('not implemented yet');
return this.appSyncRealTime.subscribe({
query: print(query as DocumentNode),
variables,
appSyncGraphqlEndpoint: AppSync.endpoint,
region: AppSync.region,
authenticationType: AppSync.defaultAuthMode,
});
}
}
// if (InternalPubSub && typeof InternalPubSub.subscribe === 'function') {
// return InternalPubSub.subscribe(
Expand Down
2 changes: 1 addition & 1 deletion packages/api-graphql/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type GraphQLAuthMode = 'AWS_IAM' | 'COGNITO_USERPOOLS' | 'API_KEY';
*/
export interface GraphQLOptions {
query: string | DocumentNode;
variables?: object;
variables?: Record<string, unknown>;
authMode?: GraphQLAuthMode;
authToken?: string;
/**
Expand Down

0 comments on commit d03d711

Please sign in to comment.