Skip to content
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

fix: stop pascal casing enums for MIS #931

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ module.exports = {
'__mocks__',
'__e2e__',
'coverage',

// Ignore project/file templates
'function-template-dir',

Expand All @@ -271,6 +271,8 @@ module.exports = {
'test-apps',

// Ignore lint for standalone JSON validation function
'/packages/appsync-modelgen-plugin/src/validate-cjs.js'
'/packages/appsync-modelgen-plugin/src/validate-cjs.js',

'.eslintrc.js'
]
};
Original file line number Diff line number Diff line change
Expand Up @@ -2971,6 +2971,13 @@ exports[`Model Introspection Visitor Metadata snapshot should generate correct m
\\"enumVal1\\",
\\"enumVal2\\"
]
},
\\"camel\\": {
\\"name\\": \\"camel\\",
\\"values\\": [
\\"bactrian\\",
\\"dromedary\\"
]
}
},
\\"nonModels\\": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ describe('AppSync Dart Visitor', () => {
`;
const outputModels: string[] = ['Todo', 'Task'];
outputModels.forEach(model => {
const generatedCode = getVisitor({schema, selectedType: model, directives: [...AppSyncDirectives, ...V1Directives, DeprecatedDirective] }).generate();
const generatedCode = getVisitor({ schema, selectedType: model, directives: [...AppSyncDirectives, ...V1Directives, DeprecatedDirective] }).generate();
expect(generatedCode).toMatchSnapshot();
});
});
Expand Down Expand Up @@ -330,6 +330,26 @@ describe('AppSync Dart Visitor', () => {
expect(generatedCode).toMatchSnapshot();
});
});

it('should pascal case enum', () => {
const schema = /* GraphQL */ `
enum status {
yes
no
maybe
}`;

const generatedCode = getVisitor({ schema, selectedType: 'status' }).generate();
const statusEnum = generatedCode.split('\n').slice(-5).join('\n')
expect(statusEnum).toMatchInlineSnapshot(`
"enum Status {
yes,
no,
maybe
}"
`);

})
});

