-
Notifications
You must be signed in to change notification settings - Fork 8
Give records a reference to the model manager which loaded them #754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c07859e
9793b6a
4322f79
026a840
c1dfa0d
2adc8fc
3da5862
c2a37ba
c0f645e
bb79861
3700e0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import type { GadgetConnection } from "./GadgetConnection.js"; | ||
import type { FindFirstFunction, FindManyFunction, FindOneFunction, GetFunction } from "./GadgetFunctions.js"; | ||
import type { GadgetRecord } from "./GadgetRecord.js"; | ||
import type { InternalModelManager } from "./InternalModelManager.js"; | ||
|
||
export type AnyModelFinderMetadata = { | ||
/** The name of the GraphQL API field that should be called for this operation */ | ||
operationName: string; | ||
/** The model's api identifier */ | ||
modelApiIdentifier: string; | ||
/** What fields to select from the GraphQL API if no explicit selection is passed */ | ||
defaultSelection: Record<string, any>; | ||
/** A namespace this operation is nested in. Absent for old clients or root-namespaced operations */ | ||
namespace?: string | string[] | null; | ||
/** Type-time only type member used for strong typing of finders */ | ||
selectionType: any; | ||
/** Type-time only type member used for strong typing of finders */ | ||
optionsType: any; | ||
/** Type-time only type member used for strong typing of finders */ | ||
schemaType: any | null; | ||
}; | ||
|
||
export type AnyFindOneFunc = FindOneFunction<any, any, any, any>; | ||
export type AnyFindManyFunc = FindManyFunction<any, any, any, any>; | ||
export type AnyFindFirstFunc = FindFirstFunction<any, any, any, any>; | ||
|
||
/** | ||
* The manager class for a given model that uses the Public API, like `api.post` or `api.user` | ||
**/ | ||
export interface AnyPublicModelManager< | ||
FindOneFunc extends AnyFindOneFunc = AnyFindOneFunc, | ||
FindManyFunc extends AnyFindManyFunc = AnyFindManyFunc, | ||
FindFirstFunc extends AnyFindFirstFunc = AnyFindFirstFunc | ||
> { | ||
connection: GadgetConnection; | ||
findOne: FindOneFunc; | ||
findMany: FindManyFunc; | ||
findFirst: FindFirstFunc; | ||
maybeFindFirst(options: any): Promise<GadgetRecord<any> | null>; | ||
maybeFindOne(id: string, options: any): Promise<GadgetRecord<any> | null>; | ||
} | ||
|
||
/** | ||
* The manager class for a given single model that uses the Public API, like `api.session` | ||
**/ | ||
export interface AnyPublicSingletonModelManager<GetFunc extends GetFunction<any, any, any, any> = GetFunction<any, any, any, any>> { | ||
connection: GadgetConnection; | ||
get: GetFunc; | ||
} | ||
|
||
/** | ||
* Prior to 1.1 actions were defined to accept just a connection | ||
*/ | ||
export interface AnyLegacyModelManager { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to address a test case inside |
||
connection: GadgetConnection; | ||
} | ||
|
||
/** | ||
* Any model manager, either public or internal | ||
*/ | ||
export type AnyModelManager = AnyPublicModelManager | AnyPublicSingletonModelManager | AnyLegacyModelManager | InternalModelManager; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kind of unfortunate but addresses an issue with
useSession
anduseAuth
typing. For both these react hooks we accept a type that is:js-clients/packages/react/src/auth/useAuth.ts
Lines 21 to 22 in e3773e9
Which scopes the client down to one that has both
user
findMany
function and asession
with aget
function. This is narrowing the type of theModelManager
and the resulting object is then typed to be aGadgetRecord
based on the input client/model manager. In order to make it play nicely with hydration and keeping the reference to the model manager I want to also add theAnyPublicModelManager
orAnyPublicSingletonModelManager
interface too. I had changed it to be:however this leads to an infinite typing/complexity error getting the output
GadgetRecord
type. To address this I have added these optional generic typings to allow the function typing to be passed through. This way theget
andfindMany
function types can be passed through and the resulting type can be inferred again.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awkward but this makes sense