Skip to content

Instantly share code, notes, and snippets.

@arackaf
Created June 24, 2019 06:05
Show Gist options
  • Save arackaf/ed04b8ca1a65318fba0eb38f37d75641 to your computer and use it in GitHub Desktop.
Save arackaf/ed04b8ca1a65318fba0eb38f37d75641 to your computer and use it in GitHub Desktop.
export type BasicQuery<T extends keyof Query> = Pick<Query, T>;
export type AliasQuery<T extends Record<string, keyof Query>> = { [k in keyof T]: Query[T[k]] };
export type CombinedQuery<T extends keyof Query, U extends Record<string, keyof Query>> = BasicQuery<T> & AliasQuery<U>;
const justTesting1 = useQuery<BasicQuery<"allBooks" | "allSubjects">>(null);
const { data: data1 } = justTesting1;
data1.allSubjects;
const justTesting2 = useQuery<AliasQuery<{ sub: "allSubjects"; sub2: "allSubjects" }>>(null);
const { data: data2 } = justTesting2;
data2.sub2;
const justTesting3 = useQuery<CombinedQuery<"allBooks" | "allLabelColors", { sub: "allSubjects"; sub2: "allSubjects" }>>(null);
const { data: data3 } = justTesting3;
data3.sub;
data3.allBooks;
type GetIt<T> = T extends never
? {}
: T extends keyof Query
? Pick<Query, T>
: T extends Record<string, keyof Query>
? { [k in keyof T]: Query[T[k]] }
: never;
export type MasterQuery<T extends keyof Query | Record<string, keyof Query>, U extends keyof Query | Record<string, keyof Query> = never> = GetIt<T> &
GetIt<U>;
const justTesting1a = useQuery<MasterQuery<"allBooks" | "allSubjects">>(null);
const { data: data1a } = justTesting1a;
data1a.allSubjects;
const justTesting2a = useQuery<MasterQuery<{ sub: "allSubjects"; sub2: "allSubjects" }>>(null);
const { data: data2a } = justTesting2a;
data2a.sub2;
const justTesting3a = useQuery<MasterQuery<"allBooks" | "allLabelColors", { sub: "allSubjects"; sub2: "allSubjects" }>>(null);
const { data: data3a } = justTesting3a;
data3a.sub;
data3a.allBooks;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment