[Awesome Ruby Gem - Graphiti] Use graphiti-rails gem to provide RESTful API
graphiti-rails
Graphiti makes RESTful Resources a first-class concept. This enables reading and writing a graph of data in a single request, a schema with backwards-compatible guarantee, end-to-end integration test patterns, seamless microservices and much more.
This quickstart will use Rails with ActiveRecord to give an overview of Graphiti functionality on familiar ground. For a more in-depth breakdown, head to the Guides - https://www.graphiti.dev/guides.
Installation
You can install it as a gem:
1 | gem install graphiti-rails |
or add it into a Gemfile (Bundler):
1 | # Gemfile |
Then, run bundle install
.
1 | bundle install |
Usages
Defining a Model
First, let’s define our Model:
1 | bundle exec rails generate model Post title:string upvotes:integer active:boolean |
Config
The .graphiticfg.yml
file lives in the root directory of your application. It holds configuration we need to reuse across a variety of contexts (primarily generates and rake tasks). If you use our template to create your application, it’s created for you.
Primarily this is used to hold your “API namespace”:
1 | # .graphiticfg.yml |
If this file doesn’t exist you may get unexpected errors - make sure to create it!
Defining a Resource
A Resource defines how to query and persist your Model. In other words: a Model is to the database as Resource is to the API.
Now we can use the built-in generator to define our Resource, corresponding Endpoint, and Integration Tests.
1 | bundle exec rails g graphiti:resource Post title:string upvotes:integer active:boolean |
Check generated code
PostsController
1 | # app/controllers/api/v1/posts_controller.rb |
ApplicationController
1 | # app/controller/application_controller.rb |
Route
1 | # config/routes.rb |
ApplicationResource
1 | # app/resources/application_resource.rb |
PostResource
1 | # app/resources/post_resource.rb |
Call PostResource
built-in methods:
1 | rails c |
Run
You’ll see a number of files created. Now run your app!:
1 | bundle exec rails s |
Verify http://localhost:3000/api/v1/posts
renders JSON correctly. Now we just need data.
Seeding Data
Edit db/seeds.rb
to create a few Posts:
1 | Post.create!(title: 'My title', upvotes: 10, active: true) |
And run the script:
1 | bundle exec rails db:seed |
Now load http://localhost:3000/api/v1/posts
. You should have 3 Posts in your database!
FAQs
undefined method `delete_suffix’ for nil:NilClass (NoMethodError)
1 | bundle exec rails g graphiti:resource Post title:string upvotes:integer active:boolean |
File .graphiticfg.yml
doesn’t exist you may get unexpected errors - make sure to create it!
1 | # .graphiticfg.yml |
File unchanged! The supplied flag value not found! config/routes.rb
1 | bundle exec rails g graphiti:resource Post title:string upvotes:integer active:boolean |
TODO
uninitialized constant ApplicationResource (NameError)
1 | bundle exec rails g graphiti:resource Post title:string upvotes:integer active:boolean |
Try to run bundle exec rails g graphiti:resource Post title:string upvotes:integer active:boolean
again to fix that issue.
1 | bundle exec rails g graphiti:resource Post title:string upvotes:integer active:boolean |
NoMethodError (undefined method `respond_with’
1 | NoMethodError (undefined method `respond_with' for #<Restful::Api::V1::DataDictionariesController:0x000000000131a0> |
Append include Graphiti::Rails::Responders
into file application_controller.rb
.
1 | # app/controller/application_controller.rb |
Could not find type :jsonb! This was specified on attribute :annotations within resource DataDictionaryResource (Graphiti::Errors::TypeNotFound)
1 | RAILS_ENV=production rails c |
TODO
/usr/local/lib/ruby/3.0.0/debug.rb:6:in <main>': undefined method
>’ for nil:NilClass (NoMethodError)
1 | # /usr/local/lib/ruby/3.0.0/debug.rb |
References
[1] Stylish Graph APIs | Guides - https://www.graphiti.dev/guides/
[2] graphiti-api/graphiti: Stylish Graph APIs - https://github.com/graphiti-api/graphiti
[3] Migrating to graphiti-rails - https://www.graphiti.dev/guides/graphiti-rails-migration
[4] graphiti-api/graphiti-rails - https://github.com/graphiti-api/graphiti-rails
[5] graphiti-rails | RubyGems.org | your community gem host - https://rubygems.org/gems/graphiti-rails/
[6] JSON:API — A specification for building APIs in JSON - https://jsonapi.org/
[7] JSON:API — Implementations - https://jsonapi.org/implementations/