-
Notifications
You must be signed in to change notification settings - Fork 28
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
Dependant resources #46
Comments
That's a good pattern, I am using it one project:
In my case it is resolved via declarative dependencies of a resource, something like export const dependentOnAccountResource = createResource({
type: "DEPENDENT",
getKey: () => "dependent",
- resources: [accountResource],
+ dependencies: [
+ /*resource*/accountResource,
+ /*url param*/'queryParamName',
+ /*random selector*/(routerContext) => routerContext.params.paramName
+ ],
getData: async (accountResourceResponse, queryParam, param) => {
- const [{ data }] = useResource(accountResource); // useResource is a react hook, it's would be very hacky to use it here
return await fetch(`/feature_dependent_on_account_config/${accountResourceResponse.account.id}`)
},
}) However, that would be a breaking change for RRR, so - probably not the best idea. I hope there are better way to compose not depend. |
@theKashey Yes, defined wasn’t suggesting we actually use a hook within the resource body! I’d be interested to see more about how your declarative example works in practice. Thinking about this a little more and it would relatively easy to compose a fetcher that was memoized using the value from the route params, which would mean you could create an implicit relationship between two resources by sharing that fetcher. It feels to me like it would be a nice additional to have something out of the box that allowed that sort of behaviour, even if it were a little manual (passing the router context around). |
Oh, and I realise that it would be a lot more difficult to do the sort of composition outside the library in the case of trying to support derived state from a call to an |
Long story short - to fullfill your request we have to ensure only two moments:
Nowadays the second is controlled via
The final API might look as following: export const dependentOnAccountResource = createResource({
type: "DEPENDENT",
- getKey: () => "dependent",
- resources: [accountResource],
+ dependencies: [
+ /*resource*/accountResource,
+ /*url param*/'queryParamName',
+ /*random selector*/(routerContext) => routerContext.params.paramName
+ ],
+ mapDependenciesToState:([accountResourceResponse, queryParam, param], routerState) => (
+ // using well known name for example
+ return `/feature_dependent_on_account_config/${accountResourceResponse.account.id}`
+ }
+ guard: (state, resources, routerState) => routerState.featureFlags.enabled && accountResourceResponse.accoind.id%2 === 0,
// as I've said -> only even accounts (or it's only odd?)
+
getData: async (state) => {
// no logic here. This is an executor or a changed state
return await fetch(state)
},
}) An open question is the location of a |
Yep, right, that makes sense to me. Thanks for outlining it in a bit more detail! |
Did anything like this ever get merged into |
Having used this a little more recently, one of the things that I’ve come across a few times is that I’d like to have resources that are dependent on the result of another resource.
Something like:
This exact structure will fail obviously (not least because
data
will be null) but I wondered if it was something that felt possible/useful in the scope of this library. The reason I like something like it is that means:Would be interested to know if you’d felt similar pains, and if you’d solved them in other ways (or if I’ve missed something that means this is already easy). Thanks!
The text was updated successfully, but these errors were encountered: