-
Notifications
You must be signed in to change notification settings - Fork 787
Allow ApolloProvider Prop Updates #479
Allow ApolloProvider Prop Updates #479
Conversation
@wmcbain: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Meteor Contributor Agreement here: https://contribute.meteor.com/ |
530a883
to
8138b99
Compare
Nice! This looks like a great change. |
@wmcbain if you can add a couple tests for this, we can merge it and release it! Thanks! |
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.
A couple of comments, looks good though 👍
Also needs some tests so we can make sure that this behavior never regresses 😊
src/ApolloProvider.tsx
Outdated
this._init(props); | ||
} | ||
|
||
componentWillReceiveProps(nextProps) { |
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 think this should be componentDidUpdate
. We also wouldn’t need to pass in props
in that case.
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.
Also, we should probably put this in an if
that checks if we actually have a new client.
src/ApolloProvider.tsx
Outdated
@@ -61,7 +69,6 @@ export default class ApolloProvider extends Component<ProviderProps, any> { | |||
// intialize the built in store if none is passed in | |||
props.client.initStore(); | |||
this.store = props.client.store; |
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.
Shouldn’t the client and store be state instead of class properties? Or better yet, why do we even need them to be class properties? We can just call initStore
on them when they change.
@@ -10,7 +10,9 @@ import { | |||
Store, | |||
} from 'redux'; | |||
|
|||
import ApolloClient from 'apollo-client'; | |||
/* tslint:disable:no-unused-variable */ | |||
import ApolloClient, { ApolloStore } from 'apollo-client'; |
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.
TS compiler was throwing an error without ApolloStore
not being in scope when potentially returning an ApolloStore
in getChildContext
.
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.
Yeah this is one weird edge case in TS
Changelog.md
Outdated
@@ -3,6 +3,7 @@ | |||
Expect active development and potentially significant breaking changes in the `0.x` track. We'll try to be diligent about releasing a `1.0` version in a timely fashion (ideally within 1 or 2 months), so that we can take advantage of SemVer to signify breaking changes from that point on. | |||
|
|||
### vNext | |||
- ApolloProvider now changes it's client and store when those props change. [PR #479](https://github.com/apollographql/react-apollo/pull/479) |
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.
It's -> its
Ready for review |
b964e63
to
0a36251
Compare
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.
Looks good to me. I’d just like to see one or two more tests that assert GraphQL container components query the correct client and/or when the client changes re-queries (or not) against the new client.
0a36251
to
70fa2ec
Compare
expect(initStoreMock).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('child component should be able to query new client and store when props change', () => { |
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.
@calebmer Test added
@calebmer I'll let you finish reviewing this and include it in the next release. |
The new test is good, but I really want to see something that uses the When the client changes, does the HOC make a new request to the new client? When the client changes and then These are behaviors I’d like to see asserted with tests. |
@calebmer I'm not sure if I'm familiar enough with this library to provide the depth of tests that you are looking for. I'm struggling to provide these tests within reasonable time as a result. Is there an example that you can point to that would help me see what you're looking for? |
* Change client and store when new props are passed to the ApolloProvider * Update Changelog
* Requested ApolloProvider changes * Add tests
4a70834
to
7d91e47
Compare
7e8a9b9
to
c18528c
Compare
|
||
// Both cases fail | ||
expect(initialInterface.query).not.toHaveBeenCalled(); | ||
expect(nextInterface.query).toHaveBeenCalled(); |
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.
Hey @calebmer, revisited this problem after learning a bit more and running into a bug that fit the test case you described exactly in this PR. I'm wondering now if you foresaw this being an issue?
Any help/insight is appreciated.
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 added a better test and fixed the bugs the test revealed. I’ll merge once CI passes 👍
Released in 1.0.0-rc.3 🎉 |
Thanks! |
We have a use case in React Native where we switch the GraphQL endpoint dynamically while the app is running. This PR changes the ApolloProvider API by updating it's client and store when new props are delivered to the provider.
TODO: