Building GraphQL API in Rails A Quick Setup Guide

Rails での GraphQL API の構築: クイック セットアップ ガイド

GraphQL, a modern query language for APIs, paired with Ruby on Rails, offers a powerful combination for streamlined API development. In this quick guide, we’ll walk through the essential steps to set up a GraphQL API in a Ruby on Rails application, providing you with a solid foundation for efficient and flexible web development.

Step 1: Create a new Rails project

bash

rails new graphql_example

cd graphql_example

Step 2: Add necessary gems to your Gemfile

Add the following gems to your Gemfile:

ruby

gem 'graphql'

gem 'graphiql-rails', group: :development

走る バンドルインストール to install the new gems.

Step 3: Create a GraphQL schema

Create a file named app/graphql/types/query_type.rb:

ruby

# app/graphql/types/query_type.rb

Types::QueryType = GraphQL::ObjectType.define do

 name 'Query'

 description 'The root query type'

 field :hello do

 type types.String

 description 'An example field'

 resolve ->(_obj, _args, _ctx) { 'Hello, GraphQL!' }

 終わり

終わり

Step 4: Create a GraphQL controller

Generate a controller to handle GraphQL queries:

bash

rails generate controller graphql execute

Replace the content of app/controllers/graphql_controller.rb with the following:

ruby

# app/controllers/graphql_controller.rb

class GraphqlController < ApplicationController

 def execute

 variables = ensure_hash(params[:variables])

 query = params[:query]

 operation_name = params[:operationName]

 context = {

 # Add any necessary context values here, such as current_user or session

 }

 result = Schema.execute(query, variables: variables, context: context, operation_name: operation_name)

 render json: result

 終わり

 プライベート

 def ensure_hash(variables)

 case variables

 when String

 JSON.parse(variables) || {}

 when Hash

 variables

 when nil

 {}

 それ以外

 raise ArgumentError, "Invalid variables: #{variables}"

 終わり

 終わり

終わり

Step 5: Create the GraphQL schema

Create a file named app/graphql/schema.rb:

ruby

# app/graphql/schema.rb

Schema = GraphQL::Schema.define do

 query(Types::QueryType)

 # Add mutation types if needed

終わり

Step 6: Configure routes

Update your config/routes.rb to include the GraphQL endpoint:

ruby

# config/routes.rb

Rails.application.routes.draw は実行します

 post '/graphql', to: 'graphql#execute'

 if Rails.env.development?

 mount GraphiQL::Rails::Engine, at: '/graphiql', graphql_path: '/graphql'

 終わり

終わり

Step 7: Run your Rails server

bash

rails s

Visit http://localhost:3000/graphiql in your browser to use GraphiQL, an in-browser IDE for exploring GraphQL.

In the GraphiQL interface, you can enter a query like:

graphql

{

 hello

}

And you should receive a response:

json

{

 "data": {

 "hello": "Hello, GraphQL!"

 }

}

This is a simple example, but you can expand your GraphQL schema with more types and mutations to suit your application’s needs.

結論

By following this quick setup guide, you’ll be well-equipped to integrate GraphQL into your Ruby on Rails projects seamlessly. Harness the power of GraphQL to create APIs that cater to your application’s specific needs while providing an excellent developer and user experience. Get ready to elevate your API development game with the simplicity and flexibility of GraphQL in Ruby on Rails. Elevate your company’s digital presence with top-tier Ruby on Rails developers から レールカーマ. Our seasoned professionals bring a wealth of experience and innovation to the table, ensuring your projects are not only executed seamlessly but exceed industry standards.

関連記事

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

jaJapanese