GraphQL API with your Rails API — part 1
What is GraphQL?
GraphQl is a query language for APIs and a server-side runtime for executing those queries against your database. GraphQL allows you to fetch multiple resources with a single request by specifying the exact data you want, thus removing the need for multiple API calls. Since GraphQL is not dependent on any specific languages, thus it can be implemented with any programming language.
For this tutorial, we will quickly implement GraphQL into our already set up Rails API project.
Setting Up GraphQL
First things first, we need to access our Gemfile, which can be found under the project directory. We will add 2 gems to our file, graphql, and graphql-rails. The first gem, graphql can be added anywhere in our Gemfile. The second gem, graphql-rails should be added under the development dependencies.

Like any other gems, we need to install these using the bundle install command once the file has been saved.
Next, we install boilerplate code for the graphql-ruby gem to work. We can do so using the rails g graphql:install, which will generate several files such as grapghql_container.rb, a graphql directory inside the app directory, and changes to the routes.rb file. Next, we need to uncomment require “sprockets/railtie” from our application.rb file to allow Rails to render pages.

Under the app/assets/config directory, we will create a file called manifest.js. We will add two lines that tell Rails to precompile the 2 files before it’s rendered.

Once we have everything installed, we need to make changes to our routes.rb file to allow us to test our graphQL queries. We open up our routes.rb file which can be found under the config directory.

When we are in development mode, the /graphql route will take you to the query editor/playground where you can test your code.
Setting Up your Model and Query Type
We can set our Rails model how we normally do it. We will create a Note model with title and body fields.

Once the model has been created, we can run rails db:migrate command to create our tables. To access the model we just created using GraphQL, we must create a NoteType.

The command creates a GraphQL object tye Note with 3 fields, id, title, and body. The ! after the field type tells GraphQL, queries should never return a null value. Next, we will create an Input type for the note object, which defines objects that can be passed to queries as arguments. To keep our code structured, we will create an input directory under the graphql/types directory. Within the input directory, we will create a note_input_type.rb file.

The NoteInputType class inherits from the Types::BaseInputObject which is provided with the boilerplate code. It accepts two arguments of string type, title, and body.
In the next blog, we will go ahead and set up our queries and mutations for GrahQL.
To learn more about this: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-ruby-on-rails-graphql-api#prerequisites