-
Notifications
You must be signed in to change notification settings - Fork 787
Skip & data.refetch #289
Comments
I agree this could be made more helpful. I may be missing something in the react apollo docs, so I've opened a SO question: http://stackoverflow.com/questions/40296800/how-do-you-dynamically-control-react-apollo-client-query-initiation Ideally, it'd be great to be able to dynamically control when a query is initiated. Apparently skip is not evaluated in response to prop updates. For example, I have a map which loads different types of data. I'd like to only load one type on component mount. At the moment, if I wrap the map component in multiple apollo containers, all data types are loaded on mount or prop change. It would be handy to have a way to prevent the initial query for each wrapping container from being fired - but to be able to trigger it later, say in response to user input. |
@bkniffler, @heysailor it currently is by design because the idea is you can't refetch something not fetched? I can see the usefulness in supporting it though! Would you be able to open a PR? |
It would be also awesome if we can pass some option, for example |
That's a great idea. Two new would handle it:
|
If you want that level of control over when the query runs etc, I wonder if you should just use |
Yes, that's what I'm doing currently. But it seems like a handy feature that would allow more use of the container style. On Wed, Nov 2, 2016 at 10:01 AM +1100, "Tom Coleman" notifications@github.com wrote: If you want that level of control over when the query runs etc, I wonder if you should just use withApollo and run the query yourself? — |
@tmeasday It is fine to use |
Hey Michel, I'm not doing anything fancy but doing a direct call to 'apolloClient.query({ options })' - documented in the API reference of the Apollo client. It returns a promise with a data parameter which I parse in the same way you can parse the Apollo result from a containerised call. |
Note that with the release of Apollo client version 0.5, the query merging style of query batching is being discontinued. You need a batching capable server to do it. It would therefore now be better to avoid many individual components making queries as I was using earlier. |
@heysailor We're still planning on publishing query merging in a separate package. If making lots of individual components with small queries made your code easier to understand and maintain, I would stick to that, and not make things more complicated just because there's currently no package that does batching for you if the server doesn't support it. |
@helfer Any word on the concept of query merging? |
there is an other issue with skip. Actually when skip change to false, graphql HoC unsubscribes from the queryObservable (https://github.com/apollostack/react-apollo/blob/master/src/graphql.tsx#L286). So I see 2 options:
What do you think? I did proposition 2 pull requests : #342 |
is this still an issue? how can you disable the first automatic fetch while being able to fetch it later on? |
We've been discussing a bit on this topic here: #196 The best way currently to skip the query while allowing to refetch is to use
Basically this will force to run graphql agains the cache, for example if you got a detail view that gets an id prop, which may be empty if you want to create a new object
|
@bkniffler still don't see how this helps for my situation. If you pass an id to the component but don't want it loaded until refetch is called, this wont work, since the id is there. My use case is that I don't want to generate a signed download URL from google cloud storage unless a user clicks the download button. Can't find an easy way to only query after a user clicks :/ unless I do some weird hacky thing. |
Can't you do something like: <Download _id={id} />
@graphql(
gql`
query xyz($id: String) {
xyz(id: $id) {
id
}
}
`,
{
options: ({ id }) => ({
fetchPolicy: !id? 'cache-only' : undefined,
variables: { id }
}),
},
)
class Download extends Component {
triggerRefetch(){
refetch({ id: this.props._id })
}
} I agree its hacky and I'd wish for some proper way to control skipping. |
@bkniffler oh okay, I thought options only took in the component props, not the refetch variables. |
Urgh, I think you're right. I think you're better off using the vanilla apollo client that you get with @withapollo. By the way, why aren't you using mutations? |
@bkniffler I'm not changing anything. Just loading data, why would it be a mutation? |
Hello ! It seems you can achieve this using directive export default graphql(gql`
query SearchQuery($query: String, $first: Int, $skip: Boolean!) {
search(query: $query, first: $first) @skip(if: $skip) {
edges {
....
}
}
}
`, { options: {variables: { skip: true } }})(Input); Initial query wont be triggered, but refetch will be available :) |
I encountered a similar situation today. I'm still new to the GraphQL scene, but in my mind, I keep wondering how is this not the default case for every query. Isn't most data requested after some kind of async user interaction? Clicking a link, a button, filling an input. A basic search/filter scenario is the simplest use case I can think of where this is most evident. Think about a set of results being displayed after submitting a search. Is there any "proper" approach to handling this type of situations, thinking in GraphQL? |
@dstendardi That is a great example! We should add it to the docs on how to achieve this! I think we can also support a variant / policy towards skipping that may make this easier as well? I'm torn because everything (that I have seen) is possible with today's skip process. @nfantone I think GraphQL is used both in response to inputs and as a way to fetch data for the app to even render. So it is needed to be able to be called on demand and load initially |
@jbaxleyiii Well... that's not entirely true. At least, not always. Sometimes you find yourself wanting to just display components that would allow the user to provide some input that may, potentially, trigger some data fetching. Or not. So the rendering capabilities of GraphQL don't even play a part until the user interacts with the UI. |
@nfantone I agree! That's what I meant by |
Instead get the `<FloodsRoutes>` component to fetch `currentUser` after logging in. Had to switch from apollo's skip operation https://www.apollographql.com/docs/react/basics/queries.html#graphql-skip, to the `@skip` directive so that the component would be able to call `data.refetch()`. Apollo doesn't pass data to the component with the skip operation. apollographql/react-apollo#289 (comment)
@dstendardi Is it possible to send a different set of variables to query using your technique? From what I can tell, I'm stuck with the variables that are hard coded in it when you do the composition. |
@dstendardi ´s solution works fine for the use case of skipping initially and calling the query on button press! |
This doesn't work if you don't want to fire an initial query to your GraphQL endpoint. I don't really see this as a viable solution. |
For people still wondering the best way to load a |
created a custom hook with useState as a workarround.
|
Skipping a query will not allow to refetch data, since the refetch function will be undefined. Is this a bug or by design?
The text was updated successfully, but these errors were encountered: