-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api-graphql): add .enums property to the GQL client
- Loading branch information
Showing
9 changed files
with
181 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/api-graphql/__tests__/internals/utils/generateEnumsProperty.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { GraphQLProviderConfig } from '@aws-amplify/core/internals/utils'; | ||
import { generateEnumsProperty } from '../../../src/internals/utils/generateEnumsProperty'; | ||
|
||
describe('generateEnumsProperty()', () => { | ||
it('returns an empty object when there is no valid `modelIntrospection`', () => { | ||
const mockAPIGraphQLConfig: GraphQLProviderConfig['GraphQL'] = { | ||
endpoint: 'endpoint', | ||
defaultAuthMode: 'iam', | ||
}; | ||
const result = generateEnumsProperty(mockAPIGraphQLConfig); | ||
|
||
expect(Object.keys(result)).toHaveLength(0); | ||
}); | ||
|
||
it('returns expected `enums` object', () => { | ||
const mockAPIGraphQLConfig: GraphQLProviderConfig['GraphQL'] = { | ||
endpoint: 'endpoint', | ||
defaultAuthMode: 'iam', | ||
modelIntrospection: { | ||
version: 1, | ||
models: {}, | ||
nonModels: {}, | ||
enums: { | ||
TodoStatus: { | ||
name: 'TodoStatus', | ||
values: ['Planned', 'InProgress', 'Completed'], | ||
}, | ||
SomeEnum: { | ||
name: 'SomeEnum', | ||
values: ['value1', 'value2'], | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const result = generateEnumsProperty(mockAPIGraphQLConfig); | ||
|
||
expect(Object.keys(result)).toEqual(['TodoStatus', 'SomeEnum']); | ||
expect((result as any).TodoStatus.values()).toEqual([ | ||
'Planned', | ||
'InProgress', | ||
'Completed', | ||
]); | ||
expect((result as any).SomeEnum.values()).toEqual(['value1', 'value2']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/api-graphql/src/internals/utils/generateEnumsProperty.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { ModelIntrospectionSchema, GraphQLProviderConfig } from '@aws-amplify/core/internals/utils'; | ||
import { EnumTypes } from '@aws-amplify/data-schema-types'; | ||
|
||
export const generateEnumsProperty = <T extends Record<any, any> = never>( | ||
graphqlConfig: GraphQLProviderConfig['GraphQL'], | ||
): EnumTypes<T> => { | ||
const modelIntrospection: ModelIntrospectionSchema | undefined = | ||
graphqlConfig.modelIntrospection; | ||
|
||
if (!modelIntrospection) { | ||
return {} as EnumTypes<never>; | ||
} | ||
|
||
const enums: { | ||
[EnumName in keyof typeof modelIntrospection.enums]: { | ||
values: () => string[]; | ||
}; | ||
} = {}; | ||
|
||
for (const [_, enumData] of Object.entries(modelIntrospection.enums)) { | ||
enums[enumData.name] = { | ||
values: () => enumData.values, | ||
}; | ||
} | ||
|
||
return enums as EnumTypes<T>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/api-graphql/src/internals/utils/isApiGraphQLProviderConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { GraphQLProviderConfig } from '@aws-amplify/core/internals/utils'; | ||
|
||
export function isApiGraphQLConfig( | ||
apiGraphQLConfig: GraphQLProviderConfig['GraphQL'] | undefined, | ||
): apiGraphQLConfig is GraphQLProviderConfig['GraphQL'] { | ||
return apiGraphQLConfig !== undefined; | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/api-graphql/src/internals/utils/isConfigureEventWithResourceConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { HubCapsule, ResourcesConfig } from '@aws-amplify/core'; | ||
|
||
export function isConfigureEventWithResourceConfig( | ||
payload: HubCapsule<'core', { event: string; data?: unknown }>['payload'], | ||
): payload is { | ||
event: 'configure'; | ||
data: ResourcesConfig; | ||
} { | ||
return payload.event === 'configure'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"strictNullChecks": true | ||
"strictNullChecks": true, | ||
"baseUrl": ".", | ||
"paths": { | ||
"@aws-amplify/data-schema-types": ["../../node_modules/@aws-amplify/data-schema-types"] | ||
} | ||
}, | ||
"include": ["./src", "__tests__"] | ||
} |