Convert Figma logo to code with AI

graphiti-api logographiti

Stylish Graph APIs

1,018
143
1,018
72

Top Related Projects

No Longer Maintained - A lightning fast JSON:API serializer for Ruby Objects.

A fast JSON:API serializer for Ruby (fork of Netflix/fast_jsonapi)

A resource-focused Rails library for developing JSON:API compliant servers.

ActiveModel::Serializer implementation and Rails hooks

Quick Overview

Graphiti is a Ruby library for building and maintaining consistent, high-performance APIs. It provides a framework for creating RESTful APIs with a focus on flexibility, efficiency, and developer productivity. Graphiti aims to solve common API development challenges while promoting best practices and maintainable code.

Pros

  • Highly customizable and flexible API design
  • Built-in support for efficient querying and pagination
  • Automatic documentation generation
  • Strong emphasis on testing and maintainability

Cons

  • Steeper learning curve compared to simpler API frameworks
  • Requires adherence to specific conventions and patterns
  • May be overkill for small or simple API projects
  • Limited community support compared to more popular alternatives

Code Examples

  1. Defining a resource:
class PostResource < ApplicationResource
  attribute :title, :string
  attribute :body, :string
  attribute :published_at, :datetime

  has_many :comments
  belongs_to :author
end
  1. Customizing a query:
class PostResource < ApplicationResource
  def base_scope
    Post.published
  end

  filter :title, :string do
    eq do |scope, value|
      scope.where('lower(title) = ?', value.downcase)
    end
  end
end
  1. Defining a custom endpoint:
class PostResource < ApplicationResource
  endpoint :featured do
    get do
      scope = base_scope.featured
      render_jsonapi(scope)
    end
  end
end

Getting Started

To get started with Graphiti, add it to your Gemfile:

gem 'graphiti'
gem 'graphiti-rails'

Then run:

bundle install
rails generate graphiti:install

Create a resource:

rails generate graphiti:resource Post title:string body:text

This will generate a resource file, controller, serializer, and specs. You can now start defining your API endpoints and customizing your resources.

Competitor Comparisons

No Longer Maintained - A lightning fast JSON:API serializer for Ruby Objects.

Pros of fast_jsonapi

  • Faster serialization performance due to optimized code generation
  • Simpler setup and configuration for basic use cases
  • Lightweight with minimal dependencies

Cons of fast_jsonapi

  • Less flexible for complex relationships and custom data structures
  • Limited query parameter support and filtering options
  • Lack of built-in pagination and sorting features

Code Comparison

fast_jsonapi:

class MovieSerializer
  include FastJsonapi::ObjectSerializer
  attributes :name, :year
  has_many :actors
end

Graphiti:

class MovieResource < ApplicationResource
  attribute :name, :string
  attribute :year, :integer
  has_many :actors
end

Key Differences

  1. Graphiti provides a more comprehensive API design framework, while fast_jsonapi focuses primarily on serialization.
  2. Graphiti offers built-in support for filtering, sorting, and pagination, which fast_jsonapi lacks.
  3. fast_jsonapi is generally faster for simple serialization tasks, but Graphiti offers more flexibility for complex data structures and relationships.
  4. Graphiti follows a resource-centric approach, while fast_jsonapi is more focused on model-based serialization.
  5. Graphiti provides better support for customizing API behavior and adhering to JSON:API specifications.

A fast JSON:API serializer for Ruby (fork of Netflix/fast_jsonapi)

Pros of jsonapi-serializer

  • Lightweight and focused solely on JSON:API serialization
  • Simple and straightforward API for basic use cases
  • Widely adopted and battle-tested in many production environments

Cons of jsonapi-serializer

  • Limited to serialization only, doesn't handle deserialization or querying
  • Lacks advanced features like automatic relationship handling and query customization
  • Requires more manual configuration for complex data structures

Code Comparison

jsonapi-serializer:

class MovieSerializer
  include JSONAPI::Serializer
  attributes :title, :year
  has_many :actors
end

Graphiti:

class MovieResource < ApplicationResource
  attribute :title, :string
  attribute :year, :integer
  has_many :actors
end

Summary

jsonapi-serializer is a lightweight, focused tool for JSON:API serialization, making it easy to implement basic JSON:API responses. It's widely adopted and simple to use for straightforward scenarios. However, it lacks advanced features and requires more manual configuration for complex use cases.

Graphiti, on the other hand, offers a more comprehensive solution for building JSON:API compliant APIs. It provides additional features like automatic relationship handling, query customization, and both serialization and deserialization. While it has a steeper learning curve, Graphiti offers more flexibility and power for complex API scenarios.

The choice between the two depends on the project's requirements, complexity, and the desired level of control over the API implementation.

A resource-focused Rails library for developing JSON:API compliant servers.

Pros of jsonapi-resources

  • More mature and established project with a larger community
  • Closer adherence to the JSON:API specification
  • Better integration with Rails conventions and ActiveRecord

Cons of jsonapi-resources

  • Less flexible and more opinionated approach
  • Steeper learning curve for developers new to JSON:API
  • Limited support for non-ActiveRecord data sources

Code Comparison

jsonapi-resources:

class BookResource < JSONAPI::Resource
  attributes :title, :author
  has_many :chapters
end

Graphiti:

class BookResource < ApplicationResource
  attribute :title, :string
  attribute :author, :string
  has_many :chapters
end

Both libraries aim to simplify the implementation of JSON:API compliant APIs in Ruby on Rails applications. jsonapi-resources offers a more traditional Rails approach with tight ActiveRecord integration, while Graphiti provides greater flexibility and customization options.

jsonapi-resources is better suited for projects that strictly adhere to JSON:API specifications and rely heavily on ActiveRecord. Graphiti, on the other hand, offers more adaptability for complex data sources and custom API designs.

Developers should consider their project requirements, team expertise, and desired level of customization when choosing between these libraries. Both have their strengths and can be effective tools for building robust APIs in Rails applications.

ActiveModel::Serializer implementation and Rails hooks

Pros of Active Model Serializers

  • Simpler setup and integration with Rails applications
  • More familiar syntax for Rails developers
  • Lightweight and focused solely on serialization

Cons of Active Model Serializers

  • Less flexible for complex data relationships
  • Limited support for filtering, sorting, and pagination
  • Slower performance for large datasets

Code Comparison

Active Model Serializers:

class UserSerializer < ActiveModel::Serializer
  attributes :id, :name, :email
  has_many :posts
end

Graphiti:

class UserResource < ApplicationResource
  attribute :name, :string
  attribute :email, :string
  has_many :posts
end

Key Differences

  • Graphiti provides a more comprehensive API solution, including querying, filtering, and pagination
  • Active Model Serializers focuses primarily on JSON serialization
  • Graphiti offers better performance for complex data structures and large datasets
  • Active Model Serializers has a simpler learning curve for Rails developers

Use Cases

  • Choose Active Model Serializers for simpler APIs with straightforward data structures
  • Opt for Graphiti when building complex, scalable APIs with advanced querying and filtering requirements

Community and Maintenance

  • Active Model Serializers has a larger user base and longer history
  • Graphiti is actively maintained and gaining popularity for its comprehensive feature set

Both libraries have their strengths, and the choice depends on the specific needs of your project, complexity of the API, and performance requirements.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Graphiti

CI Gem Version Ruby Style Guide semantic-release: angular

discord guides

Graphiti logo Graphiti is a resource-oriented framework that sits on top of your models (usually ActiveRecord) and exposes them via a JSON:API-compliant interface. It abstracts common concerns like serialization, filtering, sorting, pagination, and sideloading relationships, so you can build powerful APIs with minimal boilerplate. By defining resources instead of controllers and serializers, Graphiti helps you keep your API logic organized, consistent, and easy to maintain.

Examples

