jpskill.com
🛠️ 開発・MCP コミュニティ

rails-graphql-patterns

graphql-ruby gem を使った Rails アプリケーションで、GraphQL のスキーマ、型、リゾルバーなどを扱う際に、N+1 問題の検出やデータローダーの活用を促し、効率的な GraphQL 開発を支援するSkill。

📜 元の英語説明(参考)

GraphQL patterns for Rails applications using graphql-ruby gem. Automatically invoked when working with GraphQL schemas, types, resolvers, mutations, subscriptions, or the app/graphql directory. Triggers on "GraphQL", "schema", "resolver", "mutation", "query type", "subscription", "graphql-ruby", "field", "argument", "N+1 graphql", "dataloader".

🇯🇵 日本人クリエイター向け解説

一言でいうと

graphql-ruby gem を使った Rails アプリケーションで、GraphQL のスキーマ、型、リゾルバーなどを扱う際に、N+1 問題の検出やデータローダーの活用を促し、効率的な GraphQL 開発を支援するSkill。

※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。

⚡ おすすめ: コマンド1行でインストール(60秒)

下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。

🍎 Mac / 🐧 Linux
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o rails-graphql-patterns.zip https://jpskill.com/download/10046.zip && unzip -o rails-graphql-patterns.zip && rm rails-graphql-patterns.zip
🪟 Windows (PowerShell)
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/10046.zip -OutFile "$d\rails-graphql-patterns.zip"; Expand-Archive "$d\rails-graphql-patterns.zip" -DestinationPath $d -Force; ri "$d\rails-graphql-patterns.zip"

完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。

💾 手動でダウンロードしたい(コマンドが難しい人向け)
  1. 1. 下の青いボタンを押して rails-graphql-patterns.zip をダウンロード
  2. 2. ZIPファイルをダブルクリックで解凍 → rails-graphql-patterns フォルダができる
  3. 3. そのフォルダを C:\Users\あなたの名前\.claude\skills\(Win)または ~/.claude/skills/(Mac)へ移動
  4. 4. Claude Code を再起動

⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。

🎯 このSkillでできること

下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。

📦 インストール方法 (3ステップ)

  1. 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
  2. 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
  3. 3. 展開してできたフォルダを、ホームフォルダの .claude/skills/ に置く
    • · macOS / Linux: ~/.claude/skills/
    • · Windows: %USERPROFILE%\.claude\skills\

Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。

詳しい使い方ガイドを見る →
最終更新
2026-05-18
取得日時
2026-05-18
同梱ファイル
1

📖 Skill本文(日本語訳)

※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。

Rails GraphQL パターン

graphql-ruby を使用して Rails アプリケーションで GraphQL API を構築するためのパターンです。

この Skill が適用される場合

  • GraphQL スキーマと型の設計
  • リゾルバーとミューテーションの実装
  • クエリの最適化と N+1 問題の防止
  • GraphQL での認証/認可の処理
  • リアルタイム更新のためのサブスクリプションの実装
  • app/graphql ディレクトリの構造化

クイックリファレンス

コンポーネント 目的 場所
Types オブジェクトの形状を定義します app/graphql/types/
Queries 読み取り操作 app/graphql/types/query_type.rb
Mutations 書き込み操作 app/graphql/mutations/
Resolvers クエリロジック app/graphql/resolvers/
Sources DataLoader バッチ処理 app/graphql/sources/

詳細なドキュメント

  • patterns.md - 例を含む完全な GraphQL パターン

型定義の基本

# app/graphql/types/user_type.rb
module Types
  class UserType < Types::BaseObject
    field :id, ID, null: false
    field :email, String, null: false
    field :name, String
    field :created_at, GraphQL::Types::ISO8601DateTime, null: false

    field :posts, [Types::PostType], null: true
    field :posts_count, Integer, null: false

    def posts_count
      object.posts.size  # カウンターキャッシュには size を使用します
    end
  end
end

ミューテーションの基本

# app/graphql/mutations/create_post.rb
module Mutations
  class CreatePost < BaseMutation
    argument :title, String, required: true
    argument :content, String, required: true

    field :post, Types::PostType
    field :errors, [String], null: false

    def resolve(title:, content:)
      post = context[:current_user].posts.build(title: title, content: content)

      if post.save
        { post: post, errors: [] }
      else
        { post: nil, errors: post.errors.full_messages }
      end
    end
  end
end

DataLoader による N+1 防止

# app/graphql/sources/record_loader.rb
class Sources::RecordLoader < GraphQL::Dataloader::Source
  def initialize(model_class)
    @model_class = model_class
  end

  def fetch(ids)
    records = @model_class.where(id: ids).index_by(&:id)
    ids.map { |id| records[id] }
  end
end

# 型での使用例
def author
  dataloader.with(Sources::RecordLoader, User).load(object.user_id)
end

主要な原則

  • 型安全性: すべてのフィールドに対して明示的な型を定義します
  • Null 処理: null: true/false について意図的に行います
  • バッチロード: DataLoader を使用して N+1 クエリを防止します
  • 認可: フィールドレベルで権限を確認します
  • エラー処理: 例外ではなく、構造化されたエラーを返します
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開

Rails GraphQL Patterns

Patterns for building GraphQL APIs in Rails applications using graphql-ruby.

When This Skill Applies

  • Designing GraphQL schemas and types
  • Implementing resolvers and mutations
  • Optimizing queries and preventing N+1 problems
  • Handling authentication/authorization in GraphQL
  • Implementing subscriptions for real-time updates
  • Structuring the app/graphql directory

Quick Reference

Component Purpose Location
Types Define object shapes app/graphql/types/
Queries Read operations app/graphql/types/query_type.rb
Mutations Write operations app/graphql/mutations/
Resolvers Query logic app/graphql/resolvers/
Sources DataLoader batching app/graphql/sources/

Detailed Documentation

  • patterns.md - Complete GraphQL patterns with examples

Type Definition Basics

# app/graphql/types/user_type.rb
module Types
  class UserType < Types::BaseObject
    field :id, ID, null: false
    field :email, String, null: false
    field :name, String
    field :created_at, GraphQL::Types::ISO8601DateTime, null: false

    field :posts, [Types::PostType], null: true
    field :posts_count, Integer, null: false

    def posts_count
      object.posts.size  # Use size for counter cache
    end
  end
end

Mutation Basics

# app/graphql/mutations/create_post.rb
module Mutations
  class CreatePost < BaseMutation
    argument :title, String, required: true
    argument :content, String, required: true

    field :post, Types::PostType
    field :errors, [String], null: false

    def resolve(title:, content:)
      post = context[:current_user].posts.build(title: title, content: content)

      if post.save
        { post: post, errors: [] }
      else
        { post: nil, errors: post.errors.full_messages }
      end
    end
  end
end

N+1 Prevention with DataLoader

# app/graphql/sources/record_loader.rb
class Sources::RecordLoader < GraphQL::Dataloader::Source
  def initialize(model_class)
    @model_class = model_class
  end

  def fetch(ids)
    records = @model_class.where(id: ids).index_by(&:id)
    ids.map { |id| records[id] }
  end
end

# Usage in type
def author
  dataloader.with(Sources::RecordLoader, User).load(object.user_id)
end

Key Principles

  • Type Safety: Define explicit types for all fields
  • Null Handling: Be intentional about null: true/false
  • Batch Loading: Use DataLoader to prevent N+1 queries
  • Authorization: Check permissions at field level
  • Error Handling: Return structured errors, not exceptions