Skip to content

Commit

Permalink
fix: export generateClient return types; don't throw error in genClie…
Browse files Browse the repository at this point in the history
…nt (#12577)
  • Loading branch information
iartemiev authored Nov 16, 2023
1 parent 407b52e commit c69c562
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 28 deletions.
5 changes: 5 additions & 0 deletions packages/adapter-nextjs/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ export {
generateServerClientUsingReqRes,
generateServerClientUsingCookies,
} from './generateServerClient';

export {
V6ClientSSRCookies as ClientUsingSSRCookies,
V6ClientSSRRequest as ClientUsingSSRReq,
} from '@aws-amplify/api-graphql';
18 changes: 9 additions & 9 deletions packages/api-graphql/__tests__/generateClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ const USER_AGENT_DETAILS = {
};

describe('generateClient', () => {
test('raises clear error when API GraphQL isnt configured', () => {
const getConfig = jest.fn().mockReturnValue({});
const amplify = {
getConfig,
} as unknown as AmplifyClassV6;
expect(() => generateClient({ amplify })).toThrow(
'The API configuration is missing. This is likely due to Amplify.configure() not being called prior to generateClient()'
);
});
// test('raises clear error when API GraphQL isnt configured', () => {
// const getConfig = jest.fn().mockReturnValue({});
// const amplify = {
// getConfig,
// } as unknown as AmplifyClassV6;
// expect(() => generateClient({ amplify })).toThrow(
// 'The API configuration is missing. This is likely due to Amplify.configure() not being called prior to generateClient()'
// );
// });

test('can produce a client bound to an arbitrary amplify object for getConfig()', async () => {
// TS lies: We don't care what `amplify` is or does. We want want to make sure
Expand Down
4 changes: 2 additions & 2 deletions packages/api-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
},
"homepage": "https://aws-amplify.github.io/",
"devDependencies": {
"@aws-amplify/data-schema": "^0.12.2",
"@aws-amplify/data-schema": "^0.12.6",
"@rollup/plugin-typescript": "11.1.5",
"rollup": "3.29.4",
"typescript": "5.0.2"
Expand All @@ -81,7 +81,7 @@
"dependencies": {
"@aws-amplify/api-rest": "4.0.2",
"@aws-amplify/core": "6.0.2",
"@aws-amplify/data-schema-types": "^0.6.2",
"@aws-amplify/data-schema-types": "^0.6.4",
"@aws-sdk/types": "3.387.0",
"graphql": "15.8.0",
"rxjs": "^7.8.1",
Expand Down
11 changes: 8 additions & 3 deletions packages/api-graphql/src/internals/generateModelsProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ export function generateModelsProperty<T extends Record<any, any> = never>(
const config = params.amplify.getConfig();

if (!config.API?.GraphQL) {
throw new Error(
'The API configuration is missing. This is likely due to Amplify.configure() not being called prior to generateClient().'
);
// breaks compatibility with certain bundler, e.g. Vite where component files are evaluated before
// the entry point causing false positive errors. Revisit how to better handle this post-launch

// throw new Error(
// 'The API configuration is missing. This is likely due to Amplify.configure() not being called
// prior to generateClient().'
// );
return {} as ModelTypes<never>;
}

const modelIntrospection: ModelIntrospectionSchema | undefined =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { listFactory } from '../operations/list';
import { getFactory } from '../operations/get';

export function generateModelsProperty<
T extends Record<any, any> = never,
_T extends Record<any, any> = never,
ClientType extends
| V6ClientSSRRequest<Record<string, any>>
| V6ClientSSRCookies<Record<string, any>> = V6ClientSSRCookies<
Expand All @@ -26,9 +26,7 @@ export function generateModelsProperty<
}

if (!config.API?.GraphQL) {
throw new Error(
'The API configuration is missing. This is likely due to Amplify.configure() not being called prior to generateClient().'
);
return {} as ModelTypes<never>;
}

const modelIntrospection: ModelIntrospectionSchema | undefined =
Expand Down
14 changes: 12 additions & 2 deletions packages/api-graphql/src/utils/resolveConfig.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { AmplifyClassV6 } from '@aws-amplify/core';
import { AmplifyClassV6, ConsoleLogger } from '@aws-amplify/core';
import { APIValidationErrorCode, assertValidationError } from './errors';

const logger = new ConsoleLogger('GraphQLAPI resovleConfig');

/**
* @internal
*/
export const resolveConfig = (amplify: AmplifyClassV6) => {
const config = amplify.getConfig();

if (!config.API?.GraphQL) {
logger.warn(
'The API configuration is missing. This is likely due to Amplify.configure() not being called prior to generateClient().'
);
}

const {
apiKey,
customEndpoint,
customEndpointRegion,
defaultAuthMode,
endpoint,
region,
} = amplify.getConfig().API?.GraphQL ?? {};
} = config.API?.GraphQL ?? {};

// TODO: re-enable when working in all test environments:
// assertValidationError(!!endpoint, APIValidationErrorCode.NoEndpoint);
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export type {
CONNECTION_STATE_CHANGE,
} from '@aws-amplify/api-graphql';

export type { V6Client as Client } from '@aws-amplify/api-graphql';

export {
get,
put,
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c69c562

Please sign in to comment.