Created
March 28, 2021 18:05
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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