GraphQL is a powerful & nimble API query language.
This post serves as a step-by-step “Getting Started” guide to adding GraphQL to your Ruby application.

1. Install the graphql-ruby gem

# Gemfile
gem 'graphql'

$ bundle install

$ rails generate graphql:install

2. Add GraphiQL to your Development Environment

Graphiql is a an excellent tool used to test your queries.

# Gemfile
gem 'graphiql-rails'

Add the GraphiQL engine to your routes.rb:

# config/routes.rb
Rails.application.routes.draw do
  # ...
  if Rails.env.development?
    mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql"

3. Add your data Types

You’ll need to add your data objects and their queryable fields

# app/graphql/types/playlist_type.rb

Types::PlaylistType = GraphQL::ObjectType.define do
  name "Playlist"
  description "A user\'s playlist"
  field :id, !types.ID
  field :title, types.String
  field :spotify_id, types.String

4. Create your Query schema

This schema is the structure of possible graphql calls.

Your GraphQL configuration files can be found in: app > graphql > query_type.rb

# app/graphql/query_type.rb

Types::QueryType = GraphQL::ObjectType.define do
  name "Query"
  # Add root-level fields here.
  # They will be entry points for queries on your schema.

  field :allPlaylists, types[Types::PlaylistType] do
    description "Playlists that belong to the user"
    resolve ->(obj, args, ctx) { { |x| x }

This creates a new field titled allPlaylists for your queries. This field is then further defined within the resolve block to further interact with your data model.

5. Success! 🎉

Now, when you visit your localhost:8080/graphiql you are able to query your data using GraphQL— like so:


This methodology was used to create Spotless-API, a helper-project I created that uses GraphQL to interface with the Spotify API.