Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Add setVariables to data #472

Closed
wmertens opened this issue Feb 21, 2017 · 13 comments
Closed

Add setVariables to data #472

wmertens opened this issue Feb 21, 2017 · 13 comments

Comments

@wmertens
Copy link
Contributor

I found setVariables in apollo client and that seems to be super useful for updating a query based on internal state.

However, right now it seems to be missing from data as provided by graphql(). Is there a reason? Can it be included?

@calebmer
Copy link
Contributor

The correct way to change variables in React is to pass in a new set of variables to props. react-apollo doesn’t keep variables in state so the container component can’t actually change its own variables. Only the parent component can change variables by passing in a new set of variables. You could try calling refetch with some new variables, but I’m not sure that will work…

This may be worth reconsidering.

@stubailo
Copy link
Contributor

I'm not sure if it would be good to start introducing a lot of state into the React HoC - perhaps we should suggest an approach where state is kept outside? https://dev-blog.apollodata.com/simplify-your-react-components-with-apollo-and-recompose-8b9e302dea51#.jxb1nyyih

Alternatively we could make state a primary function for stuff like keeping pagination variables etc.

@wmertens
Copy link
Contributor Author

wmertens commented Feb 22, 2017 via email

@wmertens
Copy link
Contributor Author

wmertens commented Feb 22, 2017 via email

@calebmer
Copy link
Contributor

@wmertens I’m not even sure if refetch works correctly, I’d be surprised if it did. Not surprised if it fetched with a new set of variables, but surprised if those variables persisted after a component re-render.

It isn’t so much about whether or not we should introduce state into an HoC, but more so that we have already made the decision that variables is a function of the component’s props. I’m open to considering how we could change that decision, however.

@wmertens
Copy link
Contributor Author

wmertens commented Feb 27, 2017

@calebmer IMHO variables should be initialized and updated from props, but setVariables and refetch should update the variables. These latter two are super useful for e.g. displaying a list of items from a query and then updating the query with search parameters.

This can be done in a completely backwards compatible manner, presumably at virtually no runtime cost.

Not having setVariables means that a simple graphql(...)(MyComponent) has to be wrapped in a component that does nothing besides provide callbacks to put its state into props, so that the display component can provide the new query variables by calling the callbacks. This is rather hostile to developers that are not used to writing code in that way, and actually takes away an existing feature from apollo-client…

@calebmer
Copy link
Contributor

calebmer commented Mar 1, 2017

The problem with that is that when the variables prop changes we are in the awkward position of needing to either blow away the variables set with setVariables, or merge them together 😣

@wmertens
Copy link
Contributor Author

wmertens commented Mar 2, 2017 via email

@helfer
Copy link
Contributor

helfer commented Mar 21, 2017

I really think that variables should be given by props and not set internally in the component. If you have a specific use-case where this would be useful, please add a clear description here: apollographql/apollo-client#1391

@amannn
Copy link
Contributor

amannn commented Jul 27, 2017

I just stumbled across this issue. I was actually pleasantly surprised to see that variables can be passed to refetch which will update the query and pass the new variables via this.props.variables to the new component. However, I've seen that these variables are reset at some point which I couldn't really narrow down.

If react-apollo doesn't want to support this, maybe it would make sense to warn/error on refetch(variables) when options.variables is set on the HOC (is that the only case where variables can be reset while the component is mounted?). I really thought this was a bug.

Anyhow, the withState HOC of recompose is really helpful for this use case:

compose(
  withState('sort', 'onUpdateSort', 'created,desc'),
  graphql(query, {options: ({sort}) => ({variables: {sort}})})
)(SomeComponent);

This works fine for me, although it would be nice to not having to wrap another HOC around. Maybe it would make sense to provide state if options is specified as an object, but not if it is set to a function and sets variables. By doing that, we'd never end up in a situation were we have to decide if/how variables should be merged, but can reduce boilerplate for simpler use cases.

@joshuataylor
Copy link

Refetch is fantastic until you need to delete a variable that is fetched. You can't just set foo: null, because foo might have a null value.

@wmertens
Copy link
Contributor Author

wmertens commented Sep 3, 2017 via email

@joshuataylor
Copy link

Yeah, ended up doing that, sorry forgot to loop back here and update it. The http://facebook.github.io/graphql/#sec-Null-Value caught me out :).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants