{"id":13878758,"url":"https://github.com/RenoFi/rack-graphql","last_synced_at":"2025-07-16T14:32:49.504Z","repository":{"id":34934771,"uuid":"191759958","full_name":"RenoFi/rack-graphql","owner":"RenoFi","description":"Rack middleware for building ruby services with graphql api.","archived":false,"fork":false,"pushed_at":"2024-11-22T10:38:49.000Z","size":309,"stargazers_count":21,"open_issues_count":0,"forks_count":2,"subscribers_count":17,"default_branch":"main","last_synced_at":"2024-11-22T11:27:03.325Z","etag":null,"topics":["backend"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RenoFi.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-06-13T12:35:58.000Z","updated_at":"2024-11-22T10:33:44.000Z","dependencies_parsed_at":"2023-10-02T07:47:43.436Z","dependency_job_id":"e1de0e5d-179b-462d-827c-0cc1712c0dc4","html_url":"https://github.com/RenoFi/rack-graphql","commit_stats":{"total_commits":283,"total_committers":7,"mean_commits":40.42857142857143,"dds":"0.24028268551236753","last_synced_commit":"1dfb1a6eaecea29aaaad7cf87cf2811ecbf26730"},"previous_names":[],"tags_count":55,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RenoFi%2Frack-graphql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RenoFi%2Frack-graphql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RenoFi%2Frack-graphql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RenoFi%2Frack-graphql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RenoFi","download_url":"https://codeload.github.com/RenoFi/rack-graphql/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226138849,"owners_count":17579496,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["backend"],"created_at":"2024-08-06T08:01:58.979Z","updated_at":"2024-11-24T07:31:17.522Z","avatar_url":"https://github.com/RenoFi.png","language":"Ruby","funding_links":[],"categories":["Ruby"],"sub_categories":[],"readme":"[![Gem Version](https://badge.fury.io/rb/rack-graphql.svg)](https://rubygems.org/gems/rack-graphql)\n[![Build Status](https://github.com/RenoFi/rack-graphql/actions/workflows/ci.yml/badge.svg)](https://github.com/RenoFi/rack-graphql/actions/workflows/ci.yml?query=branch%3Amain)\n\n# rack-graphql\n\n`rack-graphql` is designed to build ruby services with graphql api. It provides `/graphql` endpoint and can handle [subscriptions](https://graphql-ruby.org/guides#subscriptions-guides) and [multiplex](https://graphql-ruby.org/queries/multiplex.html).\n\nIt works on pure rack and none of `ActionController`/`ActionDispatch`/`ActionPack` or `Sinatra` is required. By default it provides health route on `/health` and `/`, which can be disabled.\n\nIt can be used together with rails to not make graphql requests be routed with `ActionDispatch` or more pure ruby apps.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'rack-graphql'\n```\n\n## Usage example\n\nAdd following to your `config.ru` file:\n\n```ruby\nrun RackGraphql::Application.call(\n  schema: YourGraqphqlSchema,                       # required\n  app_name: 'your-service-name',                    # optional, used for health endpoint content\n  context_handler: YourGraphqlContextHandler,       # optional, empty `proc` by default\n  log_exception_backtrace: !A9n.env.production?,    # optional, `false` default\n  # `true` when `RACK_GRAPHQL_LOG_EXCEPTION_BACKTRACE` env var is set to `'1'` or `'true'`\n  health_route: true,                               # optional, true by default\n  health_on_root_path: health_route,                # optional, health_route value by default (mind map '/' is covering '/any/path-123') \n  root_path_app: YourDashboardApp,                  # optional\n  logger: A9n.logger,                               # optional, not set by default\n  error_status_code_map: { IamTeapotError =\u003e 418 }, # optional\n  re_raise_exceptions: true,                        # optional, false by default\n  request_epilogue: -\u003e { ActiveRecord::Base.connection_handler.clear_active_connections! }\n)\n```\n\n`context_handler` can be a class, object or proc. It must respond to `call` method taking `env` as an argument. It is supposed to decode or transform request properties to graphql context (eg. jwt token to user object, as shown on an example below).\n\n### Example: using context handler for JWT authentication\n\n```ruby\nclass GraphqlContextHandler\n  class \u003c\u003c self\n    def call(env)\n      payload = decode_payload(env)\n\n      graphql_context_hash(payload)\n    end\n\n    private\n\n    def graphql_context_hash(payload)\n      {\n        current_user: current_user(payload)\n      }\n    end\n\n    def decode_payload(env)\n      jwt = env[\"HTTP_AUTHORIZATION\"].to_s.split(' ').last\n\n      return if jwt.blank?\n\n      DecodeJwt.call(jwt) || {}\n    end\n\n    def current_user(payload)\n      return unless payload\n      return unless payload['user_id']\n\n      UserRepo.find_by_id(payload['user_id'])\n    end\n  end\nend\n```\n\n### Logging exception backtrace\n\nRackGraphql catches all errors and respond with 500 code. By default it adds exception backtrace to the response body. If you don't want to have the backtrace in the response set:\n\n```\nRackGraphql.log_exception_backtrace = false\n```\n\n### Error tracking/reporting\n\nTo respect the graphql spec, all errors need to be returned as json and `rack-graphql` catches all exceptions and does NOT re-raise them. You can change this behavior via `re_raise_exceptions` argument.\nBecause of this, using error tracking middleware (`use Sentry::Rack::CaptureExceptions`, `use Raven::Rack`) does not take any effect for graphql requests.\n\nTo use Sentry or other reporting tool for graphql queries, you should handle it on graphql schema level:\n\n```ruby\nclass MySchema \u003c GraphQL::Schema\n  rescue_from StandardError do |e, obj, args, ctx, field|\n    extra = {\n      args: args,\n      field: field.inspect,\n      context: ctx\n    }\n    Sentry.capture_exception(e, extra: extra)\n    # re-raise to be handled by rack middleware\n    raise\n    # or return execution error\n    ::GraphQL::ExecutionError.new(\n      exception.class.to_s,\n      options: { \"http_status\" =\u003e 500 },\n      extensions: {\n        \"code\" =\u003e exception.class.to_s,\n        \"http_status\" =\u003e 500,\n        \"details\" =\u003e exception.inspect\n      }\n    )\n  end\nend\n```\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/RenoFi/rack-graphql. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRenoFi%2Frack-graphql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FRenoFi%2Frack-graphql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FRenoFi%2Frack-graphql/lists"}