A simple case study of how to use GraphQL with Ruby on Rails
- Ruby v2.4.1
- Rails v5.1.1
- GraphQL v1.6.3
The project base code was created with the following command:
$ rails new rails-graphql --api
The - -api option was used to create a lightweight version of the full-stack framework, containing only what I need start to build a REST API.
- Clone this repo
- At rails-graphql folder execute
bundle install
to install all dependencies - Execute all migrations in SQLite3 with
rake db:migrate
- Put some data in SQLite3 with
rake db:seed
(see db/seeds.rb) - Start the server with
rails s
- Using the terminal, query it with cURL:
curl -XGET http://localhost:3000/movies -d "query={
movie(id: 1) {
title,
year,
actors {
name
}
}
}"
- In another terminal, execute the IRB console with
bundle exec rails c
- Add some data to Movie and Actor, then place Actor inside Movie
movie = Movie.create!(title: "Indiana Jones", year: 1984, summary: "Temple of Doom")
actor = Actor.create!(name: "Harrison Ford", bio: "Some long biography about this actor")
movie.actors << actor
- Exit IRB and query it using cURL:
curl -XGET http://localhost:3000/movies -d "query={
movie(id: 5) {
title,
year,
actors {
name
}
}
}"
You could also query about Actors (by ID) and Movies (by year) -- see app/types/query_type.rb for more.
- Create some tests
- Create a ERB interface