Skip to content

Send the query key as argument to the skip fn #810

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

Merged
merged 6 commits into from
Oct 19, 2019
Merged

Conversation

andyghiuta
Copy link
Contributor

This enabled the possibility for the skip to be defined in defaultOptions
And based on certain things from the query to skip it
EG: query with variable X should be skipped until Y prop is available

Example:

In a Vue I have 2 queries that depend on someGlobalParam (set after some initialization, etc), but one that can be fired right away, because it's not dependent on that:

export default {
  apollo: {
    queryOne: {
      variables() {
        return {
          someGlobalParam: this.someGlobalParam,
        };
      },
    },
    queryTwo: {
      variables() {
        return {
          someGlobalParam: this.someGlobalParam,
        };
      },
    },
    queryThree: {
      variables() {
        return {
          canGoRightAway: true,
        };
      },
    },
  },
};

One option is to add skip for each of them, but it becomes cumbersome, prone to errors (people forget) and it means a lot of duplicate. Instead I thought this could be done in the apollo provider initialization, but that means I need to know which query is accessing that, hence this change, to send the query key.

The provider initialization:

const apolloProvider = new VueApollo({
  defaultClient: apolloClient,
  defaultOptions: {
    $query: {
      loadingKey: 'loading',
      fetchPolicy: 'cache-and-network',
      skip(vm, key) {
        if (!this.isAuthenticated) {
          return true;
        }
        let { variables } = this.$apollo.queries[key].options;
        if (!variables) {
          return false;
        }
        if (typeof variables === 'function') {
          // variables is a function, call it and get the resulting object
          variables = variables.call(this);
        }
        if (Object.prototype.hasOwnProperty.call(variables, 'someGlobalParam')) {
          // skip if someGlobalParam is not (yet) set
          return !this.someGlobalParam;
        }
        return false;
      },
    },
  },
});

andyghiuta and others added 6 commits October 6, 2019 20:56
This enabled the possibility for the skip to be defined in defaultOptions
And based on certain things from the query to skip it
EG: query with variable X should be skiped  until Y prop is available
@Akryum Akryum merged commit 78749c9 into vuejs:dev Oct 19, 2019
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

Successfully merging this pull request may close these issues.

2 participants