Skip to content

Instantly share code, notes, and snippets.

@davidmwhynot
Created March 28, 2021 18:05
Show Gist options
  • Save davidmwhynot/33733d92da0459be56b9dc4bf3c718e8 to your computer and use it in GitHub Desktop.
Save davidmwhynot/33733d92da0459be56b9dc4bf3c718e8 to your computer and use it in GitHub Desktop.
Proof of concept for extracting a the query created by a generated document node.
type Query<TData> = {
data: TData;
};
type QueryOne = {
queryOne: {
foo: string;
};
};
type QueryTwo = {
queryTwo: {
foos: string[];
};
};
const queryOne: QueryOne = { queryOne: { foo: 'bar' } };
const queryTwo: QueryTwo = { queryTwo: { foos: ['a', 'b', 'c'] } };
const dictionary = { queryOne, queryTwo };
const map = new Map(Object.entries(dictionary));
type QueryResult<TData = any> = {
data?: TData | undefined;
};
type TypedDocument<Result = { [key: string]: any }> = {
key: string;
__resultType?: Result;
};
const useQuery = <TData>(query: TypedDocument<TData>): QueryResult<TData> => ({
data: (map.get(query.key) as unknown) as TData | undefined
});
const extractQueryProperty = <TData, TKey extends keyof TData>(
query: TypedDocument<TData>,
key: TKey
): TData[TKey] | null => {
const { data } = useQuery(query);
if(!data) {
return null;
}
return data[key];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment