Skip to content

Commit

Permalink
feat(core): add auto type option.
Browse files Browse the repository at this point in the history
for nested edges, auto type allows to have dynamic type based on parent's type and field name.
  • Loading branch information
binier committed Aug 2, 2020
1 parent 52c362e commit de0e0f6
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/core/src/edge/edge-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface EdgeBuilderConstructor {

export class EdgeBuilder {
protected type?: string;
protected _autoType?: boolean
protected edges: Projection<EdgeBuilder> = {};
protected directives: Record<string, DirectiveBuilder> = {};
protected args: ArgsBuilder = new ArgsBuilder();
Expand All @@ -49,8 +50,12 @@ export class EdgeBuilder {
this.type = capitalize(type);
}

if (edges instanceof EdgeBuilder)
return clone(edges);
if (edges instanceof EdgeBuilder) {
const edge = clone(edges);
if (edges.autoType) edge.type = this.type;

return edge;
}

this.setEdges(edges);
}
Expand All @@ -59,6 +64,9 @@ export class EdgeBuilder {
edges: EdgeBuilder | RawProjection<EdgeBuilder>,
overwrite = false
) {
if (edges instanceof EdgeBuilder)
return this.setEdges(edges.edges, overwrite);

this.edges = Object.entries(edges)
.reduce((r, [k, v]) => {
if (typeof v === 'object') {
Expand All @@ -71,6 +79,16 @@ export class EdgeBuilder {
}, overwrite ? {} : this.edges);
}


get autoType() {
return this._autoType;
}

setAutoType(flag = true) {
this._autoType = flag;
return this;
}

withArgs(args: ArgsBuilder | Omit<ArgsBuilderData, 'func'>) {
if (args instanceof ArgsBuilder)
this.args = args;
Expand Down

0 comments on commit de0e0f6

Please sign in to comment.