-
Notifications
You must be signed in to change notification settings - Fork 31
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
Best way to use local client schema in prepareVariables? #526
Comments
Another approach would be |
Related to #297 – we don't currently support async We could do it, but I don't really know of a good way to automatically "subscribe" to variables in the route. We don't really have a great concept of route "effects" that would let you do this in a way that's coupled to the route, rather than to the page component, though – but if I were to approach this, I'd subscribe to the relevant store entry in the component, and call |
Thanks for the suggestion, @taion. I realized that because I'm using the pagination container, for my particular use case it makes more sense to rely on
<Route
...
Component={MyComponent}
prepareVariables={(_params, { context: { environment } }) => {
const request = getRequest(graphql`
query routes_searchQuery_Query {
... on Query {
__typename
}
searchQuery {
query
selectedFacets {
id
type
}
}
}
`);
const operation = createOperationDescriptor(request);
const {
searchQuery: { query, selectedFacets },
} = environment.lookup(operation.fragment, operation).data;
return {
query,
selectedSelectors: selectedFacets,
};
}}
/>
const MyComponent = createPaginationContainer(
MyComponentInner,
{
search: graphql`
fragment MyComponent_search on Query {
searchQuery {
query
selectedFacets {
id
}
}
...
}
`
}
)
const MyComponentInner = ({ relay, search: { query, selectedFacets } }) => {
// Update search results when the query or filters change
useEffect(
() => {
relay.refetchConnection(20, null, {
query,
facets: selectedFacets,
});
},
[query, relay, selectedFacets]
);
} One could do something similar using a refetch container, as well, when dealing with non-connection queries. |
That But, yeah, if you're using a container that can refetch, your approach there looks good. |
Ahaha. Thanks for the feedback :-) |
I'm trying to provide variables inside my
prepareVariables
function from Relay's store, but I'm not sure how to do this. Here was my first shot:(I'm hydrating my Relay store beforehand so that my
searchQuery { selectedFacets }
query returns something.)This almost works. The main problem is that, for whatever reason, returning a promise causes
prepareVariables
to return nothing during SSR. I don't have any problems on my browser (SSR is running on Node 10, so async/await is supported.)Another problem is that using
fetchQuery
on a local schema causes Relay compiler to fail. This workaround works, but it causes an annoying error. @sibelius suggested a different workaround using hooks, but that doesn't apply here sinceprepareVariables
isn't a React component.Lastly, I'm not sure of how to make the
QueryRenderer
re-render when there's a change to the variables that I'm getting from the store viaprepareVariables
. I want it such that it will re-run the query if the results ofsearchQuery { selectedFacets }
changes in any way; or even better, if I could specify a comparison function to control exactly when it re-renders.Has anyone else dealt with this use case? Any suggestions would be greatly appreciated!
The text was updated successfully, but these errors were encountered: