Skip to content

How to do query on button click? #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
NCR-Shi opened this issue Sep 25, 2017 · 6 comments
Closed

How to do query on button click? #121

NCR-Shi opened this issue Sep 25, 2017 · 6 comments

Comments

@NCR-Shi
Copy link

NCR-Shi commented Sep 25, 2017

I want to say thanks first for this great Apollo integration!
I ran into some problem recently and got no answer after googling around. The scenario is pretty simple: I don't want the query to be called automatically when the page/components is loaded, I just want to call the query on button click.
So in React with react-apollo I can do something like this:

import { withApollo } from 'react-apollo';
import{ Component } from 'react';

class MyComponent extends Component {
  doQuery() {
  	this.props.client.query({
  	  query: myCustomQuery,
  	  variables: {
  	  	variable1,
  	  	variable2,
  	  }
  	})
  }
  render() {
  	<button onClick={() => this.doQuery}></button>
  }
} 

export default withApollo(MyComponent);

The 'client' prop comes from withApollo HOC
I wonder if there is a way to do this in vue-apollo?
Looked into this issue #36 , but I believe the answer provided does not meet my need, because "refetch" will simply do the query again with same query/variables. I am searching for a way in which every time I click the button I can get different queries according to my variables.

@Akryum
Copy link
Member

Akryum commented Sep 25, 2017

You can either use this.$apollo.query(...) or use skip in the smart query definition:

export default {
  data () {
    return {
      myVariables: null,
    },
  },

  apollo: {
    items: {
      query: GQL_QUERY,
      variables () {
        return this.myVariables
      },
      skip () {
        return !this.myVariables
      },
    },
  },

  methods: {
    onButtonClick () {
      this.myVariables = {
        // ...
      },
    },
  },
}

@NCR-Shi
Copy link
Author

NCR-Shi commented Sep 25, 2017

Thanks for you reply. So with the skip, will it help me avoid auto query on component load? (Because I only want the query on button click). If not, is there any other ways to do this?

@Akryum
Copy link
Member

Akryum commented Sep 25, 2017

I showed you two different ways already. 😄

@NCR-Shi
Copy link
Author

NCR-Shi commented Sep 25, 2017

Oh I see what you are saying. this.$apollo.query works perfectly for me. Thanks!

@Akryum Akryum closed this as completed Oct 28, 2017
@indpurvesh
Copy link

@Akryum thanks for the answer. I have another question:

  • How do we specify custom client when we do query/mutation on a click event.

       this.$apollo.mutate({
             mutation: UserAuth,
            variables: {
                email: "[email protected]",
                password: "admin123",
             },
       }).then((data) => {
         return data;
       }).catch((error) => {
         // Error
         console.error(error)
         // We restore the initial user input
       });
    

@indpurvesh
Copy link

indpurvesh commented Apr 28, 2019

@Akryum i got the answer. Just in case if someone wants to know the solution.

methods: {
    async authMutation() {
        this.$apollo.client = "auth"; // replace auth with your client id from vue-apollo.js file.
        return this.$apollo.mutate({
             mutation: UserAuth,
            clientId: 'auth',
            variables: {
               email: "[email protected]", // change this to actual data
               password: "admin123", // change this to actual data
            },
         }).then((data) => {
          return data;
         }).catch((error) => {
            console.error(error)
        });
    }
}

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

No branches or pull requests

3 participants