0

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?

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

2 Comments

You, I like you.
Edit : the import is import { withApollo } from 'react-apollo'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.