-
Notifications
You must be signed in to change notification settings - Fork 5
Fix reloading of disposed queries (#15) #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,13 @@ import { | |
| type IEnvironmentProvider, | ||
| loadQuery, | ||
| JSResourceReference, | ||
| PreloadOptions, | ||
| PreloadedQuery, | ||
| GraphQLTaggedNode, | ||
| PreloadableConcreteRequest, | ||
| PreloadOptions, | ||
| EnvironmentProviderOptions, | ||
| } from "react-relay"; | ||
| import type { LoaderFunction, LoaderFunctionArgs } from "react-router-dom"; | ||
| } from 'react-relay'; | ||
| import type { LoaderFunction, LoaderFunctionArgs, ShouldRevalidateFunction, ShouldRevalidateFunctionArgs } from "react-router-dom"; | ||
| import type { ComponentType } from "react"; | ||
|
|
||
| import type { | ||
|
|
@@ -16,9 +17,12 @@ import type { | |
| SimpleEntryPoint, | ||
| } from "./entry-point.types"; | ||
| import EntryPointRoute from "./EntryPointRoute"; | ||
| import type {EntryPointRouteObject} from './entry-point-route-object.types'; | ||
| import {OperationType} from 'relay-runtime'; | ||
|
|
||
| type EntryPointRouteProperties = { | ||
| loader: LoaderFunction; | ||
| shouldRevalidate: ShouldRevalidateFunction; | ||
| Component: ComponentType<Record<string, never>>; | ||
| }; | ||
|
|
||
|
|
@@ -29,17 +33,19 @@ export function createEntryPointRoute< | |
| entryPoint: | ||
| | SimpleEntryPoint<Component, PreloaderContext> | ||
| | JSResourceReference<SimpleEntryPoint<Component, PreloaderContext>>, | ||
| rest: Omit<EntryPointRouteObject, 'entryPoint'>, | ||
| environmentProvider: IEnvironmentProvider<never>, | ||
| contextProvider?: PreloaderContextProvider<PreloaderContext> | ||
| contextProvider?: PreloaderContextProvider<PreloaderContext>, | ||
| ): EntryPointRouteProperties { | ||
| let queries: {[p: string]: PreloadedQuery<OperationType, any>} | undefined = undefined; | ||
|
|
||
| async function loader(args: LoaderFunctionArgs): Promise<any> { | ||
| const loadedEntryPoint = | ||
| "load" in entryPoint ? await entryPoint.load() : entryPoint; | ||
| const { queries: queryArgs, ...props } = loadedEntryPoint.getPreloadProps({ | ||
| ...args, | ||
| preloaderContext: contextProvider?.getPreloaderContext() as any, | ||
| }); | ||
| let queries = undefined; | ||
| if (queryArgs) { | ||
| queries = Object.fromEntries( | ||
| Object.entries(queryArgs).map( | ||
|
|
@@ -80,8 +86,23 @@ export function createEntryPointRoute< | |
| }; | ||
| } | ||
|
|
||
| // This is needed to avoid cases where the query has been disposed of but the | ||
| // router would not normally revalidate and rerun the loader which is needed | ||
| // to reload the query. | ||
| // See https://github.com/loop-payments/react-router-relay/issues/15. | ||
|
Comment on lines
+89
to
+92
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I follow this part. My (definitely limited) understanding of
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docs certainly make it sound that way but it gets called as part of |
||
| function shouldRevalidate(args: ShouldRevalidateFunctionArgs): boolean { | ||
| for (let key in queries) { | ||
| const query = queries[key] | ||
| if (query.isDisposed) { | ||
| return true; | ||
| } | ||
| } | ||
| return rest.shouldRevalidate?.(args) ?? args.defaultShouldRevalidate; | ||
| } | ||
|
|
||
| return { | ||
| loader, | ||
| shouldRevalidate, | ||
| Component: EntryPointRoute(entryPoint), | ||
| }; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't get this to compile. It would fail saying that it has the type
anybecause it referred to itself.