-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(api-graphql): add .enums
property to the GQL client
#12980
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b785b8c
chore(api-graphql): bump data-schema-types version to gain EnumTypes
HuiSF 5afd3aa
chore(core): export GraphQLProviderConfig from /internals/utils subpath
HuiSF 6d07e60
feat(api-graphql): add .enums property to the GQL client
HuiSF f059dbd
resolve comments
HuiSF 26ecf81
bump size limit
HuiSF 70944b3
update unit test
HuiSF File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
33 changes: 33 additions & 0 deletions
33
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,33 @@ | ||
// 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: string]: { | ||
values: () => string[]; | ||
}; | ||
} = {}; | ||
|
||
for (const [_, enumData] of Object.entries(modelIntrospection.enums)) { | ||
enums[enumData.name] = { | ||
values: () => enumData.values, | ||
}; | ||
} | ||
|
||
return enums as EnumTypes<T>; | ||
}; |
26 changes: 14 additions & 12 deletions
26
...l/src/internals/generateModelsProperty.ts → ...internals/utils/generateModelsProperty.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
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__"] | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This paths alias is required to allow accessing the unique symbol
__modelMeta__
inside the workspace, soSchema
can infer correct enum type in the unit test codebase.