Topsy
Topsy

Reputation: 1172

How to call a query only on refetch

I was wondering if there was a way to call a query in a react component without connecting the component with graphql, because it lead the component to launch the query the first time it mount.

So basically is there a way to avoid the default call?

Upvotes: 0

Views: 426

Answers (1)

Andy Ray
Andy Ray

Reputation: 32076

If you manually need to control the query or mutation and not use the default behavior from the graphql() HOC, use withApollo. It injects a GraphQL API client object into your component as a prop.

The client has .query and .mutate as methods. So you can do this.props.client.query(...) in componentDidMount.

import { withApollo } from 'react-apollo';

class Thing extends Component {
    componentDidMount() {
        this.props.client.query(myQuery).then(data => {
            this.setState({ data });
        };
    }
}

export default withApollo(Thing);

Upvotes: 3

Related Questions