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

Querybuilder generates invalid edgeql for invalid insert shape

Open jaclarke opened this issue 2 years ago • 3 comments

From issue: https://github.com/edgedb/edgedb/issues/3647

If function returning a shape object is used instead of plain shape object in e.insert:

e.insert(e.Person, () => ({
  firstName: 'john',
  lastName: 'smith'
}))

querybuilder generates the following incorrect edgeql:

INSERT default::Person {
  id
}

Typescript does catch this mistake, but we should still check at runtime and throw specific error instead of generating incorrect edgeql.

jaclarke avatar Mar 23 '22 13:03 jaclarke

I was also trying to insert a blank Person with the following query, not sure if this is related. But it is the same error.

    await e.insert(e.Person, {}).run(client)

    InternalServerError: invalid reference to FROM-clause entry for table "Person~1"

internalfx avatar Mar 23 '22 13:03 internalfx

Typescript does catch this mistake, but we should still check at runtime and throw specific error instead of generating incorrect edgeql.

It does not seem to catch the error for me on Typescript, the following does not throw TS errors:

// This is valid, even though the shape doesn't even match.
const query = e.insert(e.User, () => ({
  name: "Hello",
  this_does_not_exist: "x",
}));

const result = await query.run(client); // <{ id: string; }>

Using edgedb-js 0.19.15, Typescript 4.6.2 and client is freshly generated using --target cjs. Am I missing an use case for inserts that return a shape from a function?

Sikarii avatar Mar 23 '22 13:03 Sikarii

I was also trying to insert a blank Person with the following query, not sure if this is related. But it is the same error.

Yep, that should be valid, but the querybuilder also mistakenly injects an id into the shape here as well.

jaclarke avatar Mar 23 '22 13:03 jaclarke

This seems to be fixed in the latest version and has test coverage.

scotttrinh avatar May 02 '23 14:05 scotttrinh