Skip to content

Commit

Permalink
update 'parseAWSExports' util to support API config parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
david-mcafee committed Sep 11, 2023
1 parent 9929d33 commit f6b5b63
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 28 deletions.
3 changes: 3 additions & 0 deletions packages/api-graphql/src/internals/InternalGraphQLAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,9 +456,12 @@ export class InternalGraphQLAPIClass {
additionalHeaders = {},
customUserAgentDetails?: CustomUserAgentDetails
): Observable<any> {
debugger;
if (!this.appSyncRealTime) {
const { AppSync } = Amplify.getConfig().API ?? {};

debugger;

this.appSyncRealTime = new AWSAppSyncRealTimeProvider();

return this.appSyncRealTime.subscribe({
Expand Down
58 changes: 30 additions & 28 deletions packages/api/src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,42 +67,44 @@ export class APIClass extends InternalAPIClass {
models: {},
};

for (const model of Object.values(modelIntrospection.models)) {
const { name } = model as any;
if (modelIntrospection) {
for (const model of Object.values(modelIntrospection.models)) {
const { name } = model as any;

client.models[name] = {} as any;
client.models[name] = {} as any;

Object.entries(graphQLOperationsInfo).forEach(
([key, { operationPrefix }]) => {
const operation = key as ModelOperation;
Object.entries(graphQLOperationsInfo).forEach(
([key, { operationPrefix }]) => {
const operation = key as ModelOperation;

// e.g. clients.models.Todo.update
client.models[name][operationPrefix] = async (arg?: any) => {
const query = generateGraphQLDocument(model, operation);
const variables = buildGraphQLVariables(model, operation, arg);
// e.g. clients.models.Todo.update
client.models[name][operationPrefix] = async (arg?: any) => {
const query = generateGraphQLDocument(model, operation);
const variables = buildGraphQLVariables(model, operation, arg);

const res = (await this.graphql({
query,
// TODO V6
// @ts-ignore
variables,
})) as any;
const res = (await this.graphql({
query,
// TODO V6
// @ts-ignore
variables,
})) as any;

// flatten response
if (res.data !== undefined) {
const [key] = Object.keys(res.data);
// flatten response
if (res.data !== undefined) {
const [key] = Object.keys(res.data);

if (res.data[key].items) {
return res.data[key].items;
}
if (res.data[key].items) {
return res.data[key].items;
}

return res.data[key];
}
return res.data[key];
}

return res;
};
}
);
return res;
};
}
);
}
}

return client as V6Client<T>;
Expand Down
1 change: 1 addition & 0 deletions packages/core/__tests__/parseAWSExports.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parseAWSExports } from '../src/parseAWSExports';

// TODO: Add API category tests
describe('Parser', () => {
test('aws_mobile_analytics_app_id', () => {
const appId = 'app-id';
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/parseAWSExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@ import { ResourcesConfig } from './singleton/types';
*
* @returns A {@link ResourcesConfig} object.
*/

export const parseAWSExports = (
config: Record<string, any> = {}
): ResourcesConfig => {
const {
aws_appsync_apiKey,
aws_appsync_authenticationType,
aws_appsync_graphqlEndpoint,
aws_appsync_region,
aws_cognito_identity_pool_id,
// TODO - which category does this fall under?
// aws_cognito_region,
aws_cognito_sign_up_verification_method,
aws_mandatory_sign_in,
aws_mobile_analytics_app_id,
aws_mobile_analytics_app_region,
// TODO - which category does this fall under?
// aws_project_region,
aws_user_files_s3_bucket,
aws_user_files_s3_bucket_region,
aws_user_files_s3_dangerously_connect_to_http_endpoint_for_testing,
Expand All @@ -40,6 +49,18 @@ export const parseAWSExports = (
};
}

// API
if (aws_appsync_graphqlEndpoint) {
amplifyConfig.API = {
AppSync: {
apiKey: aws_appsync_apiKey,
defaultAuthMode: aws_appsync_authenticationType,
endpoint: aws_appsync_graphqlEndpoint,
region: aws_appsync_region,
},
};
}

// Auth
if (aws_cognito_identity_pool_id || aws_user_pools_id) {
amplifyConfig.Auth = {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/singleton/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type APIConfig = {
defaultAuthMode?: GraphQLAuthMode;
region?: string;
endpoint?: string;
apiKey: string;
// TODO: switch this when dependency is added:
// modelIntrospectionSchema: InternalModelIntrospectionSchema;
modelIntrospectionSchema?: any;
Expand Down

0 comments on commit f6b5b63

Please sign in to comment.