-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: set correct type for array of enum (#703)
- Loading branch information
Showing
3 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...elgen-plugin/src/__tests__/visitors/__snapshots__/appsync-typescript-visitor.test.ts.snap
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,74 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`TypeScript visitor list enum 1`] = ` | ||
"import { ModelInit, MutableModel, PersistentModelConstructor } from \\"@aws-amplify/datastore\\"; | ||
import { initSchema } from \\"@aws-amplify/datastore\\"; | ||
import { schema } from \\"./schema\\"; | ||
export enum DayOfWeek { | ||
MONDAY = \\"MONDAY\\", | ||
TUESDAY = \\"TUESDAY\\", | ||
WEDNESDAY = \\"WEDNESDAY\\", | ||
THURSDAY = \\"THURSDAY\\", | ||
FRIDAY = \\"FRIDAY\\", | ||
SATURDAY = \\"SATURDAY\\", | ||
SUNDAY = \\"SUNDAY\\" | ||
} | ||
type EagerRecurrenceModel = { | ||
readonly daysOfWeek: DayOfWeek[] | Array<keyof typeof DayOfWeek>; | ||
} | ||
type LazyRecurrenceModel = { | ||
readonly daysOfWeek: DayOfWeek[] | Array<keyof typeof DayOfWeek>; | ||
} | ||
export declare type RecurrenceModel = LazyLoading extends LazyLoadingDisabled ? EagerRecurrenceModel : LazyRecurrenceModel | ||
export declare const RecurrenceModel: (new (init: ModelInit<RecurrenceModel>) => RecurrenceModel) | ||
const { Recurrence } = initSchema(schema) as { | ||
Recurrence: PersistentModelConstructor<RecurrenceModel>; | ||
}; | ||
export { | ||
};" | ||
`; | ||
exports[`TypeScript visitor singular enum 1`] = ` | ||
"import { ModelInit, MutableModel, PersistentModelConstructor } from \\"@aws-amplify/datastore\\"; | ||
import { initSchema } from \\"@aws-amplify/datastore\\"; | ||
import { schema } from \\"./schema\\"; | ||
export enum Frequency { | ||
YEARLY = \\"YEARLY\\", | ||
WEEKLY = \\"WEEKLY\\" | ||
} | ||
type EagerRecurrenceModel = { | ||
readonly frequency: Frequency | keyof typeof Frequency; | ||
} | ||
type LazyRecurrenceModel = { | ||
readonly frequency: Frequency | keyof typeof Frequency; | ||
} | ||
export declare type RecurrenceModel = LazyLoading extends LazyLoadingDisabled ? EagerRecurrenceModel : LazyRecurrenceModel | ||
export declare const RecurrenceModel: (new (init: ModelInit<RecurrenceModel>) => RecurrenceModel) | ||
const { Recurrence } = initSchema(schema) as { | ||
Recurrence: PersistentModelConstructor<RecurrenceModel>; | ||
}; | ||
export { | ||
};" | ||
`; |
57 changes: 57 additions & 0 deletions
57
packages/appsync-modelgen-plugin/src/__tests__/visitors/appsync-typescript-visitor.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,57 @@ | ||
import { buildSchema, GraphQLSchema, parse, visit } from 'graphql'; | ||
import { validateTs } from '@graphql-codegen/testing'; | ||
import { TYPESCRIPT_SCALAR_MAP } from '../../scalars'; | ||
import { directives, scalars } from '../../scalars/supported-directives'; | ||
import { AppSyncModelTypeScriptVisitor } from '../../visitors/appsync-typescript-visitor'; | ||
|
||
const buildSchemaWithDirectives = (schema: String): GraphQLSchema => { | ||
return buildSchema([schema, directives, scalars].join('\n')); | ||
}; | ||
const getVisitor = (schema: string): AppSyncModelTypeScriptVisitor => { | ||
const ast = parse(schema); | ||
const builtSchema = buildSchemaWithDirectives(schema); | ||
const visitor = new AppSyncModelTypeScriptVisitor( | ||
builtSchema, | ||
{ directives, target: 'typescript', scalars: TYPESCRIPT_SCALAR_MAP, codegenVersion: '3.3.4' }, | ||
{}, | ||
); | ||
visit(ast, { leave: visitor }); | ||
return visitor; | ||
}; | ||
|
||
describe('TypeScript visitor', () => { | ||
test('singular enum', () => { | ||
const schema = /* GraphQL */ ` | ||
enum Frequency { | ||
YEARLY | ||
WEEKLY | ||
} | ||
type Recurrence { | ||
frequency: Frequency! | ||
} | ||
`; | ||
const visitor = getVisitor(schema); | ||
expect(visitor.generate()).toMatchSnapshot(); | ||
}); | ||
|
||
test('list enum', () => { | ||
const schema = /* GraphQL */ ` | ||
enum DayOfWeek { | ||
MONDAY | ||
TUESDAY | ||
WEDNESDAY | ||
THURSDAY | ||
FRIDAY | ||
SATURDAY | ||
SUNDAY | ||
} | ||
type Recurrence { | ||
daysOfWeek: [DayOfWeek!]! | ||
} | ||
`; | ||
const visitor = getVisitor(schema); | ||
expect(visitor.generate()).toMatchSnapshot(); | ||
}); | ||
}); |
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