import type { ReaderWithRefetchQueries } from './entrypoint'; import { type ComponentOrFieldName, type StoreLink, } from './IsographEnvironment'; import type { PromiseWrapper } from './PromiseWrapper'; import type { StartUpdate } from './reader'; import { stableCopy } from './util'; // TODO type this better export type VariableValue = | string | number | boolean | null | { readonly [index: string]: VariableValue; } | VariableValue[]; export type Variables = { readonly [index: string]: VariableValue }; export type UnknownTReadFromStore = { parameters: object; data: object; startUpdate?: StartUpdate; }; export type ExtractData = T extends { data: infer D extends object; } ? D : never; export type ExtractParameters = T extends { parameters: infer P extends Variables; } ? P : Variables; export type ExtractStartUpdate = T['startUpdate']; export type ExtractUpdatableData = ExtractUpdatableDataFromStartUpdate>; export type ExtractUpdatableDataFromStartUpdate = T extends StartUpdate ? D : never; export type FragmentReference< TReadFromStore extends UnknownTReadFromStore, TClientFieldValue, > = { readonly kind: 'FragmentReference'; readonly readerWithRefetchQueries: PromiseWrapper< ReaderWithRefetchQueries >; readonly root: StoreLink; readonly fieldName: ComponentOrFieldName; readonly readerArtifactKind: | 'EagerReaderArtifact' | 'ComponentReaderArtifact'; // TODO we potentially stably copy and stringify variables a lot! // So, we should employ interior mutability: pretend that fragment reference // is immutable, but actually store something like // `Map` // and read or update that map when we would otherwise stably copy and // stringify. readonly variables: ExtractParameters; readonly networkRequest: PromiseWrapper; }; export type StableIdForFragmentReference = string; export function stableIdForFragmentReference( fragmentReference: FragmentReference, ): StableIdForFragmentReference { return `${fragmentReference.root.__typename}/${fragmentReference.root.__link}/${fragmentReference.fieldName}/${JSON.stringify(stableCopy(fragmentReference.variables))}`; }