Skip to content

Commit

Permalink
enable nested collections of nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Jul 12, 2022
1 parent b347bc9 commit 26863b1
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 5 deletions.
1 change: 1 addition & 0 deletions .ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
17 changes: 15 additions & 2 deletions packages/codegen-ts/src/GenTypescriptModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,21 @@ export default class ${this.schema.name}

private getNestedTypeImports(): Import[] {
const typeFields = Object.values(this.schema.fields)
.flatMap(f => f.type)
.filter((f): f is string => typeof f === 'string');
.flatMap(f =>
f.type.map(t => {
if (typeof t === 'string') {
return t;
}

// TODO: technically we need to recurse into array and map structures to pull imports
if (t.type === 'array' || t.type === 'map') {
if (typeof t.values === 'string') {
return t.values;
}
}
}),
)
.filter((f): f is string => f != null);

return typeFields.map(f => tsImport(f, null, './' + f + '.js'));
}
Expand Down
8 changes: 7 additions & 1 deletion packages/integration-tests-ts/src/generated/AppState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SIGNED-SOURCE: <fbfd48c01a5bf75e8867c91bf01688af>
// SIGNED-SOURCE: <65764a353ad3194f110bfcf09dcd69b1>
/**
* AUTO-GENERATED FILE
* Do not modify. Update your schema and re-generate for changes.
Expand All @@ -11,11 +11,13 @@ import { SID_of } from "@aphro/runtime-ts";
import { Context } from "@aphro/runtime-ts";
import Deck from "./Deck.js";
import Identity from "./Identity.js";
import Component from "./Component.js";

export type Data = {
id: SID_of<AppState>;
identity: Identity;
openDeckId: SID_of<Deck> | null;
copiedComponents: readonly Component[];
};

export default class AppState extends Node<Data> {
Expand All @@ -32,4 +34,8 @@ export default class AppState extends Node<Data> {
get openDeckId(): SID_of<Deck> | null {
return this.data.openDeckId;
}

get copiedComponents(): readonly Component[] {
return this.data.copiedComponents;
}
}
1 change: 1 addition & 0 deletions packages/integration-tests-ts/src/schema/domain.aphro
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ AppState as UnmanagedNode {
id: ID<AppState>
identity: Identity
openDeckId: ID<Deck> | null
copiedComponents: Array<Component>
} & Mutations {
create as Create {
identity
Expand Down
2 changes: 1 addition & 1 deletion packages/schema-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ type ArrayField = {
name: string;
type: 'array';
// Ideally we use `Omit` on name but see https://github.com/microsoft/TypeScript/issues/31501
values: RemoveNameField<Field>;
values: RemoveNameField<Field> | string;
} & FieldBase;

export type OutboundEdgesAst = {
Expand Down
26 changes: 26 additions & 0 deletions packages/schema/src/parser/__tests__/condense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,29 @@ Bar as Edge<Foo, Foo> {
// we still return data, even when we have errors.
expect(condensed.edges['Bar'].fields['bar']).not.toBeUndefined();
});

test('Array of nodes', () => {
const ast = parseString(`
engine: postgres
db: test
Bar as Node {
bars: Array<Bar>
}
`);

const [errors, condensed] = condense(ast);
expect(errors.length).toBe(0);
expect(condensed).toEqual({
nodes: {
Bar: {
type: 'node',
name: 'Bar',
primaryKey: 'id',
fields: { bars: { name: 'bars', type: [{ type: 'array', values: 'Bar' }] } },
extensions: {},
storage: { name: 'storage', db: 'test', engine: 'postgres', type: 'sql', tablish: 'bar' },
},
},
edges: {},
});
});
2 changes: 1 addition & 1 deletion packages/schema/src/parser/ohm/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const grammarDefinition = String.raw`
| "any"
ArrayField
= "Array" "<" FieldType ">"
= "Array" "<" (FieldType | name) ">"
MapField
= "Map" "<" NonCompositeFieldType "," FieldType ">"
Expand Down

0 comments on commit 26863b1

Please sign in to comment.