diff --git a/integration-tests/lts/select.test.ts b/integration-tests/lts/select.test.ts index 5ae6d4b09..eeaa867e5 100644 --- a/integration-tests/lts/select.test.ts +++ b/integration-tests/lts/select.test.ts @@ -4,6 +4,7 @@ import * as $ from "../../packages/generate/src/syntax/reflection"; import e, { type $infer } from "./dbschema/edgeql-js"; import { setupTests, teardownTests, tc, type TestData } from "./setupTeardown"; + let client: edgedb.Client; let data: TestData; @@ -1358,8 +1359,8 @@ SELECT __scope_0_defaultPerson { name: true, id: true, })); - const heroShape = e.shape(e.Hero, () => ({ - villains: true, + const personShape = e.shape(e.Person, () => ({ + name: true, })); const villainShape = e.shape(e.Villain, () => ({ nemesis: true, @@ -1422,9 +1423,7 @@ SELECT __scope_0_defaultPerson { const cast = e.select(query, () => ({ characters: true })); const freeObjWithShape = e.select({ - heros: e.select(cast.characters.is(e.Hero), (h) => { - return heroShape(h); - }), + heros: e.select(cast.characters.is(e.Hero), personShape), villains: e.select(cast.characters.is(e.Villain), villainShape), }); type FreeObjWithShape = $infer; @@ -1432,7 +1431,7 @@ SELECT __scope_0_defaultPerson { tc.IsExact< FreeObjWithShape, { - heros: { villains: { id: string }[] }[]; + heros: { name: string }[]; villains: { nemesis: { id: string } | null }[]; } > diff --git a/packages/generate/src/syntax/select.ts b/packages/generate/src/syntax/select.ts index 8e9f21333..61f92dc3a 100644 --- a/packages/generate/src/syntax/select.ts +++ b/packages/generate/src/syntax/select.ts @@ -36,7 +36,6 @@ import type { ExclusiveTuple, orLiteralValue, EnumType, - assert_single, } from "./typesystem"; import { @@ -846,26 +845,38 @@ export const $existingScopes = new Set< Expression> >(); +const shapeSymbol = Symbol("portableShape"); + +export interface $Shape< + Element extends ObjectType, + SelectShape, + Card extends Cardinality = Cardinality.One, +> { + [shapeSymbol]: { + __element__: Element; + __cardinality__: Card; + __shape__: SelectShape; + }; +} + function $shape< Expr extends ObjectTypeExpression, Element extends Expr["__element__"], Shape extends objectTypeToSelectShape & SelectModifiers, + SelectCard extends ComputeSelectCardinality< + Expr, + Pick + >, Scope extends $scopify & $linkPropify<{ [k in keyof Expr]: k extends "__cardinality__" ? Cardinality.One : Expr[k]; }>, - ElementOfAnyShape extends Omit & { __shape__: any }, >( _expr: Expr, shape: (scope: Scope) => Readonly, -): ( - scope: Omit & { - __element__: ElementOfAnyShape; - assert_single(): assert_single; - }, -) => Readonly; +): ((scope: unknown) => Readonly) & $Shape; function $shape(_a: unknown, b: (...args: any) => any) { return b; } diff --git a/packages/generate/src/syntax/typesystem.ts b/packages/generate/src/syntax/typesystem.ts index deef0718d..fa6df6aac 100644 --- a/packages/generate/src/syntax/typesystem.ts +++ b/packages/generate/src/syntax/typesystem.ts @@ -10,13 +10,7 @@ import type { import { TypeKind } from "edgedb/dist/reflection/index"; import type { cardutil } from "./cardinality"; import type { Range, MultiRange } from "edgedb"; -import type { - ComputeSelectCardinality, - SelectModifierNames, - SelectModifiers, - normaliseShape, - objectTypeToSelectShape, -} from "./select"; +import type { $Shape, normaliseShape } from "./select"; ////////////////// // BASETYPE @@ -732,53 +726,17 @@ export type BaseTypeToTsType< ? computeObjectShape : never; -type shapeFnToTsType = ShapeFn extends ( - scope: infer Scope, -) => Readonly - ? Scope extends $scopify - ? Shape extends objectTypeToSelectShape & - SelectModifiers - ? computeTsType< - ObjectType< - Scope["__element__"]["__name__"], - Scope["__element__"]["__pointers__"], - normaliseShape - >, - ComputeSelectCardinality< - { - __element__: Scope["__element__"]; - // TODO: Drop this explicit cardinality in a breaking change version - // A previous implementation of this acted this way, so we need to - // keep it for compatibility. - __cardinality__: Cardinality.Many; - }, - Pick - > +export type setToTsType = + Set extends $Shape + ? Shape extends object + ? computeTsTypeCard< + computeObjectShape>, + Card > : never - : never - : never; - -type portableShapeToTsType = PortableShape extends ( - expr: infer Expr extends ObjectTypeExpression, - shape: (scope: unknown) => Readonly, -) => (scope: unknown) => void - ? Shape extends objectTypeToSelectShape & - SelectModifiers - ? computeTsType< - ObjectType< - Expr["__element__"]["__name__"], - Expr["__element__"]["__pointers__"], - normaliseShape - >, - ComputeSelectCardinality> - > - : never - : shapeFnToTsType; - -export type setToTsType = Set extends TypeSet - ? computeTsType - : portableShapeToTsType; + : Set extends TypeSet + ? computeTsType + : never; export type computeTsTypeCard = Cardinality extends C ? unknown