groq-js icon indicating copy to clipboard operation
groq-js copied to clipboard

Throw on missing parameters

Open rexxars opened this issue 3 years ago • 1 comments

I expected the following to throw an error:

let query = `name == $name`
let tree = parse(query)
let value = await evaluate(tree, {dataset: [], params: {}})
let data = await value.get()

...but it actually results in true

I think it'd be better if undefined parameters would throw an error:

Param $name referenced, but not provided

rexxars avatar Oct 18 '20 20:10 rexxars

@rexxars The current behavior actually seems right to me. Check out this example which might help explain:

let {parse, evaluate} = require("groq-js")
let query = `constructor.name == $name`
let tree = parse(query)
let value = await evaluate(tree, {dataset: [], params: {$name: 'Array'}})
let data = await value.get();

Note that your query does not have a top-level projection * [...], so you're effectively asking whether [].name == undefined, which is true. If you set query to * [name == $name] then you get an empty array back which also matches expectations.

Your suggestion definitely seems like it would be useful, so it would be nice to have a strictParameters: true setting on the options object in evaluate which would trigger errors for missing params.

ryaninvents avatar Feb 26 '21 19:02 ryaninvents