0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

graphql-ruby で GraphQLスキーマを自動更新する GitHub Actions を実装する

Last updated at Posted at 2025-05-12

はじめに

GitHub Actions を使用して Rails アプリケーションの GraphQL スキーマを自動的に更新するようにしました。

自動化によって以下のメリットが得られるメリット

  • スキーマの更新忘れを防止できる
  • 常に最新のスキーマ定義が維持される
  • 開発者の作業負担が軽減される

GitHub Actionsワークフローの実装

まずはスキーマを作成してくれる Rake タスクを作成します。

このタスクを設定することで、bundle exec rails graphql:schema:json コマンドでスキーマを生成できるようになります。

lib/tasks/graphql.rake
require "graphql/rake_task"
GraphQL::RakeTask.new(schema_name: "MyappSchema")

次にワークフローを定義します。

.github/workflows/update-graphql-schema.yml
name: Update GraphQL Schema

on:
  pull_request:
    branches: [ main, develop ]
    paths:
      - 'app/graphql/**/*.rb'

permissions:
  contents: write
  pull-requests: write

jobs:
  update-schema:
    name: Update GraphQL Schema
    runs-on: ubuntu-latest

    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: password
          MYSQL_DATABASE: myapp_test
          TZ: "Asia/Tokyo"
        ports:
          - 3306:3306
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=5

    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          ref: ${{ github.head_ref }}
          fetch-depth: 0

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.1.7'
          bundler-cache: false

      - name: Update Bundler
        run: |
          gem update --system
          gem install bundler -v 2.4.22

      - name: Install dependencies
        run: |
          bundle _2.4.22_ install

      - name: Update database configuration
        run: |
          sed -i 's/host: db/host: 127.0.0.1/g' config/database.yml
          sed -i 's/password: password/password: password/g' config/database.yml

      - name: Generate GraphQL schema
        run: bundle _2.4.22_ exec rails graphql:schema:json

      - name: Commit and push schema changes
        run: |
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add schema.json
          git commit -m "chore: Update GraphQL schema" schema.json || echo "No changes to commit"
          git push || echo "No changes to push"

動作確認

app/graphql/配下のRubyファイルを変更すると、CIが実行されます。
仮にスキーマに差分が発生しt場合は、GitHubがコミットを積んでくれます。

image.png

まとめ

この自動化により、フロントエンドとバックエンド間のGraphQLスキーマの同期を簡単に保つことができ、開発効率の向上につながります🙋‍♂️

Twitter もよろしければフォローお願いします🙏

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?