RailsAuditLog::Graphql

CI Gem Version Total Downloads Ruby codecov

A graphql-ruby API layer for the rails_audit_log gem. Provides ready-made GraphQL types, queries, and subscriptions for querying audit log entries — without coupling graphql-ruby to the base gem.

Table of Contents

Installation

Add to your application's Gemfile:

gem "rails_audit_log"
gem "rails_audit_log-graphql"

Then run the install generator to wire up your schema:

rails g rails_audit_log:graphql:install

This injects include RailsAuditLog::Graphql::Queries::AuditLogEntriesQueryMixin into app/graphql/types/query_type.rb. If that file doesn't exist, the generator prints the line for you to add manually.

↑ Back to top

Usage

AuditLogEntryType

RailsAuditLog::Graphql::Types::AuditLogEntryType is a graphql-ruby object type that maps directly to RailsAuditLog::AuditLogEntry.

GraphQL field Type Nullable
id ID no
event String no
itemType String no
itemId ID no
createdAt ISO8601DateTime no
objectChanges JSON yes
object JSON yes
metadata JSON yes
reason String yes
whodunnitSnapshot String yes
actorType String yes
actorId ID yes
tenantId String yes

↑ Back to top

AuditLogEntriesQueryMixin

Include RailsAuditLog::Graphql::Queries::AuditLogEntriesQueryMixin into your app's QueryType to add two fields:

# app/graphql/types/query_type.rb
class Types::QueryType < Types::BaseObject
  include RailsAuditLog::Graphql::Queries::AuditLogEntriesQueryMixin
end

auditLogEntry(id: ID!): AuditLogEntry

Fetch a single entry by ID. Returns nil if not found.

auditLogEntries(...): [AuditLogEntry!]!

List entries with optional filters and offset pagination.

Argument Type Default Description
event String Filter by event type (create, update, destroy)
itemType String Filter by audited model class name
itemId ID Filter by audited record ID
actorId ID Filter by actor ID
page Int 1 Page number (1-based)
perPage Int 25 Results per page

Results are ordered by created_at DESC.

↑ Back to top

Authentication

If RailsAuditLog.authenticate is configured, the block is called with the GraphQL context before every query. Return a truthy value to allow access; return falsy to raise GraphQL::ExecutionError with "Unauthorized".

RailsAuditLog.configure do |config|
  config.authenticate { |ctx| ctx[:current_user]&.admin? }
end

If no authenticate block is set, all queries are permitted.

↑ Back to top

Development

bin/setup         # install dependencies
bundle exec rake  # lint + tests

↑ Back to top

Contributing

See CONTRIBUTING.md.

↑ Back to top

License

The gem is available as open source under the terms of the MIT License.