describe('Field tests', () => {
Expand Down Expand Up @@ -405,7 +425,7 @@ describe('AppSync Dart Visitor', () => {
name: String
}
`;
const visitor = getVisitor({schema, generate: CodeGenGenerateEnum.loader });
const visitor = getVisitor({ schema, generate: CodeGenGenerateEnum.loader });
const generatedCode = visitor.generate();
expect(generatedCode).toMatchSnapshot();
});
Expand Down Expand Up @@ -565,7 +585,7 @@ describe('AppSync Dart Visitor', () => {

models.forEach(type => {
it(`should generate correct dart class for ${!type ? 'ModelProvider' : type} with nullsafety`, () => {
const generatedCode = getVisitor({schema, selectedType: type, generate: !type ? CodeGenGenerateEnum.loader : CodeGenGenerateEnum.code }).generate();
const generatedCode = getVisitor({ schema, selectedType: type, generate: !type ? CodeGenGenerateEnum.loader : CodeGenGenerateEnum.code }).generate();

expect(generatedCode).toMatchSnapshot();
})
Expand Down Expand Up @@ -594,7 +614,7 @@ describe('AppSync Dart Visitor', () => {
name: String
}
`;
const visitor = getVisitor({ schema, isTimestampFieldsAdded: true });
const visitor = getVisitor({ schema, isTimestampFieldsAdded: true });


const generatedCode = visitor.generate();
Expand Down Expand Up @@ -871,7 +891,7 @@ describe('AppSync Dart Visitor', () => {
content: String
related: [SqlRelated!] @hasMany(references: ["primaryId"])
}

type SqlRelated @refersTo(name: "sql_related") @model {
id: Int! @primaryKey
content: String
Expand Down Expand Up @@ -899,7 +919,7 @@ describe('AppSync Dart Visitor', () => {
content: String
related: SqlRelated @hasOne(references: ["primaryId"])
}

type SqlRelated @refersTo(name: "sql_related") @model {
id: Int! @primaryKey
content: String
Expand Down Expand Up @@ -927,13 +947,13 @@ describe('AppSync Dart Visitor', () => {
relatedMany: [RelatedMany] @hasMany(references: ["primaryId"])
relatedOne: RelatedOne @hasOne(references: ["primaryId"])
}

type RelatedMany @model {
id: ID! @primaryKey
primaryId: ID!
primary: Primary @belongsTo(references: ["primaryId"])
}

type RelatedOne @model {
id: ID! @primaryKey
primaryId: ID!
Expand All @@ -959,7 +979,7 @@ describe('AppSync Dart Visitor', () => {
bar1: Bar @hasOne(references: ["bar1Id"])
bar2: Bar @hasOne(references: ["bar2Id"])
}

type Bar @model {
id: ID!
bar1Id: ID
Expand Down Expand Up @@ -990,7 +1010,7 @@ describe('AppSync Dart Visitor', () => {
content: String
related: [Related!] @hasMany(references: ["primaryTenantId", "primaryInstanceId", "primaryRecordId"])
}

type Related @model {
content: String
primaryTenantId: ID!
Expand Down Expand Up @@ -1021,7 +1041,7 @@ describe('AppSync Dart Visitor', () => {
content: String
related: Related @hasOne(references: ["primaryTenantId", "primaryInstanceId", "primaryRecordId"])
}

type Related @model {
content: String
primaryTenantId: ID!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ describe('Javascript visitor', () => {
`);
});

it('should pascal case enum', () => {
const schema = /* GraphQL */ `
enum status {
pending
done
}`;

const visitor = getVisitor(schema)
const enumObj = (visitor as any).enumMap['status'];
const statusEnum = (visitor as any).generateEnumObject(enumObj, true);
validateTs(statusEnum);
expect(statusEnum).toMatchInlineSnapshot(`
"export const Status = {
\\"PENDING\\": \\"pending\\",
\\"DONE\\": \\"done\\"
};"
`);
});

it('should generate import statements', () => {
const imports = (visitor as any).generateImportsJavaScriptImplementation();
validateTs(imports);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ describe('Model Introspection Visitor', () => {
interface SimpleInterface {
firstName: String!
}
enum camel {
bactrian
dromedary
}
union SimpleUnion = SimpleModel | SimpleEnum | SimpleNonModelType | SimpleInput | SimpleInterface
`;
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema);
Expand Down Expand Up @@ -143,6 +147,10 @@ describe('Model Introspection Visitor', () => {
expect((visitor as any).getType('SimpleInterface')).toEqual({ interface: 'SimpleInterface' });
});

it('should not pascal case enum', () => {
expect((visitor as any).getType('camel')).toEqual({ enum: 'camel' })
})

it('should throw error for unknown type', () => {
expect(() => (visitor as any).getType('unknown')).toThrowError('Unknown type');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { generateLicense } from '../utils/generateLicense';
import { GraphQLSchema } from 'graphql';
import { DART_SCALAR_MAP } from '../scalars';
import { pascalCase } from 'change-case';

export interface RawAppSyncModelDartConfig extends RawAppSyncModelConfig {}

Expand Down Expand Up @@ -166,7 +167,7 @@ export class AppSyncModelDartVisitor<
//Enum
Object.entries(this.getSelectedEnums()).forEach(([name, enumVal]) => {
const body = Object.values(enumVal.values).join(',\n');
result.push([`enum ${name} {`, indentMultiline(body), '}'].join('\n'));
result.push([`enum ${pascalCase(name)} {`, indentMultiline(body), '}'].join('\n'));
});
return result.join('\n\n');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export class AppSyncModelJavaVisitor<
const enumDeclaration = new JavaDeclarationBlock()
.asKind('enum')
.access('public')
.withName(this.getEnumName(enumValue))
.withName(pascalCase(this.getEnumName(enumValue)))
.annotate(['SuppressWarnings("all")'])
.withComment('Auto generated enum from GraphQL schema.');
const body = Object.values(enumValue.values);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { DEFAULT_SCALARS, NormalizedScalarsMap } from '@graphql-codegen/visitor-
import { GraphQLSchema } from 'graphql';
import { AppSyncModelTypeScriptVisitor } from './appsync-typescript-visitor';
import { CodeGenEnum, CodeGenModel, ParsedAppSyncModelConfig, RawAppSyncModelConfig } from './appsync-visitor';
import { pascalCase } from 'change-case';

export interface RawAppSyncModelJavaScriptConfig extends RawAppSyncModelConfig {
/**
Expand Down Expand Up @@ -112,7 +113,7 @@ export class AppSyncModelJavascriptVisitor<
* @param exportEnum: boolean export the enum object
*/
protected generateEnumObject(enumObj: CodeGenEnum, exportEnum: boolean = false): string {
const enumName = this.getEnumName(enumObj);
const enumName = pascalCase(this.getEnumName(enumObj));
const header = [exportEnum ? 'export' : null, 'const', enumName].filter(h => h).join(' ');

return `${header} = ${JSON.stringify(enumObj.values, null, 2)};`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { indent, indentMultiline, NormalizedScalarsMap } from '@graphql-codegen/visitor-plugin-common';
import { camelCase } from 'change-case';
import { camelCase, pascalCase } from 'change-case';
import { GraphQLSchema } from 'graphql';
import { lowerCaseFirst } from 'lower-case-first';
import { plurality } from 'graphql-transformer-common';
Expand Down Expand Up @@ -305,7 +305,7 @@ export class AppSyncSwiftVisitor<
.asKind('enum')
.access('public')
.withProtocols(['String', 'EnumPersistable'])
.withName(this.getEnumName(enumValue));
.withName(pascalCase(this.getEnumName(enumValue)));

Object.entries(enumValue.values).forEach(([name, value]) => {
enumDeclaration.addEnumValue(name, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
ParsedAppSyncModelConfig,
RawAppSyncModelConfig,
} from './appsync-visitor';
import { pascalCase } from 'change-case';

export interface RawAppSyncModelTypeScriptConfig extends RawAppSyncModelConfig {}
export interface ParsedAppSyncModelTypeScriptConfig extends ParsedAppSyncModelConfig {
Expand Down Expand Up @@ -77,7 +78,7 @@ export class AppSyncModelTypeScriptVisitor<
protected generateEnumDeclarations(enumObj: CodeGenEnum, exportEnum: boolean = false): string {
const enumDeclarations = new TypeScriptDeclarationBlock()
.asKind('enum')
.withName(this.getEnumName(enumObj))
.withName(pascalCase(this.getEnumName(enumObj)))
.withEnumValues(enumObj.values)
.export(exportEnum);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ParsedConfig,
RawConfig,
} from '@graphql-codegen/visitor-plugin-common';
import { camelCase, constantCase, pascalCase } from 'change-case';
import { camelCase, constantCase } from 'change-case';
import { plural } from 'pluralize';
import crypto from 'crypto';
import {
Expand Down Expand Up @@ -608,9 +608,9 @@ export class AppSyncModelVisitor<

protected getEnumName(enumField: CodeGenEnum | string): string {
if (typeof enumField === 'string') {
return pascalCase(enumField);
return enumField;
}
return pascalCase(enumField.name);
return enumField.name;
}

protected getModelName(model: CodeGenModel) {
Expand Down
Loading