Skip to content

Commit 7c72c7e

Browse files
committed
Improve support types by making them dynamic with a derived type (and i want to use this dervive-er elsewhere)
1 parent dee632b commit 7c72c7e

File tree

3 files changed

+43
-35
lines changed

3 files changed

+43
-35
lines changed

packages/api-client-core/spec/TestSchema.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { AvailableSelection } from "../src/types.js";
2+
13
export type NestedThing = {
24
bool: boolean;
35
string: string;
@@ -45,34 +47,4 @@ export type TestSchema = {
4547
};
4648
};
4749

48-
export type AvailableTestSchemaSelection = {
49-
num?: boolean | null | undefined;
50-
str?: boolean | null | undefined;
51-
obj?: {
52-
test?: boolean | null | undefined;
53-
bool?: boolean | null | undefined;
54-
};
55-
optionalObj?: {
56-
test?: boolean | null | undefined;
57-
bool?: boolean | null | undefined;
58-
};
59-
list?: {
60-
title: boolean | null | undefined;
61-
};
62-
optionalList?: {
63-
title: boolean | null | undefined;
64-
};
65-
nested?: NestedThing;
66-
someConnection?: {
67-
edges?: {
68-
node?: {
69-
id: boolean | null | undefined;
70-
state: boolean | null | undefined;
71-
};
72-
};
73-
pageInfo?: {
74-
hasNextPage?: boolean | null | undefined;
75-
hasPreviousPage?: boolean | null | undefined;
76-
};
77-
};
78-
};
50+
export type AvailableTestSchemaSelection = AvailableSelection<TestSchema>;

packages/api-client-core/src/ModelManager.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@ import type { GadgetConnection } from "./GadgetConnection.js";
22
import type { GadgetRecord } from "./GadgetRecord.js";
33
import type { GadgetRecordList } from "./GadgetRecordList.js";
44

5+
export type AnyModelFinderMetadata = {
6+
operationName: string;
7+
modelApiIdentifier: string;
8+
defaultSelection: Record<string, any>;
9+
selectionType: any;
10+
optionsType: any;
11+
schemaType: any | null;
12+
};
13+
514
/**
615
* Object representing one model's API in a high level way
716
* This is a generic interface. Concrete ones are generated by Gadget, */
817
export interface AnyModelManager {
918
connection: GadgetConnection;
10-
findOne(id: string, options: any): Promise<GadgetRecord<any>>;
11-
maybeFindOne(id: string, options: any): Promise<GadgetRecord<any> | null>;
12-
findMany(options: any): Promise<GadgetRecordList<any>>;
13-
findFirst(options: any): Promise<GadgetRecord<any>>;
19+
findOne: ((id: string, options: any) => Promise<GadgetRecord<any>>) & AnyModelFinderMetadata;
20+
findMany: ((options: any) => Promise<GadgetRecordList<any>>) & AnyModelFinderMetadata;
21+
findFirst: ((options: any) => Promise<GadgetRecord<any>>) & AnyModelFinderMetadata;
1422
maybeFindFirst(options: any): Promise<GadgetRecord<any> | null>;
23+
maybeFindOne(id: string, options: any): Promise<GadgetRecord<any> | null>;
1524
}

packages/api-client-core/src/types.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,30 @@ export type PaginateOptions = {
697697
last?: number | null;
698698
select?: AnySelection | InternalFieldSelection | null;
699699
};
700+
701+
/**
702+
* Convert a schema type into the type that a selection of it must extend
703+
*
704+
* Example Schema:
705+
*
706+
* {
707+
* foo: boolean;
708+
* bar?: string;
709+
* nested?: {
710+
* count: number
711+
* }
712+
* }
713+
*
714+
* Example available selection:
715+
*
716+
* {
717+
* foo?: boolean | null | undefined;
718+
* bar?: boolean | null | undefined;
719+
* nested?: {
720+
* count: boolean | null | undefined
721+
* }
722+
* }
723+
*/
724+
export type AvailableSelection<Schema> = Schema extends string | number | bigint | null | undefined
725+
? boolean | null | undefined
726+
: { [key in keyof Schema]?: AvailableSelection<Schema[key]> };

0 commit comments

Comments
 (0)