Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dynamic output property #205

Merged
merged 9 commits into from
Aug 26, 2019
27 changes: 17 additions & 10 deletions src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1148,16 +1148,23 @@ export class SchemaBuilder {
if (typeof val === "string") {
return this.addDynamicScalar(methodName, val, block);
}
// @ts-ignore
block[methodName] = (...args: any[]) => {
const config = isList ? [args[0], { list: isList, ...args[1] }] : args;
jasonkuhrt marked this conversation as resolved.
Show resolved Hide resolved
return val.value.factory({
args: config,
typeDef: block,
builder: this,
typeName: block.typeName,
});
};

// At this point `methodName` identifier name can be a misnormer. If the
// given factory returns a function then methodName is accurate, but not
// if the factory returns something else. For example it may return an
// object of methods (aka. methods under a namespace/field/property)
// in which case a better identifier might be `namespace`, `propName`, ...
Object.defineProperty(block, methodName, {
jasonkuhrt marked this conversation as resolved.
Show resolved Hide resolved
get() {
return val.value.factory({
args: [],
typeDef: block,
builder: this,
typeName: block.typeName,
});
},
enumerable: true,
});
});
}

Expand Down