#It looks like default values aren't
1 messages · Page 1 of 1 (latest)
I also expected that ⬆️ to work (cc @woven igloo) but I found I had to use a "constructor" pattern as well.
Same for class variables where I had to use an actual constructor vs default values.
This works:
import { dag, Container, Directory, object, func } from "@dagger.io/dagger"
@object()
class ChrisThomas {
static readonly STAGING_URL: string;
constructor() {
this.STAGING_URL = "http://github.com/dagger/dagger";
}
@func()
run(url?: string): Container {
url = this.STAGING_URL;
return dag.container().from("alpine:latest").withEnvVariable("STAGING_URL", url);
}
}
It's not that your default value isn't handled, it's that you're passing it by reference to a variable.
Try to add a value directly and it should work:
@func()
async run(url: string = "my_value"): Promise<Container> { }
Ya.. that does work... and what I ended up using.
Just didn't expect the constant to not work
@woven igloo Do you actually need the default value, or are you just testing that its an optional field ?
It's because the introspector does not resolve the reference, this is super hard to do
I inject the default value to the graphQL schema yep
Yes it's normal, that's because the object is build on each runtime call, to keep the state clean we need this constructor notation.
I understand that resolving the value is almost impossible without running the code.
I would probably be nice to have a better error message though
At least this approach can work, if you need to set a class level constant #1287482178275639390 message
Better error messages and docs to show the happy path are definitely on the roadmap. Thanks, @leaden tartan 🙏
.. technically, you don't need to set the value in the constructor. You can just have it as a constant outside of the class itself.
There are so many "this doesn't work in 'dagger-flavoured typescript'" that it makes it a little annoying when you run into one of these
Yes, that's because the TS compiler analyse the static code, so it's super hard to resolve value by their reference
If you can't parse a default value, do you report it as optional, even if it's a non-null? That's what Python does. This allows Dagger to still make it optional and omit it, so that when the function is called in TypeScript, the default is used at runtime. If you're reporting it as optional and still not able to let TypeScript set the default at runtime, then there's something wrong with the way the function is being called.
In Typescript, we can also use the result of a function as a default value 😛