Skip to content

Commit 71aadd6

Browse files
committed
Add more complete types for the InternalModelManager
The InternalModelManager is public API for Gadget developers, so it should be well documented and have nice types for use inside the editor. We had `any`s everywhere when we can instead have nice types! This adds those.
1 parent ddc7ba1 commit 71aadd6

File tree

6 files changed

+627
-104
lines changed

6 files changed

+627
-104
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const Hydrators = {
66

77
export type Hydration = keyof typeof Hydrators;
88

9+
/** Instructions for a client to turn raw transport types (like strings) into useful client side types (like Dates). Unstable and not intended for developer use. */
910
export interface HydrationPlan {
1011
[key: string]: Hydration;
1112
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import type { Jsonify } from "type-fest";
44
import type { GadgetRecord, RecordShape } from "./GadgetRecord.js";
55
import type { InternalModelManager } from "./InternalModelManager.js";
66
import type { AnyModelManager } from "./ModelManager.js";
7-
import type { PaginationOptions } from "./operationBuilders.js";
87
import { GadgetClientError, GadgetOperationError } from "./support.js";
8+
import { PaginateOptions } from "./types.js";
99

1010
type PaginationConfig = {
1111
pageInfo: { hasNextPage: boolean; hasPreviousPage: boolean; startCursor: string; endCursor: string };
12-
options?: PaginationOptions;
12+
options?: PaginateOptions;
1313
};
1414

1515
/** Represents a list of objects returned from the API. Facilitates iterating and paginating. */

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

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
hydrateRecord,
1616
hydrateRecordArray,
1717
} from "./support.js";
18+
import type { InternalFieldSelection, InternalFindListOptions, InternalFindManyOptions, InternalFindOneOptions } from "./types";
1819

1920
const internalErrorsDetails = `
2021
fragment InternalErrorsDetails on ExecutionError {
@@ -51,87 +52,6 @@ export const internalFindOneQuery = (apiIdentifier: string) => {
5152
`;
5253
};
5354

