Skip to content

Commit

Permalink
make gen async for codegenstep
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Jun 9, 2022
1 parent d8c3407 commit 7bd397e
Show file tree
Hide file tree
Showing 12 changed files with 451 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class GenTypescriptMutationImpls extends CodegenStep {
super();
}

gen(): CodegenFile {
async gen(): Promise<CodegenFile> {
// load existing file if it exists.
// condense imports since we'll want to add imports if we add impls...
// `getCode` is partial if there was an existing file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export class GenTypescriptMutations extends CodegenStep {
return Object.values(schema.extensions.mutations?.mutations || []).length > 0;
}

constructor(private schema: Node) {
constructor(private schema: Node, dest: string) {
super();
}

gen(): CodegenFile {
async gen(): Promise<CodegenFile> {
return new TypescriptFile(
this.schema.name + 'Mutations.ts',
`${importsToString(this.collectImports())}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import { algolTemplates } from '@aphro/codegen-api';
const grammarExtensions = [mutationExtension];
const { compileFromString } = createCompiler({ grammarExtensions });

test('All primitive field references can be used as inputs', () => {
primitives.forEach(primitive => {
const schema = `
test('All primitive field references can be used as inputs', async () => {
await Promise.all(
primitives.map(async primitive => {
const schema = `
Foo as Node {
someField: ${primitive}
} & Mutations {
Expand All @@ -20,12 +21,12 @@ test('All primitive field references can be used as inputs', () => {
}
}
`;
const contents = removeSignature(
genIt(compileFromString(schema)[1].nodes.Foo).contents,
algolTemplates,
);
const contents = removeSignature(
(await genIt(compileFromString(schema)[1].nodes.Foo)).contents,
algolTemplates,
);

expect(contents).toEqual(`import { ICreateOrUpdateBuilder } from "@aphro/runtime-ts";
expect(contents).toEqual(`import { ICreateOrUpdateBuilder } from "@aphro/runtime-ts";
import { Context } from "@aphro/runtime-ts";
import { MutationsBase } from "@aphro/runtime-ts";
import Foo from "./Foo.js";
Expand Down Expand Up @@ -63,25 +64,27 @@ export default class FooMutations extends MutationsBase<Foo, Data> {
}
}
`);
});
}),
);
});

test('All primitive types can be used as custom inputs', () => {
primitives.forEach(primitive => {
const schema = `
test('All primitive types can be used as custom inputs', async () => {
await Promise.all(
primitives.map(async primitive => {
const schema = `
Foo as Node {
} & Mutations {
create as Create {
customField: ${primitive}
}
}
`;
const contents = removeSignature(
genIt(compileFromString(schema)[1].nodes.Foo).contents,
algolTemplates,
);
const contents = removeSignature(
(await genIt(compileFromString(schema)[1].nodes.Foo)).contents,
algolTemplates,
);

expect(contents).toEqual(`import { ICreateOrUpdateBuilder } from "@aphro/runtime-ts";
expect(contents).toEqual(`import { ICreateOrUpdateBuilder } from "@aphro/runtime-ts";
import { Context } from "@aphro/runtime-ts";
import { MutationsBase } from "@aphro/runtime-ts";
import Foo from "./Foo.js";
Expand Down Expand Up @@ -119,16 +122,17 @@ export default class FooMutations extends MutationsBase<Foo, Data> {
}
}
`);
});
}),
);
});

test('All composite types can be used as inputs', () => {});

test('Node type names can be used as inputs', () => {
test('Node type names can be used as inputs', async () => {
fc.assert(
fc.property(
fc.asyncProperty(
fc.stringOf(fc.constantFrom('a', 'b', 'c', 'd', 'e'), { maxLength: 6, minLength: 3 }),
customName => {
async customName => {
const schema = `
Foo as Node {
} & Mutations {
Expand All @@ -138,7 +142,7 @@ test('Node type names can be used as inputs', () => {
}
`;
const contents = removeSignature(
genIt(compileFromString(schema)[1].nodes.Foo).contents,
(await genIt(compileFromString(schema)[1].nodes.Foo)).contents,
algolTemplates,
);

Expand Down Expand Up @@ -191,8 +195,8 @@ export default class FooMutations extends MutationsBase<Foo, Data> {
);
});

function genIt(schema: Node) {
return new GenTypescriptMutations(schema).gen();
async function genIt(schema: Node) {
return await new GenTypescriptMutations(schema, '').gen();
}

function asTsType(prim: PrimitiveSubtype): string {
Expand Down
3 changes: 1 addition & 2 deletions packages/codegen-api/src/CodegenStep.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { CodegenFile } from './CodegenFile.js';
import { Node, Edge } from '@aphro/schema-api';

export default abstract class CodegenStep {
constructor() {}

abstract gen(): CodegenFile;
abstract gen(): Promise<CodegenFile>;
}
4 changes: 2 additions & 2 deletions packages/codegen-sql/src/GenSqlTableSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export default class GenSqlTableSchema extends CodegenStep {
return schema.storage.type === 'sql';
}

constructor(private schema: Node) {
constructor(private schema: Node, dest: string) {
super();
}

gen(): CodegenFile {
async gen(): Promise<CodegenFile> {
const str = this.getSqlString();
return new SqlFile(
`${this.schema.name}.${this.schema.storage.engine}.sql`,
Expand Down
4 changes: 2 additions & 2 deletions packages/codegen-ts/src/GenTypescriptModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ export default class GenTypescriptModel extends CodegenStep {
return true;
}

constructor(private schema: Node) {
constructor(private schema: Node, dest: string) {
super();
}

gen(): CodegenFile {
async gen(): Promise<CodegenFile> {
return new TypescriptFile(
this.schema.name + '.ts',
`${importsToString(this.collectImports())}
Expand Down
4 changes: 2 additions & 2 deletions packages/codegen-ts/src/GenTypescriptQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class GenTypescriptQuery extends CodegenStep {
return true;
}

constructor(private schema: Node) {
constructor(private schema: Node, dest: string) {
super();
}

Expand All @@ -28,7 +28,7 @@ export default class GenTypescriptQuery extends CodegenStep {
// Since we can have edge data... so we'd need edge queries rather than a query per schema.
// b/c structure on the edges...
// TODO: de-duplicate imports by storing imports in an intermediate structure.
gen(): CodegenFile {
async gen(): Promise<CodegenFile> {
const imports = this.collectImports();
return new TypescriptFile(
nodeFn.queryTypeName(this.schema.name) + '.ts',
Expand Down
4 changes: 2 additions & 2 deletions packages/codegen-ts/src/GenTypescriptSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export default class GenTypescriptSpec extends CodegenStep {
return true;
}

constructor(private schema: Node) {
constructor(private schema: Node, dest: string) {
super();
}

gen(): CodegenFile {
async gen(): Promise<CodegenFile> {
const imports = this.collectImports();
return new TypescriptFile(
this.schema.name + 'Spec.ts',
Expand Down
24 changes: 12 additions & 12 deletions packages/codegen-ts/src/__tests__/GenTypescriptModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ Bar as Node {
}
`;

test('Generating an ID only model', () => {
const contents = genIt(compileFromString(IDOnlySchema)[1].nodes.IDOnly).contents;
test('Generating an ID only model', async () => {
const contents = (await genIt(compileFromString(IDOnlySchema)[1].nodes.IDOnly)).contents;
expect(contents).toEqual(
`// SIGNED-SOURCE: <a92063a419b3fdeaba407c8fd67efbf8>
import { P } from "@aphro/query-runtime-ts";
Expand All @@ -70,10 +70,9 @@ export default class IDOnly extends Model<Data> {
);
});

test('Generating all primitive fields', () => {
const contents = genIt(
compileFromString(PrimitiveFieldsSchema)[1].nodes.PrimitiveFields,
).contents;
test('Generating all primitive fields', async () => {
const contents = (await genIt(compileFromString(PrimitiveFieldsSchema)[1].nodes.PrimitiveFields))
.contents;
expect(contents).toEqual(`// SIGNED-SOURCE: <e88852c6860b87cb7a313e1d54871e66>
import { P } from "@aphro/query-runtime-ts";
import { Model } from "@aphro/model-runtime-ts";
Expand Down Expand Up @@ -121,8 +120,8 @@ export default class PrimitiveFields extends Model<Data> {
`);
});

test('Outbound field edge', () => {
const contents = genIt(compileFromString(OutboundFieldEdgeSchema)[1].nodes.Foo).contents;
test('Outbound field edge', async () => {
const contents = (await genIt(compileFromString(OutboundFieldEdgeSchema)[1].nodes.Foo)).contents;
expect(contents).toEqual(`// SIGNED-SOURCE: <c7e9a1b500db49dc134745c01da4495d>
import { P } from "@aphro/query-runtime-ts";
import { Model } from "@aphro/model-runtime-ts";
Expand All @@ -145,8 +144,9 @@ export default class Foo extends Model<Data> {
`);
});

test('Outbound foreign key edge', () => {
const contents = genIt(compileFromString(OutboundForeignKeyEdgeSchema)[1].nodes.Bar).contents;
test('Outbound foreign key edge', async () => {
const contents = (await genIt(compileFromString(OutboundForeignKeyEdgeSchema)[1].nodes.Bar))
.contents;
expect(contents).toEqual(`// SIGNED-SOURCE: <aae232d98ea740396d0665e7e3536165>
import { P } from "@aphro/query-runtime-ts";
import { Model } from "@aphro/model-runtime-ts";
Expand All @@ -164,6 +164,6 @@ export default class Bar extends Model<Data> {
`);
});

function genIt(schema: Node) {
return new GenTypescriptModel(schema).gen();
async function genIt(schema: Node) {
return await new GenTypescriptModel(schema, '').gen();
}
18 changes: 9 additions & 9 deletions packages/codegen-ts/src/__tests__/GenTypescriptQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ BarToFooEdge as InverseEdge<FooToBarEdge> {}

const InboundThroughLocalFieldSchema = ``;

test('NoEdgesSchema', () => {
const contents = genIt(compileFromString(NoEdgesSchema)[1].nodes.Foo).contents;
test('NoEdgesSchema', async () => {
const contents = (await genIt(compileFromString(NoEdgesSchema)[1].nodes.Foo)).contents;

// TODO: remove unneeded imports
// Validation should require that a primary key field exists
Expand Down Expand Up @@ -120,8 +120,9 @@ export default class FooQuery extends DerivedQuery<Foo> {
`);
});

test('OutboundEdgeViaFieldSchema', () => {
const contents = genIt(compileFromString(OutboundEdgeViaFieldSchema)[1].nodes.Foo).contents;
test('OutboundEdgeViaFieldSchema', async () => {
const contents = (await genIt(compileFromString(OutboundEdgeViaFieldSchema)[1].nodes.Foo))
.contents;

// TODO: queryBar is wrong as it needs a `where` statement applied
// to understand _how_ we're hopping.
Expand Down Expand Up @@ -170,10 +171,9 @@ export default class FooQuery extends DerivedQuery<Foo> {
`);
});

test('OutboundThroughForeignFieldSchema', () => {
const contents = genIt(
compileFromString(OutboundThroughForeignFieldSchema)[1].nodes.Foo,
).contents;
test('OutboundThroughForeignFieldSchema', async () => {
const contents = (await genIt(compileFromString(OutboundThroughForeignFieldSchema)[1].nodes.Foo))
.contents;

expect(contents).toEqual(`// SIGNED-SOURCE: <56d05175d9549f70182c344f3305038d>
import { DerivedQuery } from "@aphro/query-runtime-ts";
Expand Down Expand Up @@ -213,5 +213,5 @@ export default class FooQuery extends DerivedQuery<Foo> {
});

function genIt(schema: Node) {
return new GenTypescriptQuery(schema).gen();
return new GenTypescriptQuery(schema, '').gen();
}
16 changes: 12 additions & 4 deletions packages/codegen/src/CodegenPipeline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { maybeMap } from '@strut/utils';
import * as fs from 'fs';
import * as path from 'path';
import { Node, Edge } from '@aphro/schema-api';
Expand All @@ -14,9 +13,18 @@ export default class CodegenPipleine {
constructor(private readonly steps: readonly Step[]) {}

async gen(schemas: (Node | Edge)[], dest: string) {
const files = schemas.flatMap(schema =>
maybeMap(this.steps, step => (!step.accepts(schema) ? null : new step(schema, dest).gen())),
);
let files = (
await Promise.all(
schemas.map(
async schema =>
await Promise.all(
this.steps.map(
async step => await (!step.accepts(schema) ? null : new step(schema, dest).gen()),
),
),
),
)
).flatMap(f => f.filter((f): f is CodegenFile => f != null));

await fs.promises.mkdir(dest, { recursive: true });
const toWrite = await this.checkHashesAndAddManualCode(dest, files);
Expand Down
Loading

0 comments on commit 7bd397e

Please sign in to comment.