Skip to content

Commit cdca3eb

Browse files
author
Dane Pilcher
authored
fix: set correct type for array of enum (#703)
1 parent d469484 commit cdca3eb

File tree

3 files changed

+135
-2
lines changed

3 files changed

+135
-2
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`TypeScript visitor list enum 1`] = `
4+
"import { ModelInit, MutableModel, PersistentModelConstructor } from \\"@aws-amplify/datastore\\";
5+
import { initSchema } from \\"@aws-amplify/datastore\\";
6+
7+
import { schema } from \\"./schema\\";
8+
9+
export enum DayOfWeek {
10+
MONDAY = \\"MONDAY\\",
11+
TUESDAY = \\"TUESDAY\\",
12+
WEDNESDAY = \\"WEDNESDAY\\",
13+
THURSDAY = \\"THURSDAY\\",
14+
FRIDAY = \\"FRIDAY\\",
15+
SATURDAY = \\"SATURDAY\\",
16+
SUNDAY = \\"SUNDAY\\"
17+
}
18+
19+
20+
21+
type EagerRecurrenceModel = {
22+
readonly daysOfWeek: DayOfWeek[] | Array<keyof typeof DayOfWeek>;
23+
}
24+
25+
type LazyRecurrenceModel = {
26+
readonly daysOfWeek: DayOfWeek[] | Array<keyof typeof DayOfWeek>;
27+
}
28+
29+
export declare type RecurrenceModel = LazyLoading extends LazyLoadingDisabled ? EagerRecurrenceModel : LazyRecurrenceModel
30+
31+
export declare const RecurrenceModel: (new (init: ModelInit<RecurrenceModel>) => RecurrenceModel)
32+
33+
const { Recurrence } = initSchema(schema) as {
34+
Recurrence: PersistentModelConstructor<RecurrenceModel>;
35+
};
36+
37+
export {
38+
39+
};"
40+
`;
41+
42+
exports[`TypeScript visitor singular enum 1`] = `
43+
"import { ModelInit, MutableModel, PersistentModelConstructor } from \\"@aws-amplify/datastore\\";
44+
import { initSchema } from \\"@aws-amplify/datastore\\";
45+
46+
import { schema } from \\"./schema\\";
47+
48+
export enum Frequency {
49+
YEARLY = \\"YEARLY\\",
50+
WEEKLY = \\"WEEKLY\\"
51+
}
52+
53+
54+
55+
type EagerRecurrenceModel = {
56+
readonly frequency: Frequency | keyof typeof Frequency;
57+
}
58+
59+
type LazyRecurrenceModel = {
60+
readonly frequency: Frequency | keyof typeof Frequency;
61+
}
62+
63+
export declare type RecurrenceModel = LazyLoading extends LazyLoadingDisabled ? EagerRecurrenceModel : LazyRecurrenceModel
64+
65+
export declare const RecurrenceModel: (new (init: ModelInit<RecurrenceModel>) => RecurrenceModel)
66+
67+
const { Recurrence } = initSchema(schema) as {
68+
Recurrence: PersistentModelConstructor<RecurrenceModel>;
69+
};
70+
71+
export {
72+
73+
};"
74+
`;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { buildSchema, GraphQLSchema, parse, visit } from 'graphql';
2+
import { validateTs } from '@graphql-codegen/testing';
3+
import { TYPESCRIPT_SCALAR_MAP } from '../../scalars';
4+
import { directives, scalars } from '../../scalars/supported-directives';
5+
import { AppSyncModelTypeScriptVisitor } from '../../visitors/appsync-typescript-visitor';
6+
7+
const buildSchemaWithDirectives = (schema: String): GraphQLSchema => {
8+
return buildSchema([schema, directives, scalars].join('\n'));
9+
};
10+
const getVisitor = (schema: string): AppSyncModelTypeScriptVisitor => {
11+
const ast = parse(schema);
12+
const builtSchema = buildSchemaWithDirectives(schema);
13+
const visitor = new AppSyncModelTypeScriptVisitor(
14+
builtSchema,
15+
{ directives, target: 'typescript', scalars: TYPESCRIPT_SCALAR_MAP, codegenVersion: '3.3.4' },
16+
{},
17+
);
18+
visit(ast, { leave: visitor });
19+
return visitor;
20+
};
21+
22+
describe('TypeScript visitor', () => {
23+
test('singular enum', () => {
24+
const schema = /* GraphQL */ `
25+
enum Frequency {
26+
YEARLY
27+
WEEKLY
28+
}
29+
30+
type Recurrence {
31+
frequency: Frequency!
32+
}
33+
`;
34+
const visitor = getVisitor(schema);
35+
expect(visitor.generate()).toMatchSnapshot();
36+
});
37+
38+
test('list enum', () => {
39+
const schema = /* GraphQL */ `
40+
enum DayOfWeek {
41+
MONDAY
42+
TUESDAY
43+
WEDNESDAY
44+
THURSDAY
45+
FRIDAY
46+
SATURDAY
47+
SUNDAY
48+
}
49+
50+
type Recurrence {
51+
daysOfWeek: [DayOfWeek!]!
52+
}
53+
`;
54+
const visitor = getVisitor(schema);
55+
expect(visitor.generate()).toMatchSnapshot();
56+
});
57+
});

packages/appsync-modelgen-plugin/src/visitors/appsync-typescript-visitor.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class AppSyncModelTypeScriptVisitor<
4848
this.processDirectives(
4949
shouldUseModelNameFieldInHasManyAndBelongsTo,
5050
shouldImputeKeyForUniDirectionalHasMany,
51-
shouldUseFieldsInAssociatedWithInHasOne
51+
shouldUseFieldsInAssociatedWithInHasOne,
5252
);
5353
const imports = this.generateImports();
5454
const enumDeclarations = Object.values(this.enumMap)
@@ -320,7 +320,9 @@ export class AppSyncModelTypeScriptVisitor<
320320
let nativeType = super.getNativeType(field);
321321

322322
if (this.isEnumType(field)) {
323-
nativeType = `${nativeType} | keyof typeof ${this.getEnumName(this.enumMap[typeName])}`;
323+
const baseEnumType = `keyof typeof ${this.getEnumName(this.enumMap[typeName])}`;
324+
const enumType = field.isList ? `Array<${baseEnumType}>` : baseEnumType;
325+
nativeType = `${nativeType} | ${enumType}`;
324326
}
325327

326328
nativeType = nativeType + nullableTypeUnion;

0 commit comments

Comments
 (0)