[Ruby] How to split each GraphQL query into a file

Introduction

Recently, I am implementing API using graphql-ruby. In the beginning, I wrote the logic in query_type.rb. But when the number of queries is large, it's hard to see! Therefore, I would like to explain how to divide each query without writing logic in query_type.rb.

Thing you want to do

First, suppose query_type.rb, which originally wrote the query, looks like this.

types/query_type.rb


module Types
  class Query < Types::BaseObject
    field :user, ObjectTypes::UserType, null: false do
    argument :id, ID, required: true
    end

    def user(id:)
      User.find(id)
    end


    field :post, ObjectTypes::PostType, null: false do
    argument :id, ID, required: true
    end

    def post(id:)
      Post.find(id)
    end
  end

It's a simple query that takes an id as an argument and returns user and post. I would like to split each query that returns this user and post into two files (user.rb, post.rb).

Split the query

Now let's split the query. First, change query_types.rb as follows.

types/query_type.rb


module Types
  class QueryType < Types::BaseObject
    field :user, resolver: Queries::User #Define a query that returns user
    field :post, resolver: Queries::Post #Define a query that returns a post
  end

Define only field in query_type.rb as above. Use resolever to specify where the actual logic is. Next, define the actual logic (resolver). This time, I created a folder called queries directly under the graphql folder.

queries/user.rb


module Queries
  class User < BaseQuery
    type ObjectTypes::UserType, null: false
    argument :id, ID, required: true

    def resolve(id:)
      User.find(id)
    end
  end

Now that we have written the resolver defined in query_type.rb, we can get the user. There is no problem if you prepare post.rb for post as well. I personally highly recommend it because it will be much easier to see just by splitting the file!

Recommended Posts

[Ruby] How to split each GraphQL query into a file
How to create a query using variables in GraphQL [Using Ruby on Rails]
How to divide a two-dimensional array into four with ruby
[Xcode] How to add a README.md file
How to split Spring Boot message file
[Ruby] How to convert CSV file to Yaml (Yml)
[Ruby] How to generate a random alphabet string
How to implement Pagination in GraphQL (for ruby)
How to jump from Eclipse Java to a SQL file
[Ruby] How to find the sum of each digit
How to launch another command in a Ruby program
How to download a file (Servlet, HTML, Apache, Tomcat)
How to convert a file to a byte array in Java
[Ruby] How to extract a specific value from an array under multiple conditions [select / each]
How to request a CSV file as JSON with jMeter
How to use Ruby return
How to load UI created by Xib file into View
How to change a string in an array to a number in Ruby
How to leave a comment
[Easy] How to automatically format Ruby erb file with vsCode
[Ruby] How to comment out
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to display a graph in Ruby on Rails (LazyHighChart)
[Ruby] How to retrieve the contents of a double hash
Ruby: How to use cookies
I want to create a Parquet file even in Ruby
[Ruby] How to write blocks
How to insert a video
How to create a method
How to create a jar file or war file using the jar command
How to make a jar file with no dependencies in Maven
How to load a Spring upload file and view its contents
How to read a file and treat it as standard input
How to open a script file from Ubuntu with VS code
[chown] How to change the owner of a file or directory
How to build a little tricky with dynamic SQL query generation
How to add columns to a table
How to iterate infinitely in Ruby
How to make a Java container
How to install ruby through rbenv
How to use Ruby on Rails
How to sign a Minecraft MOD
How to make a JDBC driver
How to convert erb file to haml
How to install Bootstrap in Ruby
[Ruby] How to use any? Method
[Java] How to create a folder
How to write a ternary operator
[Swift] How to send a notification
How to make a splash screen
How to make a Jenkins plugin
How to make a Maven project
[Beginner] How to delete NO FILE
How to use Ruby inject method
How to make a Java array
How to execute Ruby irb (interactive ruby)
[IntelliJ IDEA] How to automatically add final when saving a Java file
How to build a Ruby on Rails environment using Docker (for Docker beginners)
How to run a GIF file from the Linux command line (Ubuntu)
How to record JFR (Java Flight Recorder) and output a dump file
How to build a Ruby on Rails development environment with Docker (Rails 6.x)