GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - sunny/graph_attack: Ruby GraphQL analyser for blocking & throttling calls by IP

BranchesTags
Open more actions menu

Latest commit

 

History

78 Commits
78 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
bin
bin
 
 
lib
lib
 
 
spec
spec
 
 
.gitignore
.gitignore
 
 
.rspec
.rspec
 
 
.rubocop.yml
.rubocop.yml
 
 
.ruby-version
.ruby-version
 
 
.travis.yml
.travis.yml
 
 
CHANGELOG.md
CHANGELOG.md
 
 
CODE_OF_CONDUCT.md
CODE_OF_CONDUCT.md
 
 
Gemfile
Gemfile
 
 
LICENSE.md
LICENSE.md
 
 
README.md
README.md
 
 
Rakefile
Rakefile
 
 
graph_attack.gemspec
graph_attack.gemspec
 
 

Repository files navigation

GraphAttack

Build Status

GraphQL analyser for blocking & throttling.

Usage

This gem adds a method to limit access to your GraphQL fields by IP address:

class QueryType < GraphQL::Schema::Object
  field :some_expensive_field, String, null: false do
    extension GraphAttack::RateLimit, threshold: 15, interval: 60
  end

  # …
end

This would allow 15 requests per minute by the same IP address, blocking the 16th and subsequent requests within that 60-second window.

Requirements

Requires GraphQL Ruby and a running instance of Redis.

Installation

Add these lines to your application’s Gemfile:

# GraphQL analyser for blocking & throttling by IP.
gem "graph_attack"

And then execute:

$ bundle

Finally, make sure you add the current user’s IP address as ip: to the GraphQL context. E.g.:

class GraphqlController < ApplicationController
  def create
    result = ApplicationSchema.execute(
      params[:query],
      variables: params[:variables],
      context: {
        ip: request.ip,
      },
    )
    render json: result
  end
end

If that key is nil, throttling will be disabled.

Configuration

Custom context key

If you want to throttle using a different value than the IP address, you can choose which context key you want to use with the on option. E.g.:

extension GraphAttack::RateLimit,
          threshold: 15,
          interval: 60,
          on: :client_id

Custom Redis client

Use a custom Redis client instead of the default with the redis_client option:

extension GraphAttack::RateLimit,
          threshold: 15,
          interval: 60,
          redis_client: Redis.new(url: "…")

Custom Redis ratelimit key prefix

Use a custom prefix in front of the Redis ratelimit key:

extension GraphAttack::RateLimit,
          threshold: 15,
          interval: 60,
          redis_prefix: "my-app/production/"

Common configuration

To have a default configuration for all rate-limited fields, you can create an initializer:

GraphAttack.configure do |config|
  # config.threshold = 15
  # config.interval = 60
  # config.on = :ip
  # config.redis_client = Redis.new
  # config.redis_prefix = ""
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/rake to run the tests and the linter. You can also run bin/console for an interactive prompt that will allow you to experiment.

Versionning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Releasing

To release a new version, update the version number in version.rb and in the CHANGELOG.md. Update the README.md if there are missing segments, make sure tests and linting are pristine by calling bundle && bin/rake, then create a commit for this version, for example with:

git add --patch
git commit -m v`ruby -rbundler/setup -rgraph_attack/version -e "puts GraphAttack::VERSION"`

You can then run bin/rake release, which will assign a git tag, push using git, and push the gem to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/sunny/graph_attack. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Code of Conduct

Everyone interacting in the GraphAttack project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Authors

  • Fanny Cheung — ynote.hk
  • Sunny Ripert — sunfox.org

Acknowledgments

Hat tip to Rack::Attack for the the name.

Sponsored by Cults.

Cults. Logo

About

Ruby GraphQL analyser for blocking & throttling calls by IP

Resources

Readme
MIT license

Code of conduct

Code of conduct
Activity

Stars

55 stars

Watchers

1 watching

Forks

10 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.