DEV Community

Povilas Jurčys
Povilas Jurčys

Posted on

GraphqlRails 1.0.0 has been released

GraphqlRails 1.0.0 has been released

I am very annoyed when I see any gem with 0.x.x version. Those gems are OK, but their authors just are afraid to accept the responsibility for announcing gem as stable. For about two years I was one of those annoying authors. All that time GraphqlRails was under active development and heavy usage, but it reached only 0.8.0 version! But I finally have a courage to stop calling it risky. It’s mature, it’s handy, it’s developer friendly. I work with around 20 other developers. We all use it daily. No one ever said “um… let’s go back to graphql-ruby”.

So here we go: version 1.0.0 is here!

It’s been done a lot since 0.8.0 version. Until now GraphqlRails was more or less only syntactic sugar on top of graphql-ruby gem. But starting with 1.0.0 GraphqlRails brings it’s own philosophy and features which I would like to introduce.

gem 'graphql_rails', '1.0.0' # finally!
Enter fullscreen mode Exit fullscreen mode

Install generator and strict structure

If you have never used GraphqlRails before then I encourage you to use rails g graphql_rails:install script in order to have recommended structure in place. GraphqlRails expects that you put your files in fixed places (same as Ruby on Rails does). This requirement will give you development performance boost when working on multiple applications and it will allow to build extensions a lot easier.

bin/rails g graphql_rails:install
#      exist  app/controllers
#      create  app/controllers/graphql_controller.rb
#      create  app/controllers/graphql
#      create  app/controllers/graphql/graphql_application_controller.rb
#      create  app/controllers/graphql/example_users_controller.rb
#      create  app/graphql
#      create  app/graphql/graphql_router.rb
#       route  post "/graphql", to: "graphql#execute"
Enter fullscreen mode Exit fullscreen mode

Multiple schemas in one place

Although GraphQL promises “no more versioning”, but in large projects you will often face the issue where some components might break because of change in GraphQL.

For example, I work with project where front-end and mobile apps make requests to GraphQL. Front-end can adapt to changes in GraphQL easily, but mobile apps can't, because they need to go through all that Google/Apple store validation process. For this reason there are “edge” and LTS (long term support) GraphQL endpoints. But thanks to GraphqlRails, there is single schema file with multiple groups which looks something like this:

# app/graphql/graphql_router.rb
GraphqlRouter = GraphqlRails::Router.draw do
  resources :users

  group :mobile do
    resources :mobile_users
  end

  group :admin do
    resources :admin_users
  end
end
Enter fullscreen mode Exit fullscreen mode

In order to use specific schema, we add this:

# app/controllers/graphql_admin_controller.rb
class GraphqlAdminController < ApplicationController
  def execute
    render json: GraphqlRails::QueryRunner.call(
      params: params,
      context: graphql_context,
      group: :admin # this is where you define desired group
    )
  end
end
Enter fullscreen mode Exit fullscreen mode

Fields with arguments

This is the feature requested by many developers. GraphqlRails didn't have support for attributes with arguments. But now you don't need to worry about this, because 1.0.0 version has this feature which you can use like this:

class User
  include GraphqlRails::Model

  graphql
    .attribute(:avatar_url)
    .permit(width: :int!, height: :int!) # input attributes

  def avatar_url(width:, height:)
    avatar_image.scale(width, height).url
  end
end
Enter fullscreen mode Exit fullscreen mode

Dropped compatibility with old graphql-ruby

Until now GraphqlRails used old style graphql-ruby DSL. But graphql-ruby has moved to new DSL long time ago and added a few nice features in it. In order to support those features GraphqlRails started to use new DSL too. As a side affect, some old DSL became incompatible with GraphqlRails.

Final thoughts

I covered only things that where improved in 1.0.0 version, but if you have never tried GraphqlRails before I recommend to start from https://samesystem.github.io/graphql_rails where you can find all the details about features which were not covered in this post including controller actions, decorators, rspec helpers and much more.

Try this gem and you will be surprised how smoothly GraphqlRails integrates with Ruby On Rails.

Top comments (2)

Collapse
 
michelson profile image
miguel michelson • Edited

Nice Library. I currently use graphql-ruby on my project, but I would like to give this library a try. This gem let you avoid N+1 queries ?

Collapse
 
povilasjurcys profile image
Povilas Jurčys • Edited

GraphqlRails supports same extensions as graphql-ruby so github.com/exAspArk/batch-loader might be helpful for you