Today I Learned

hashrocket A Hashrocket project

Ruby GraphQL Generators

Today I Learned the GraphQL ruby gem includes generators for types, mutations and other fun things.

rails g graphql:object
rails g graphql:input
rails g graphql:interface
rails g graphql:union
rails g graphql:enum
rails g graphql:scalar
rails g graphql:mutation

What's even neater is if the name of thing things you're generating matches an existing ActiveRecord model, it will scaffold all the database columns as fields!

If you have a model Book like:

class Book < ApplicationRecord
  attribute :id
  attribute :author_id
  attribute :title
  attribute :published_at
end

Running the object generator rails g graphql:object Book will produce:

module Types
  class BookType < Types::BaseObject
    field :id, ID
    field :author_id, ID
    field :title, String
    field :published_at, GraphQL::Types::ISO8601DateTime
  end
end
See More #rails TILs
Looking for help? Hashrocket has been an industry leader in Ruby on Rails since 2008. Rails is a core skill for each developer at Hashrocket, and we'd love to take a look at your project. Contact us and find out how we can help you.