Skip to content

Commit

Permalink
reading config from amplifyConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
elorzafe committed Sep 20, 2023
1 parent f03aa4d commit 3352944
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 217 deletions.
6 changes: 3 additions & 3 deletions packages/api-graphql/src/internals/InternalGraphQLAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ export class InternalGraphQLAPIClass {
return this.appSyncRealTime.subscribe({
query: print(query as DocumentNode),
variables,
appSyncGraphqlEndpoint: AppSync.endpoint,
region: AppSync.region,
authenticationType: AppSync.defaultAuthMode,
appSyncGraphqlEndpoint: AppSync?.endpoint,
region: AppSync?.region,
authenticationType: AppSync?.defaultAuthMode,
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/singleton/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type APIConfig = {

export type GraphQLAuthMode =
| { type: 'apiKey'; apiKey: string }
| { type: 'jwt'; token: 'id' | 'access' }
| { type: 'jwt'; token?: 'id' | 'access' }
| { type: 'iam' }
| { type: 'lambda' }
| { type: 'custom' };
Expand Down
2 changes: 1 addition & 1 deletion packages/datastore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
"__tests__/connectivityHandling.test.ts",
"__tests__/AsyncStorageAdapter.test.ts",
"__tests__/mutation.test.ts",
"__tests__/IndexedDBAdapter.test.ts"
"__tests__/indexeddb.test.ts"
],
"moduleFileExtensions": [
"ts",
Expand Down
30 changes: 24 additions & 6 deletions packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1524,8 +1524,7 @@ class DataStore {
checkSchemaInitialized();
await checkSchemaVersion(this.storage, schema.version);

const aws_appsync_graphqlEndpoint =
Amplify.getConfig().API?.AppSync?.endpoint;
const { aws_appsync_graphqlEndpoint } = this.amplifyConfig;

if (aws_appsync_graphqlEndpoint) {
logger.debug(
Expand Down Expand Up @@ -1577,7 +1576,7 @@ class DataStore {
logger.warn(
"Data won't be synchronized. No GraphQL endpoint configured. Did you forget `Amplify.configure(awsconfig)`?",
{
config: Amplify.getConfig().API,
config: this.amplifyConfig,
}
);

Expand Down Expand Up @@ -2446,9 +2445,24 @@ class DataStore {
...configFromAmplify
} = config;

let apiKey = '';
const currentAppSyncConfig = Amplify.getConfig().API?.AppSync;
if (currentAppSyncConfig?.defaultAuthMode.type === 'apiKey') {
apiKey = currentAppSyncConfig.defaultAuthMode.apiKey;
}

const appSyncConfig = {
aws_appsync_graphqlEndpoint: currentAppSyncConfig?.endpoint,
aws_appsync_authenticationType:
currentAppSyncConfig?.defaultAuthMode.type,
aws_appsync_region: currentAppSyncConfig?.region,
aws_appsync_apiKey: apiKey,
};

this.amplifyConfig = {
...configFromAmplify,
...this.amplifyConfig,
...configFromAmplify,
...(currentAppSyncConfig && appSyncConfig),
};

this.conflictHandler = this.setConflictHandler(config);
Expand Down Expand Up @@ -2726,8 +2740,7 @@ class DataStore {
const sessionId = sessionStorage.getItem('datastoreSessionId');

if (sessionId) {
const aws_appsync_graphqlEndpoint =
Amplify.getConfig().API?.AppSync?.endpoint || '';
const { aws_appsync_graphqlEndpoint } = this.amplifyConfig;

const appSyncUrl = aws_appsync_graphqlEndpoint.split('/')[2];
const [appSyncId] = appSyncUrl.split('.');
Expand All @@ -2742,5 +2755,10 @@ class DataStore {

const instance = new DataStore();
instance.configure({});
Hub.listen('core', capsule => {
if (capsule.payload.event === 'configure') {
instance.configure({});
}
});

export { DataStore as DataStoreClass, initSchema, instance as DataStore };
14 changes: 2 additions & 12 deletions packages/datastore/src/sync/processors/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import {
getTokenForCustomAuth,
} from '../utils';
import { getMutationErrorType } from './errorMaps';
import { Amplify } from '@aws-amplify/core';

const MAX_ATTEMPTS = 10;

Expand Down Expand Up @@ -193,20 +192,11 @@ class MutationProcessor {
let opName: string = undefined!;
let modelDefinition: SchemaModel = undefined!;

const appSyncConfig = Amplify.getConfig().API?.AppSync;

if (!appSyncConfig) {
throw new AmplifyError({
message: 'AppSync not configured',
name: 'APINotConfigured',
recoverySuggestion: 'Invoke Amplify.configure',
});
}

try {
const modelAuthModes = await getModelAuthModes({
authModeStrategy: this.authModeStrategy,
defaultAuthMode: appSyncConfig.defaultAuthMode.type,
defaultAuthMode:
this.amplifyConfig.aws_appsync_authenticationType,
modelName: model,
schema: this.schema,
});
Expand Down
Loading

0 comments on commit 3352944

Please sign in to comment.