Skip to content

Commit b74466b

Browse files
avalletesoedirgo
authored andcommitted
fix(types): spread and embeding combination issues
1 parent 2352eb1 commit b74466b

File tree

3 files changed

+1158
-10
lines changed

3 files changed

+1158
-10
lines changed

src/select-query-parser/result.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ export type ProcessRPCNode<
130130
Row extends Record<string, unknown>,
131131
RelationName extends string,
132132
NodeType extends Ast.Node
133-
> = NodeType extends Ast.StarNode // If the selection is *
133+
> = NodeType['type'] extends Ast.StarNode['type'] // If the selection is *
134134
? Row
135-
: NodeType extends Ast.FieldNode
136-
? ProcessSimpleField<Row, RelationName, NodeType>
137-
: SelectQueryError<'Unsupported node type.'>
135+
: NodeType['type'] extends Ast.FieldNode['type']
136+
? ProcessSimpleField<Row, RelationName, Extract<NodeType, Ast.FieldNode>>
137+
: SelectQueryError<'RPC Unsupported node type.'>
138138
/**
139139
* Process select call that can be chained after an rpc call
140140
*/
@@ -197,12 +197,12 @@ export type ProcessNode<
197197
RelationName extends string,
198198
Relationships extends GenericRelationship[],
199199
NodeType extends Ast.Node
200-
> = NodeType extends Ast.StarNode // If the selection is *
200+
> = NodeType['type'] extends Ast.StarNode['type'] // If the selection is *
201201
? Row
202-
: NodeType extends Ast.SpreadNode // If the selection is a ...spread
203-
? ProcessSpreadNode<Schema, Row, RelationName, Relationships, NodeType>
204-
: NodeType extends Ast.FieldNode
205-
? ProcessFieldNode<Schema, Row, RelationName, Relationships, NodeType>
202+
: NodeType['type'] extends Ast.SpreadNode['type'] // If the selection is a ...spread
203+
? ProcessSpreadNode<Schema, Row, RelationName, Relationships, Extract<NodeType, Ast.SpreadNode>>
204+
: NodeType['type'] extends Ast.FieldNode['type']
205+
? ProcessFieldNode<Schema, Row, RelationName, Relationships, Extract<NodeType, Ast.FieldNode>>
206206
: SelectQueryError<'Unsupported node type.'>
207207

208208
/**
@@ -369,7 +369,9 @@ type ProcessSpreadNode<
369369
/**
370370
* Helper type to process the result of a spread node.
371371
*/
372-
type ProcessSpreadNodeResult<Result> = ExtractFirstProperty<Result> extends infer SpreadedObject
372+
type ProcessSpreadNodeResult<Result> = Result extends Record<string, SelectQueryError<string>>
373+
? Result
374+
: ExtractFirstProperty<Result> extends infer SpreadedObject
373375
? ContainsNull<SpreadedObject> extends true
374376
? Exclude<{ [K in keyof SpreadedObject]: SpreadedObject[K] | null }, null>
375377
: Exclude<{ [K in keyof SpreadedObject]: SpreadedObject[K] }, null>

test/select-query-parser/result.test-d.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { selectParams } from '../relationships'
33
import { GetResult } from '../../src/select-query-parser/result'
44
import { expectType } from 'tsd'
55
import { TypeEqual } from 'ts-expect'
6+
import { SelectQueryError } from '../../src/select-query-parser/utils'
67

78
type SelectQueryFromTableResult<
89
TableName extends keyof Database['public']['Tables'],
@@ -70,3 +71,18 @@ type SelectQueryFromTableResult<
7071
}
7172
expectType<TypeEqual<typeof result, typeof expected>>(true)
7273
}
74+
75+
// nested query with selective fields
76+
{
77+
let result: SelectQueryFromTableResult<
78+
'users',
79+
`msgs:messages(id, ...message_details(created_at, channel!inner(id, slug, owner:users(*))))`
80+
>
81+
let expected: {
82+
msgs: {
83+
id: number
84+
message_details: SelectQueryError<`Could not embed because more than one relationship was found for 'messages' and '${string}' you need to hint the column with messages!<columnName> ?`>
85+
}[]
86+
}
87+
expectType<TypeEqual<typeof result, typeof expected>>(true)
88+
}

0 commit comments

Comments
 (0)