54-
/**
55-
* A list of fields to select from the internal API
56-
* Matches the format of the Public API `select` option, but only allows going one level deep -- no relationships can be selected using the internal API.
57-
*
58-
* Supports passing a list of strings as a shorthand.
59-
*
60-
* @example
61-
* { fieldA: true, fieldB: true, fieldC: false }
62-
*
63-
* @example
64-
* ['fieldA', 'fieldB']
65-
*/
66-
export type InternalFieldSelection = string[] | { [field: string]: boolean | null | undefined };
67-
68-
/** Options for the api functions that return one record on an InternalModelManager */
69-
export interface InternalFindOneOptions {
70-
/**
71-
* What fields to retrieve from the API for this API call
72-
**/
73-
select?: InternalFieldSelection;
74-
}
75-
76-
/** Options for functions that query a list of records on an InternalModelManager */
77-
export interface InternalFindListOptions {
78-
/**
79-
* A string to search for within all the stringlike fields of the records
80-
* Matches the behavior of the Public API `search` option
81-
**/
82-
search?: string;
83-
/**
84-
* How to sort the returned records
85-
* Matches the format and behavior of the Public API `sort` option
86-
*
87-
* @example
88-
* {
89-
* sort: { publishedAt: "Descending" }
90-
* }
91-
**/
92-
sort?: Record<string, "Ascending" | "Descending"> | Record<string, "Ascending" | "Descending">[];
93-
/**
94-
* Only return records matching this filter
95-
* Matches the format and behavior of the Public API `filter` option
96-
*
97-
* @example
98-
* {
99-
* filter: { published: { equals: true } }
100-
* }
101-
* */
102-
filter?: Record<string, any>;
103-
/**
104-
* What fields to retrieve from the API for this API call
105-
**/
106-
select?: InternalFieldSelection;
107-
}
108-
109-
/** Options for functions that return a paginated list of records from an InternalModelManager */
110-
export interface InternalFindManyOptions extends InternalFindListOptions {
111-
/**
112-
* A count of records to return
113-
* Often used in tandem with the `after` option for GraphQL relay-style cursor pagination
114-
* Matches the pagination style and behavior of the Public API
115-
**/
116-
first?: number;
117-
/**
118-
* The `after` cursor from the GraphQL Relay pagination spec
119-
* Matches the pagination style and behavior of the Public API
120-
**/
121-
after?: string;
122-
/**
123-
* A count of records to return
124-
* Often used in tandem with the `before` option for GraphQL relay-style cursor pagination
125-
* Matches the pagination style and behavior of the Public API
126-
**/
127-
last?: number;
128-
/**
129-
* The `before` cursor from the GraphQL Relay pagination spec
130-
* Matches the pagination style and behavior of the Public API
131-
**/
132-
before?: string;
133-
}
134-
13555
const internalFindListVariables = (capitalizedApiIdentifier: string, options?: InternalFindListOptions) => {
13656
return {
13757
search: options?.search ? Var({ value: options?.search, type: "String" }) : undefined,

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { FieldSelection as BuilderFieldSelection, BuilderOperation, Variabl
22
import { Call, Var, compileWithVariableValues } from "tiny-graphql-query-compiler";
33
import type { FieldSelection } from "./FieldSelection.js";
44
import { filterTypeName, sortTypeName } from "./support.js";
5-
import type { VariablesOptions } from "./types.js";
5+
import type { FindManyOptions, SelectionOptions, VariablesOptions } from "./types.js";
66

77
const hydrationOptions = (modelApiIdentifier: string): BuilderFieldSelection => {
88
return {
@@ -21,22 +21,7 @@ const fieldSelectionToQueryCompilerFields = (selection: FieldSelection, includeT
2121
return output;
2222
};
2323

24-
type AnySort = any;
25-
type AnyFilter = any;
26-
27-
export type SelectionOptions = { select?: any };
28-
29-
export type PaginationOptions = {
30-
sort?: AnySort | null;
31-
filter?: AnyFilter | null;
32-
search?: string | null;
33-
after?: string | null;
34-
first?: number | null;
35-
before?: string | null;
36-
last?: number | null;
37-
} & SelectionOptions;
38-
39-
export type FindFirstPaginationOptions = Omit<PaginationOptions, "first" | "last" | "before" | "after">;
24+
export type FindFirstPaginationOptions = Omit<FindManyOptions, "first" | "last" | "before" | "after">;
4025

4126
export const findOneOperation = (
4227
operation: string,
@@ -80,7 +65,7 @@ export const findManyOperation = (
8065
operation: string,
8166
defaultSelection: FieldSelection,
8267
modelApiIdentifier: string,
83-
options?: PaginationOptions
68+
options?: FindManyOptions
8469
) => {
8570
return compileWithVariableValues({
8671
type: "query",

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { GadgetConnection } from "./GadgetConnection.js";
33
import type { GadgetRecord, RecordShape } from "./GadgetRecord.js";
44
import { GadgetRecordList } from "./GadgetRecordList.js";
55
import type { AnyModelManager } from "./ModelManager.js";
6-
import type { PaginationOptions, SelectionOptions } from "./operationBuilders.js";
76
import {
87
actionOperation,
98
findManyOperation,
@@ -23,7 +22,7 @@ import {
2322
hydrateRecord,
2423
hydrateRecordArray,
2524
} from "./support.js";
26-
import type { VariablesOptions } from "./types.js";
25+
import type { FindManyOptions, SelectionOptions, VariablesOptions } from "./types.js";
2726

2827
export const findOneRunner = async <Shape extends RecordShape = any>(
2928
modelManager: { connection: GadgetConnection },
@@ -67,7 +66,7 @@ export const findManyRunner = async <Shape extends RecordShape = any>(
6766
operation: string,
6867
defaultSelection: FieldSelection,
6968
modelApiIdentifier: string,
70-
options?: PaginationOptions,
69+
options?: FindManyOptions,
7170
throwOnEmptyData?: boolean
7271
) => {
7372
const plan = findManyOperation(operation, defaultSelection, modelApiIdentifier, options);

0 commit comments

Comments
 (0)