Skip to content

Commit

Permalink
Fixes an issue with loading not working , relaxes restriction on requ…
Browse files Browse the repository at this point in the history
…iring an input nodes as part of execution via options (#594)
  • Loading branch information
SorsOps authored Dec 17, 2024
1 parent 3d901ed commit 87b0016
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-peas-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/graph-engine": patch
---

Fixes an issue with loading not working , relaxes restriction on requiring an input nodes as part of execution via options
10 changes: 8 additions & 2 deletions packages/graph-engine/src/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ export type InputDefinition = {
};

export type GraphExecuteOptions = {
/**
* Throws an error if the input/output nodes are not found
*/
strict?: boolean;

inputs?: Record<string, InputDefinition>;
/**
* Whether to track and emit stats as part of the execution
Expand Down Expand Up @@ -149,6 +154,7 @@ export interface BatchExecution {
const defaultGraphOpts: IGraph = {
annotations: {}
};

/**
* This is our internal graph representation that we use to perform transformations on
*/
Expand Down Expand Up @@ -643,7 +649,7 @@ export class Graph {
const input = Object.values(this.nodes).find(
x => x.factory.type === 'studio.tokens.generic.input'
);
if (!input) {
if (opts?.strict && !input) {
throw new Error('No input node found');
}

Expand All @@ -659,7 +665,7 @@ export class Graph {
}

//Its possible that there is no input with the name
input.inputs[key]?.setValue(value.value, opts);
input?.inputs[key]?.setValue(value.value, opts);
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/graph-engine/src/programmatic/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class Node {
* @param data
*/
async load(uri: string, data?: unknown) {
this._graph?.loadResource(uri, this, data);
return this._graph?.loadResource(uri, this, data);
}

get isRunning() {
Expand Down
48 changes: 48 additions & 0 deletions packages/graph-engine/tests/suites/graph/execution.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Graph } from '../../../src/graph/graph.js';
import { Node, NumberSchema } from '../../../src/index.js';
import { describe, expect, test } from 'vitest';

class ExternalNode extends Node {
constructor(props) {
super(props);

this.addOutput('foo', { type: NumberSchema });
}

async execute() {
const val = await this.load('foo');
this.outputs.foo.set(val);
}
}

describe('Graph/execution', () => {
test('throws an error when the external loader can be caught', async () => {
const graph = new Graph();

new ExternalNode({ id: 'a', graph });

let thrown;
try {
await graph.execute();
} catch (err) {
thrown = err;
} finally {
expect(thrown).toBeDefined();
expect(thrown.message).toEqual('No external loader specified');
}
});

test('External loading works as expected', async () => {
const graph = new Graph();

graph.externalLoader = async () => {
return 5;
};

new ExternalNode({ id: 'a', graph });

await graph.execute();

expect(graph.nodes.a.outputs.foo.value).toEqual(5);
});
});
27 changes: 27 additions & 0 deletions packages/graph-engine/tests/suites/graph/opts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Graph } from '../../../src/graph/graph.js';
import { describe, expect, test } from 'vitest';
import Passthrough from '../../../src/nodes/generic/passthrough.js';

describe('Graph/options', () => {
test('throws an error if strict mode is enabled', async () => {
const graph = new Graph();

new Passthrough({ id: 'a', graph });
let thrown;
try {
await graph.execute({
strict: true,
inputs: {
foo: {
value: 1
}
}
});
} catch (err) {
thrown = err;
} finally {
expect(thrown).toBeDefined();
expect(thrown.message).toEqual('No input node found');
}
});
});

This file was deleted.

0 comments on commit 87b0016

Please sign in to comment.