helfer / apollo-link-debounce

An Apollo Link that debounces requests
MIT License
119 stars 22 forks source link

set debounce time on query level #2

Closed smeijer closed 5 years ago

smeijer commented 6 years ago

Would be nice if we could set the debounce time on the query level, instead of global scope. Or perhaps bound to a debounceKey?

My prefered way. Specify the delay in the query, and thereby close to the implementing components:

const op = {
    query: gql`mutation slide($val: Float){ moveSlider(value: $val) }`,
    variables: { val: 99 }
    context: {
        debounceKey: 'Slide',
        debounceDelay: 300,
    },
};

Alternative option that could be usefull. Supply a key-delay-map as second argument to the link constructor:

const DEBOUNCE_TIMEOUT = 100;
this.link = ApolloLink.from([
    new DebounceLink(DEBOUNCE_TIMEOUT, {
      Slide: 300,
    }),
    new HttpLink({ uri: URI_TO_YOUR_GRAPHQL_SERVER }),
]);
leebenson commented 6 years ago

IMO, this is vital. Debouncing a query globally is useful in very limited circumstances. It's unlikely that, say, a slider animation and an autocompleted input bar are going to require exactly the same debounce interval.

bennypowers commented 6 years ago

I'd prefer to pass in an optional debounceTimeout in the context of the query than configure it up front

Like in OP's first example.

// default for when no timeout is specified
const DEBOUNCE_TIMEOUT = 5 * 1000;

export const debounceLink =
  new DebounceLink(DEBOUNCE_TIMEOUT);
fetchMore() {
  const debounceKey = 'POSTS_KEY';
  const debounceTimeout = 1000; // custom, per-query timeout
  const context = { debounceKey, debounceTimeout };
  const variables = { after, first };
  return this.fetchMore({ context, variables, updateQuery });
}

This would let me lazy-load components that handle their own debounce config.