-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support flattened result structure
- Loading branch information
Showing
7 changed files
with
148 additions
and
27 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { DataConnectQueryClient } from "./query-client"; | ||
export { useConnectQuery } from "./useConnectQuery"; | ||
export { useConnectMutation } from "./useConnectMutation"; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { | ||
QueryClient, | ||
QueryKey, | ||
type FetchQueryOptions, | ||
} from "@tanstack/react-query"; | ||
import type { FirebaseError } from "firebase/app"; | ||
import type { FlattenedQueryResult } from "./types"; | ||
import { executeQuery, QueryRef, QueryResult } from "firebase/data-connect"; | ||
|
||
type DataConnectQueryOptions<Data, Variables> = Omit< | ||
FetchQueryOptions< | ||
FlattenedQueryResult<Data, Variables>, | ||
FirebaseError, | ||
FlattenedQueryResult<Data, Variables>, | ||
QueryKey | ||
>, | ||
"queryFn" | "queryKey" | ||
> & { | ||
queryRef: QueryRef<Data, Variables>; | ||
queryKey?: QueryKey; | ||
}; | ||
|
||
export class DataConnectQueryClient extends QueryClient { | ||
prefetchDataConnectQuery<Data extends Record<string, any>, Variables>( | ||
refOrResult: QueryRef<Data, Variables> | QueryResult<Data, Variables>, | ||
options?: DataConnectQueryOptions<Data, Variables> | ||
) { | ||
let queryRef: QueryRef<Data, Variables>; | ||
let initialData: FlattenedQueryResult<Data, Variables> | undefined; | ||
|
||
if ("ref" in refOrResult) { | ||
queryRef = refOrResult.ref; | ||
initialData = { | ||
...refOrResult.data, | ||
ref: refOrResult.ref, | ||
source: refOrResult.source, | ||
fetchTime: refOrResult.fetchTime, | ||
}; | ||
} else { | ||
queryRef = refOrResult; | ||
} | ||
|
||
return this.prefetchQuery< | ||
FlattenedQueryResult<Data, Variables>, | ||
FirebaseError, | ||
FlattenedQueryResult<Data, Variables>, | ||
QueryKey | ||
>({ | ||
...options, | ||
initialData, | ||
queryKey: options?.queryKey ?? [ | ||
queryRef.name, | ||
queryRef.variables || null, | ||
], | ||
queryFn: async () => { | ||
const response = await executeQuery(queryRef); | ||
|
||
const data = { | ||
...response.data, | ||
ref: response.ref, | ||
source: response.source, | ||
fetchTime: response.fetchTime, | ||
}; | ||
|
||
// Ensures no serialization issues with undefined values | ||
return JSON.parse(JSON.stringify(data)); | ||
}, | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { MutationResult, QueryResult } from "firebase/data-connect"; | ||
|
||
// Flattens a QueryResult data down into a single object. | ||
// This is to prevent query.data.data, and also expose additional properties. | ||
export type FlattenedQueryResult<Data, Variables> = Omit< | ||
QueryResult<Data, Variables>, | ||
"data" | "toJSON" | ||
> & | ||
Data; | ||
|
||
export type FlattenedMutationResult<Data, Variables> = Omit< | ||
MutationResult<Data, Variables>, | ||
"data" | "toJSON" | ||
> & | ||
Data; |
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
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