Skip to content
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

[QUESTION]: Reset attempts #370

Open
simplecommerce opened this issue Oct 24, 2023 · 5 comments
Open

[QUESTION]: Reset attempts #370

simplecommerce opened this issue Oct 24, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@simplecommerce
Copy link

Hi,

I was wondering if it was possible to reset the attempts from a mutation.

For example, I have a rate limit on a login mutation.
I would like to be able to reset the attempts when a reset password is called. (if applicable).

I am going through the doc to see how I could achieve this but I don't see a clear path yet.

Thanks!

@ravangen
Copy link
Owner

ravangen commented Nov 6, 2023

Hi, thank you for your question!

It is not currently possible, but it seems reasonable. I think it could be achieved via a callback. Currently the limiter only does work before the resolve action, but could also have an optional after action callable (likely only called on success?).

function onResolve(limiter, key, points, result) {
  // Your custom logic based on result
  limiter.reward(key, points);
  return result; // outcome of the original resolve (could transform if needed)
}

const resolvers = {
  Query: {
    quote: {
      resolve: () =>
        'The future is something which everyone reaches at the rate of sixty minutes an hour, whatever he does, whoever he is. ― C.S. Lewis',
      extensions: {
        // IMPORTANT: Specify any overrides via field's extensions under directive's name
        [directiveName]: {
          onResolve: onResolve,
        }
      }
    },
  },
};

Rate limiter wraps resolve, and would return the result of onResolve when provided, otherwise resolve as normal.

I can try to put together something to this effect in the next week.

At some point, you may want your own rate limiter within a resolver instead of a general purpose one.

@simplecommerce
Copy link
Author

Hi, thank you for your question!

It is not currently possible, but it seems reasonable. I think it could be achieved via a callback. Currently the limiter only does work before the resolve action, but could also have an optional after action callable (likely only called on success?).

function onResolve(limiter, key, points, result) {
  // Your custom logic based on result
  limiter.reward(key, points);
  return result; // outcome of the original resolve (could transform if needed)
}

const resolvers = {
  Query: {
    quote: {
      resolve: () =>
        'The future is something which everyone reaches at the rate of sixty minutes an hour, whatever he does, whoever he is. ― C.S. Lewis',
      extensions: {
        // IMPORTANT: Specify any overrides via field's extensions under directive's name
        [directiveName]: {
          onResolve: onResolve,
        }
      }
    },
  },
};

Rate limiter wraps resolve, and would return the result of onResolve when provided, otherwise resolve as normal.

I can try to put together something to this effect in the next week.

At some point, you may want your own rate limiter within a resolver instead of a general purpose one.

What do you mean by:

At some point, you may want your own rate limiter within a resolver instead of a general purpose one.

Would you suggest we build our own specific one based on example libraries such as this one?

I do agree that a general one makes it difficult to customize per resolver.
I was trying to think of a way of extending this one to do so.

@simplecommerce
Copy link
Author

Ah, I also forgot to ask, how would we be able to reset an attempt if such a feature was implemented?
For example, if I reached 5 out of 6 times and the last attempt is successful, i'd want to reset the count to 0 for that specific person.

@ravangen
Copy link
Owner

ravangen commented Nov 8, 2023

At some point, you may want your own rate limiter within a resolver instead of a general purpose one.

Would you suggest we build our own specific one based on example libraries such as this one?

This library builds on top of rate-limiter-flexible. You could instantiate your own limiter instance in resolve.

I think a callback is reasonable feature to add, but this library won't necessarily cover 100% of all custom cases..

@simplecommerce
Copy link
Author

At some point, you may want your own rate limiter within a resolver instead of a general purpose one.

Would you suggest we build our own specific one based on example libraries such as this one?

This library builds on top of rate-limiter-flexible. You could instantiate your own limiter instance in resolve.

I think a callback is reasonable feature to add, but this library won't necessarily cover 100% of all custom cases..

Thanks I will take a look at the library you mentioned.

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

No branches or pull requests

2 participants