The new Apollo Client 2.1 uses the render props patters instead of HoC pattern, so it's changed the way Queries and Mutations are handled a little.
I have started using it, but am unsure of a few basic things. One is error handling. I have a custom "AuthResponse" component which I want to pass in either the success or error response. If it's an error (or success) at the moment I am using React's local component state to pass the error to my AuthResponse component, but I'm not sure if this is the best way?
<View style={styles.homeContainer}>
// this is where I want to pass the error to. Currently uses React component state but I'm not sure if this is the best way?
<AuthResponse
authResponse={(this.state && this.state.authResponse) || null}
/>
<Mutation
mutation={registerMutation}
update={(cache, { data: { registerUser } }) => {
// user has been registered
}}
onError={error => {
// is this the right place to start passing errors to other components?
this.setState({
authResponse: error
});
}}
>
{(registerUser, { data, error }) => (
<View>
<Formik
onSubmit={(
values,
{ setSubmitting, setErrors /* setValues and other goodies */ }
) => {
registerUser({ variables: { ...values } });
}}
render={({
handleSubmit
}) => (
<View>
<Button
onPress={handleSubmit}
/>
</View>
)}
/>
</View>
)}
</Mutation>
<Text onPress={this.goToLogin}>Login</Text>
</View>
I am assuming here that the Mutation component should be as small as possible - i.e. not wrap the entire page, because different areas on the screen may require different data from Query or Mutation components.
I did consider wrapping the AuthResponse component with a Query component from Apollo Client, querying the data from local cache which I would have updating manually. This seems like a better solution but there's nothing in the docs about how to spread multiple Query/Mutation components around a page/screen.
Thanks
QueryandMutationcomponents is just ugly (code smell) to my mind. But good news, you can still write as pre 2.1 and compose HOC like you did before. - MacKentoch