Skip to content

Commit

Permalink
Merge pull request #729 from aws-amplify/main
Browse files Browse the repository at this point in the history
Release Codegen
  • Loading branch information
alharris-at authored Oct 2, 2023
2 parents d469484 + 9c9e1ba commit fbe38d6
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 8 deletions.
6 changes: 5 additions & 1 deletion packages/amplify-codegen/src/utils/getRelativeTypesPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ const path = require('path');

function getRelativeTypesPath(opsGenDirectory, generatedFileName) {
if (generatedFileName) {
const relativePath = path.relative(opsGenDirectory, generatedFileName);
const relativePath = path
.relative(opsGenDirectory, generatedFileName)
// ensure posix path separators are used
.split(path.win32.sep)
.join(path.posix.sep);

// generatedFileName is in same directory as opsGenDirectory
// i.e. generatedFileName: src/graphql/API.ts, opsGenDirectory: src/graphql
Expand Down
5 changes: 3 additions & 2 deletions packages/amplify-codegen/tests/commands/mock-fs-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const { getOutputFileName } = require('@aws-amplify/graphql-types-generator');
* Mocks existence of `schema.json` using mocks fs
* Mocks existence of `.graphqlconfig.yml` by mocking return value for loadConfig utility
*/
function setupMocks(mockFs, loadConfig, apiId, frontend, target) {
function setupMocks(mockFs, loadConfig, apiId, frontend, target, generatedFileNameOverride, extendMockFs) {
mockFs.restore();
const docsFilePath = {
javascript: 'src/graphql',
android: 'app/src/main/graphql/com/amazonaws/amplify/generated/graphql',
swift: 'graphql',
};
const generatedFileName = getOutputFileName('API', target);
const generatedFileName = generatedFileNameOverride || getOutputFileName('API', target);
const schemaFilePath = 'schema.json';
const nodeModulesPrettier = path.resolve(path.join(__dirname, '../../../../node_modules/prettier'));
const mockedFiles = {
Expand All @@ -26,6 +26,7 @@ function setupMocks(mockFs, loadConfig, apiId, frontend, target) {
lazy: true,
}),
[schemaFilePath]: mockFs.load(path.resolve(path.join(__dirname, './blog-introspection-schema.json'))),
...extendMockFs,
};
mockFs(mockedFiles);

Expand Down
16 changes: 16 additions & 0 deletions packages/amplify-codegen/tests/commands/types-mock-fs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ describe('command - types (mock fs)', () => {
});
});

it('should generate multiple swift files if generatedFileName is a dir', async () => {
const generatedFileName = 'api';
setupMocks(mockFs, loadConfig, MOCK_API_ID, 'swift', 'swift', generatedFileName, { [generatedFileName]: {} });

await generateStatements(MOCK_CONTEXT, false);
await generateTypes(MOCK_CONTEXT, false);

expect(fs.existsSync(generatedFileName)).toBeTruthy();
expect(fs.readdirSync(generatedFileName)).toEqual([
'Types.graphql.swift',
'mutations.graphql.swift',
'queries.graphql.swift',
'subscriptions.graphql.swift',
]);
});

it('should not generate types when target is javascript', async () => {
const generatedFileName = setupMocks(mockFs, loadConfig, MOCK_API_ID, 'javascript', 'javascript');

Expand Down
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 {
};"
`;
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();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class AppSyncModelTypeScriptVisitor<
this.processDirectives(
shouldUseModelNameFieldInHasManyAndBelongsTo,
shouldImputeKeyForUniDirectionalHasMany,
shouldUseFieldsInAssociatedWithInHasOne
shouldUseFieldsInAssociatedWithInHasOne,
);
const imports = this.generateImports();
const enumDeclarations = Object.values(this.enumMap)
Expand Down Expand Up @@ -320,7 +320,9 @@ export class AppSyncModelTypeScriptVisitor<
let nativeType = super.getNativeType(field);

if (this.isEnumType(field)) {
nativeType = `${nativeType} | keyof typeof ${this.getEnumName(this.enumMap[typeName])}`;
const baseEnumType = `keyof typeof ${this.getEnumName(this.enumMap[typeName])}`;
const enumType = field.isList ? `Array<${baseEnumType}>` : baseEnumType;
nativeType = `${nativeType} | ${enumType}`;
}

nativeType = nativeType + nullableTypeUnion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,16 @@ describe('GraphQL statements Formatter', () => {
expect(formattedOutput).toMatchSnapshot();
});

it('Generates formatted output for TS frontend', () => {
it('Generates formatted output for TS frontend with posix path', () => {
const formattedOutput = new GraphQLStatementsFormatter('typescript', 'queries', '../API.ts').format(statements);
expect(formattedOutput).toMatchSnapshot();
});

it('Generates formatted output for TS frontend with windows path', () => {
const formattedOutput = new GraphQLStatementsFormatter('typescript', 'queries', '..\\API.ts').format(statements);
expect(formattedOutput).toMatchSnapshot();
});

it('Generates formatted output for Flow frontend', () => {
const formattedOutput = new GraphQLStatementsFormatter('flow').format(statements);
expect(formattedOutput).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,33 @@ export const getProject = /* GraphQL */ \`
"
`;

exports[`GraphQL statements Formatter Generates formatted output for TS frontend 1`] = `
exports[`GraphQL statements Formatter Generates formatted output for TS frontend with posix path 1`] = `
"/* tslint:disable */
/* eslint-disable */
// this is an auto generated file. This will be overwritten
import * as APITypes from \\"../API\\";
type GeneratedQuery<InputType, OutputType> = string & {
__generatedQueryInput: InputType;
__generatedQueryOutput: OutputType;
};
export const getProject = /* GraphQL */ \`query GetProject($id: ID!) {
getProject(id: $id) {
id
name
createdAt
updatedAt
}
}
\` as GeneratedQuery<
APITypes.GetProjectQueryVariables,
APITypes.GetProjectQuery
>;
"
`;

exports[`GraphQL statements Formatter Generates formatted output for TS frontend with windows path 1`] = `
"/* tslint:disable */
/* eslint-disable */
// this is an auto generated file. This will be overwritten
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as path from 'path';
import prettier, { BuiltInParserName } from 'prettier';
import {
interfaceNameFromOperation,
Expand Down Expand Up @@ -34,7 +35,12 @@ export class GraphQLStatementsFormatter {
}[operation];
this.lintOverrides = [];
this.headerComments = [];
this.typesPath = typesPath ? typesPath.replace(/.ts/i, '') : null;
this.typesPath = typesPath
? typesPath.replace(/.ts/i, '')
// ensure posix path separators are used
.split(path.win32.sep)
.join(path.posix.sep)
: null;
this.includeTypeScriptTypes = !!(this.language === 'typescript' && this.opTypeName && this.typesPath);
}

Expand Down

0 comments on commit fbe38d6

Please sign in to comment.