Here's an example resource from the example app just to give you a taste of the possibilities.

class EmployeeResource < ApplicationResource
  attribute :first_name, :string
  attribute :last_name, :string
  attribute :age, :integer
  attribute :created_at, :datetime, writable: false
  attribute :updated_at, :datetime, writable: false
  attribute :title, :string, only: [:filterable, :sortable]

  has_many :positions
  has_many :tasks
  many_to_many :teams
  polymorphic_has_many :notes, as: :notable
  has_one :current_position, resource: PositionResource do
    params do |hash|
      hash[:filter][:current] = true
    end
  end

  filter :title, only: [:eq] do
    eq do |scope, value|
      scope.joins(:current_position).merge(Position.where(title: value))
    end
  end

  sort :title do |scope, value|
    scope.joins(:current_position).merge(Position.order(title: value))
  end

  sort :department_name, :string do |scope, value|
    scope.joins(current_position: :department)
      .merge(Department.order(name: value))
  end
end

A pretty boilerplate controller that just interfaces with the resource

class EmployeesController < ApplicationController
  def index
    employees = EmployeeResource.all(params)
    respond_with(employees)
  end

  def show
    employee = EmployeeResource.find(params)
    respond_with(employee)
  end

  def create
    employee = EmployeeResource.build(params)

    if employee.save
      render jsonapi: employee, status: 201
    else
      render jsonapi_errors: employee
    end
  end

  def update
    employee = EmployeeResource.find(params)

    if employee.update_attributes
      render jsonapi: employee
    else
      render jsonapi_errors: employee
    end
  end

  def destroy
    employee = EmployeeResource.find(params)

    if employee.destroy
      render jsonapi: { meta: {} }, status: 200
    else
      render jsonapi_errors: employee
    end
  end
end

Now you can query your endpoints simply and powerfully, like:

Request: http://localhost:3000/api/v1/employees?filter[title][eq]=Future Government Administrator&filter[age][lt]=40

JSON-API response
{
  "data": [
    {
      "id": "1",
      "type": "employees",
      "attributes": {
        "first_name": "Quinn",
        "last_name": "Homenick",
        "age": 36,
        "created_at": "2025-03-21T23:04:40+00:00",
        "updated_at": "2025-03-21T23:04:40+00:00"
      },
      "relationships": {
        "positions": {
          "links": {
            "related": "/api/v1/positions?filter[employee_id]=1"
          },
          "data": [
            {
              "type": "positions",
              "id": "1"
            },
            {
              "type": "positions",
              "id": "2"
            }
          ]
        },
        "tasks": {
          "links": {
            "related": "/api/v1/tasks?filter[employee_id]=1"
          }
        },
        "teams": {
          "links": {
            "related": "/api/v1/teams?filter[employee_id]=1"
          }
        },
        "notes": {
          "links": {
            "related": "/api/v1/notes?filter[notable_id]=1&filter[notable_type][eql]=Employee"
          }
        },
        "current_position": {
          "links": {
            "related": "/api/v1/positions?filter[current]=true&filter[employee_id]=1"
          },
          "data": {
            "type": "positions",
            "id": "1"
          }
        }
      }
    }
  ],
  "included": [
    {
      "id": "1",
      "type": "positions",
      "attributes": {
        "title": "Future Government Administrator",
        "active": true
      },
      "relationships": {
        "employee": {
          "links": {
            "related": "/api/v1/employees/1"
          }
        },
        "department": {
          "links": {
            "related": "/api/v1/departments/3"
          }
        }
      }
    },
    {
      "id": "2",
      "type": "positions",
      "attributes": {
        "title": "Manufacturing Specialist",
        "active": false
      },
      "relationships": {
        "employee": {
          "links": {
            "related": "/api/v1/employees/1"
          }
        },
        "department": {
          "links": {
            "related": "/api/v1/departments/2"
          }
        }
      }
    }
  ],
  "meta": {}
}

Graphiti Guides

Join the Discord