├── .gitignore ├── .rubocop.yml ├── .ruby-version ├── .travis.yml ├── Aptfile ├── Gemfile ├── Gemfile.lock ├── README.md ├── Rakefile ├── app ├── controllers │ ├── application_controller.rb │ ├── queries_controller.rb │ └── shas_controller.rb ├── graph │ ├── fields │ │ └── fetch_field.rb │ ├── queries │ │ ├── introQuery.graphql │ │ ├── introspectHumanType.graphql │ │ ├── introspectSchema.graphql │ │ ├── invalidFields.graphql │ │ ├── invalidNestedFragments.graphql │ │ ├── readHero.graphql │ │ ├── readHeroByEpisode.graphql │ │ ├── readLukesFriends.graphql │ │ └── readSiblings.graphql │ ├── star_wars_schema.rb │ └── types │ │ ├── character_interface.rb │ │ ├── droid_type.rb │ │ ├── episode_enum.rb │ │ ├── human_type.rb │ │ └── query_type.rb └── models │ ├── character.rb │ ├── droid.rb │ ├── friendship.rb │ └── human.rb ├── bin ├── bundle ├── rails ├── rake ├── setup └── update ├── config.ru ├── config ├── application.rb ├── boot.rb ├── cable.yml ├── database.yml ├── database.yml.travis ├── environment.rb ├── environments │ ├── development.rb │ ├── production.rb │ └── test.rb ├── initializers │ ├── application_controller_renderer.rb │ ├── assets.rb │ ├── backtrace_silencers.rb │ ├── cookies_serializer.rb │ ├── filter_parameter_logging.rb │ ├── graphiql.rb │ ├── inflections.rb │ ├── mime_types.rb │ ├── new_framework_defaults.rb │ ├── session_store.rb │ └── wrap_parameters.rb ├── locales │ └── en.yml ├── puma.rb ├── routes.rb └── secrets.yml ├── db ├── migrate │ ├── 20150711200126_create_humen.rb │ ├── 20150711200156_create_droids.rb │ └── 20150711200623_create_friendships.rb ├── schema.rb └── seeds.rb ├── public ├── 404.html ├── 422.html ├── 500.html ├── favicon.ico └── robots.txt └── test ├── controllers ├── .keep └── queries_controller_test.rb ├── fixtures └── .keep ├── helpers └── .keep ├── integration └── .keep ├── mailers └── .keep ├── models └── .keep └── test_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files for more about ignoring files. 2 | # 3 | # If you find yourself ignoring temporary files generated by your text editor 4 | # or operating system, you probably want to add a global ignore instead: 5 | # git config --global core.excludesfile '~/.gitignore_global' 6 | 7 | # Ignore bundler config. 8 | /.bundle 9 | 10 | # Ignore all logfiles and tempfiles. 11 | /log/* 12 | !/log/.keep 13 | /tmp 14 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | # This configuration is based on `rmosolgo/graphql-ruby`'s rubocop configuration 2 | 3 | AllCops: 4 | DisabledByDefault: true 5 | TargetRubyVersion: 2.3 6 | 7 | # def ... 8 | # end 9 | Lint/DefEndAlignment: 10 | EnforcedStyleAlignWith: def 11 | 12 | # value = if 13 | # # ... 14 | # end 15 | Lint/EndAlignment: 16 | EnforcedStyleAlignWith: variable 17 | 18 | Metrics/ParameterLists: 19 | Max: 6 20 | CountKeywordArgs: false 21 | 22 | Style/ClassAndModuleChildren: 23 | EnforcedStyle: nested 24 | 25 | Style/EmptyLineBetweenDefs: 26 | AllowAdjacentOneLineDefs: true 27 | 28 | Style/IndentationWidth: 29 | Width: 2 30 | 31 | Style/LambdaCall: 32 | EnforcedStyle: call 33 | 34 | Style/LeadingCommentSpace: 35 | Enabled: true 36 | 37 | Style/MethodName: 38 | EnforcedStyle: snake_case 39 | 40 | Style/WordArray: 41 | EnforcedStyle: brackets 42 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | 2.3.1 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: ruby 3 | rvm: 4 | - 2.3.1 5 | before_install: 6 | - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test 7 | - sudo apt-get -qq update 8 | - sudo apt-get install build-essential 9 | - sudo apt-get -qq install g++-5 10 | - sudo unlink /usr/bin/g++ && sudo ln -s /usr/bin/g++-5 /usr/bin/g++ 11 | - g++ --version 12 | - sudo apt-get install bison 13 | - wget https://github.com/graphql/libgraphqlparser/archive/v0.5.0.tar.gz 14 | - tar -xzvf v0.5.0.tar.gz 15 | - cd libgraphqlparser-0.5.0/ && sudo cmake . && sudo make && sudo make install 16 | - psql -c 'create database travis_ci_test;' -U postgres 17 | script: 18 | - bundle exec rake db:test:prepare 19 | - bundle exec rake 20 | -------------------------------------------------------------------------------- /Aptfile: -------------------------------------------------------------------------------- 1 | cmake 2 | flex 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | ruby '2.3.1' 4 | 5 | gem 'graphql' 6 | gem 'graphiql-rails' 7 | gem 'react-rails' 8 | gem 'rails_12factor', group: :production 9 | gem 'rails', '~> 5.0' 10 | gem 'pg' 11 | gem 'sass-rails', '~> 5.0' 12 | gem 'uglifier', '>= 1.3.0' 13 | gem 'jquery-rails' 14 | 15 | group :development, :test do 16 | gem 'pry' 17 | gem 'better_errors' 18 | gem 'byebug' 19 | gem 'spring' 20 | gem 'minitest' 21 | end 22 | 23 | group :development do 24 | gem 'web-console', '~> 2.0' 25 | end 26 | 27 | gem 'rubocop', require: false 28 | 29 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | actioncable (5.0.1) 5 | actionpack (= 5.0.1) 6 | nio4r (~> 1.2) 7 | websocket-driver (~> 0.6.1) 8 | actionmailer (5.0.1) 9 | actionpack (= 5.0.1) 10 | actionview (= 5.0.1) 11 | activejob (= 5.0.1) 12 | mail (~> 2.5, >= 2.5.4) 13 | rails-dom-testing (~> 2.0) 14 | actionpack (5.0.1) 15 | actionview (= 5.0.1) 16 | activesupport (= 5.0.1) 17 | rack (~> 2.0) 18 | rack-test (~> 0.6.3) 19 | rails-dom-testing (~> 2.0) 20 | rails-html-sanitizer (~> 1.0, >= 1.0.2) 21 | actionview (5.0.1) 22 | activesupport (= 5.0.1) 23 | builder (~> 3.1) 24 | erubis (~> 2.7.0) 25 | rails-dom-testing (~> 2.0) 26 | rails-html-sanitizer (~> 1.0, >= 1.0.2) 27 | activejob (5.0.1) 28 | activesupport (= 5.0.1) 29 | globalid (>= 0.3.6) 30 | activemodel (5.0.1) 31 | activesupport (= 5.0.1) 32 | activerecord (5.0.1) 33 | activemodel (= 5.0.1) 34 | activesupport (= 5.0.1) 35 | arel (~> 7.0) 36 | activesupport (5.0.1) 37 | concurrent-ruby (~> 1.0, >= 1.0.2) 38 | i18n (~> 0.7) 39 | minitest (~> 5.1) 40 | tzinfo (~> 1.1) 41 | arel (7.1.4) 42 | ast (2.3.0) 43 | babel-source (5.8.35) 44 | babel-transpiler (0.7.0) 45 | babel-source (>= 4.0, < 6) 46 | execjs (~> 2.0) 47 | better_errors (2.1.1) 48 | coderay (>= 1.0.0) 49 | erubis (>= 2.6.6) 50 | rack (>= 0.9.0) 51 | binding_of_caller (0.7.2) 52 | debug_inspector (>= 0.0.1) 53 | builder (3.2.3) 54 | byebug (9.0.6) 55 | coderay (1.1.1) 56 | coffee-script-source (1.12.2) 57 | concurrent-ruby (1.0.4) 58 | connection_pool (2.2.1) 59 | debug_inspector (0.0.2) 60 | erubis (2.7.0) 61 | execjs (2.7.0) 62 | globalid (0.3.7) 63 | activesupport (>= 4.1.0) 64 | graphiql-rails (1.4.1) 65 | rails 66 | graphql (1.4.1) 67 | i18n (0.7.0) 68 | jquery-rails (4.2.2) 69 | rails-dom-testing (>= 1, < 3) 70 | railties (>= 4.2.0) 71 | thor (>= 0.14, < 2.0) 72 | loofah (2.0.3) 73 | nokogiri (>= 1.5.9) 74 | mail (2.6.4) 75 | mime-types (>= 1.16, < 4) 76 | method_source (0.8.2) 77 | mime-types (3.1) 78 | mime-types-data (~> 3.2015) 79 | mime-types-data (3.2016.0521) 80 | mini_portile2 (2.1.0) 81 | minitest (5.10.1) 82 | nio4r (1.2.1) 83 | nokogiri (1.7.0.1) 84 | mini_portile2 (~> 2.1.0) 85 | parser (2.3.1.4) 86 | ast (~> 2.2) 87 | pg (0.19.0) 88 | powerpack (0.1.1) 89 | pry (0.10.4) 90 | coderay (~> 1.1.0) 91 | method_source (~> 0.8.1) 92 | slop (~> 3.4) 93 | rack (2.0.1) 94 | rack-test (0.6.3) 95 | rack (>= 1.0) 96 | rails (5.0.1) 97 | actioncable (= 5.0.1) 98 | actionmailer (= 5.0.1) 99 | actionpack (= 5.0.1) 100 | actionview (= 5.0.1) 101 | activejob (= 5.0.1) 102 | activemodel (= 5.0.1) 103 | activerecord (= 5.0.1) 104 | activesupport (= 5.0.1) 105 | bundler (>= 1.3.0, < 2.0) 106 | railties (= 5.0.1) 107 | sprockets-rails (>= 2.0.0) 108 | rails-dom-testing (2.0.2) 109 | activesupport (>= 4.2.0, < 6.0) 110 | nokogiri (~> 1.6) 111 | rails-html-sanitizer (1.0.3) 112 | loofah (~> 2.0) 113 | rails_12factor (0.0.3) 114 | rails_serve_static_assets 115 | rails_stdout_logging 116 | rails_serve_static_assets (0.0.5) 117 | rails_stdout_logging (0.0.5) 118 | railties (5.0.1) 119 | actionpack (= 5.0.1) 120 | activesupport (= 5.0.1) 121 | method_source 122 | rake (>= 0.8.7) 123 | thor (>= 0.18.1, < 2.0) 124 | rainbow (2.1.0) 125 | rake (12.0.0) 126 | react-rails (1.10.0) 127 | babel-transpiler (>= 0.7.0) 128 | coffee-script-source (~> 1.8) 129 | connection_pool 130 | execjs 131 | railties (>= 3.2) 132 | tilt 133 | rubocop (0.45.0) 134 | parser (>= 2.3.1.1, < 3.0) 135 | powerpack (~> 0.1) 136 | rainbow (>= 1.99.1, < 3.0) 137 | ruby-progressbar (~> 1.7) 138 | unicode-display_width (~> 1.0, >= 1.0.1) 139 | ruby-progressbar (1.8.1) 140 | sass (3.4.23) 141 | sass-rails (5.0.6) 142 | railties (>= 4.0.0, < 6) 143 | sass (~> 3.1) 144 | sprockets (>= 2.8, < 4.0) 145 | sprockets-rails (>= 2.0, < 4.0) 146 | tilt (>= 1.1, < 3) 147 | slop (3.6.0) 148 | spring (2.0.0) 149 | activesupport (>= 4.2) 150 | sprockets (3.7.1) 151 | concurrent-ruby (~> 1.0) 152 | rack (> 1, < 3) 153 | sprockets-rails (3.2.0) 154 | actionpack (>= 4.0) 155 | activesupport (>= 4.0) 156 | sprockets (>= 3.0.0) 157 | thor (0.19.4) 158 | thread_safe (0.3.5) 159 | tilt (2.0.5) 160 | tzinfo (1.2.2) 161 | thread_safe (~> 0.1) 162 | uglifier (3.0.4) 163 | execjs (>= 0.3.0, < 3) 164 | unicode-display_width (1.1.1) 165 | web-console (2.3.0) 166 | activemodel (>= 4.0) 167 | binding_of_caller (>= 0.7.2) 168 | railties (>= 4.0) 169 | sprockets-rails (>= 2.0, < 4.0) 170 | websocket-driver (0.6.4) 171 | websocket-extensions (>= 0.1.0) 172 | websocket-extensions (0.1.2) 173 | 174 | PLATFORMS 175 | ruby 176 | 177 | DEPENDENCIES 178 | better_errors 179 | byebug 180 | graphiql-rails 181 | graphql 182 | jquery-rails 183 | minitest 184 | pg 185 | pry 186 | rails (~> 5.0) 187 | rails_12factor 188 | react-rails 189 | rubocop 190 | sass-rails (~> 5.0) 191 | spring 192 | uglifier (>= 1.3.0) 193 | web-console (~> 2.0) 194 | 195 | RUBY VERSION 196 | ruby 2.3.1p112 197 | 198 | BUNDLED WITH 199 | 1.13.7 200 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [](https://travis-ci.org/rmosolgo/graphql-ruby-demo) 2 | 3 | ## graphql-ruby-demo 4 | Shows an implementation of GraphQL via [graphql-ruby](https://github.com/rmosolgo/graphql-ruby). 5 | 6 | 7 | - Try it on [heroku](http://graphql-ruby-demo.herokuapp.com/). 8 | - Schema is defined in [`/app/graph`](https://github.com/rmosolgo/graphql-ruby-demo/tree/master/app/graph). 9 | - Queries are served by [`queries#create`](https://github.com/rmosolgo/graphql-ruby-demo/blob/master/app/controllers/queries_controller.rb#L8). 10 | - GraphiQL IDE is served by [graphiql-rails](https://github.com/rmosolgo/graphiql-rails) 11 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require File.expand_path('../config/application', __FILE__) 5 | require 'rake/testtask' 6 | 7 | Rails.application.load_tasks 8 | 9 | Rake::TestTask.new do |t| 10 | t.libs << "test" 11 | t.pattern = "test/**/*_test.rb" 12 | t.warning = false 13 | end 14 | 15 | task default: [:test] 16 | -------------------------------------------------------------------------------- /app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | # Prevent CSRF attacks by raising an exception. 3 | # For APIs, you may want to use :null_session instead. 4 | # protect_from_forgery with: :exception 5 | end 6 | -------------------------------------------------------------------------------- /app/controllers/queries_controller.rb: -------------------------------------------------------------------------------- 1 | class QueriesController < ApplicationController 2 | def new 3 | end 4 | 5 | def create 6 | query_string = params[:query] 7 | query_variables = ensure_hash(params[:variables]) 8 | result = StarWarsSchema.execute(query_string, variables: query_variables) 9 | render json: result 10 | end 11 | 12 | private 13 | 14 | def ensure_hash(query_variables) 15 | if query_variables.blank? 16 | {} 17 | elsif query_variables.is_a?(String) 18 | JSON.parse(query_variables) 19 | else 20 | query_variables 21 | end 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /app/controllers/shas_controller.rb: -------------------------------------------------------------------------------- 1 | class ShasController < ApplicationController 2 | def show 3 | render text: `git rev-parse HEAD` 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /app/graph/fields/fetch_field.rb: -------------------------------------------------------------------------------- 1 | module Fields 2 | class FetchField 3 | def self.build(model:, type:) 4 | return_type = type 5 | GraphQL::Field.define do 6 | type(return_type) 7 | description("Find a #{model.name} by ID") 8 | argument(:id, !types.Int, "ID for Record") 9 | resolve ->(obj, args, ctx) { 10 | model.find(args["id"]) 11 | } 12 | end 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /app/graph/queries/introQuery.graphql: -------------------------------------------------------------------------------- 1 | # Use GraphQL to query data from 2 | # a Ruby on Rails backend 3 | 4 | query heroes { 5 | # Find the hero for this episode 6 | hero(episode: NEWHOPE) { 7 | # And get some information 8 | id, 9 | name, 10 | __typename 11 | # if it's a droid, you can 12 | # access this field 13 | ... on Droid { 14 | primaryFunction 15 | } 16 | } 17 | 18 | # Find a node by its global ID 19 | node(id: "SHVtYW4tMTAwMA==") { 20 | id, 21 | ... on Character { 22 | # Use a predefined set of fields 23 | # (called a fragment) 24 | ...heroFields 25 | } 26 | } 27 | } 28 | 29 | fragment heroFields on Character { 30 | name, 31 | id, 32 | 33 | friends(first: 2) { 34 | edges { 35 | node { 36 | name 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/graph/queries/introspectHumanType.graphql: -------------------------------------------------------------------------------- 1 | query introspectHumanType { 2 | __type(name: "Human") { 3 | name, 4 | fields { 5 | name, 6 | type { 7 | name, 8 | ofType { name } 9 | } 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /app/graph/queries/introspectSchema.graphql: -------------------------------------------------------------------------------- 1 | query introspectSchema { 2 | __schema { 3 | types { name } 4 | queryType { fields { name } } 5 | mutationType { fields { name } } 6 | directives { name } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /app/graph/queries/invalidFields.graphql: -------------------------------------------------------------------------------- 1 | query invalidFields { 2 | hero { 3 | id, 4 | nonsenseField, 5 | name(nonsenseArg: 1) 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /app/graph/queries/invalidNestedFragments.graphql: -------------------------------------------------------------------------------- 1 | query infiniteFragments { 2 | hero { 3 | ... heroFields 4 | } 5 | } 6 | 7 | fragment heroFields on Character { 8 | id, 9 | ... heroFields 10 | } 11 | -------------------------------------------------------------------------------- /app/graph/queries/readHero.graphql: -------------------------------------------------------------------------------- 1 | query readHero { 2 | hero { name, id } 3 | } 4 | -------------------------------------------------------------------------------- /app/graph/queries/readHeroByEpisode.graphql: -------------------------------------------------------------------------------- 1 | query readHero { 2 | newHopeHero: hero(episode: NEWHOPE) { 3 | ... heroFields 4 | } 5 | empireHero: hero(episode: EMPIRE) { 6 | ... heroFields 7 | } 8 | jediHero: hero(episode: JEDI) { 9 | ... heroFields 10 | } 11 | } 12 | 13 | fragment heroFields on Character { 14 | name, id 15 | } 16 | -------------------------------------------------------------------------------- /app/graph/queries/readLukesFriends.graphql: -------------------------------------------------------------------------------- 1 | query readLukesFriends { 2 | luke: human(id: 1000) { 3 | friends { name } 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /app/graph/queries/readSiblings.graphql: -------------------------------------------------------------------------------- 1 | query readLukesFriends { 2 | brother: human(id: 1000) { 3 | ... siblingFields 4 | } 5 | sister: human(id: 1003) { 6 | ... siblingFields 7 | } 8 | } 9 | 10 | fragment siblingFields on Human { 11 | name, 12 | homePlanet 13 | } 14 | -------------------------------------------------------------------------------- /app/graph/star_wars_schema.rb: -------------------------------------------------------------------------------- 1 | StarWarsSchema = GraphQL::Schema.define do 2 | query Types::QueryType 3 | 4 | resolve_type ->(obj, ctx) do 5 | case obj 6 | when Droid 7 | Types::DroidType 8 | when Human 9 | Types::HumanType 10 | else 11 | raise("Don't know how to get the GraphQL type of a #{obj.class.name} (#{obj.inspect})") 12 | end 13 | end 14 | 15 | object_from_id ->(id, ctx) do 16 | type_name, item_id = GraphQL::Schema::UniqueWithinType.decode(id) 17 | 18 | # This `find` gives the user unrestricted access to *all* the records in your app. In 19 | # a real world application you probably want to check if the user is allowed to access 20 | # the requested resource. 21 | type_name.constantize.find(item_id) 22 | end 23 | 24 | id_from_object -> (object, type_definition, ctx) do 25 | GraphQL::Schema::UniqueWithinType.encode(type_definition.name, object.id) 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /app/graph/types/character_interface.rb: -------------------------------------------------------------------------------- 1 | # interface Character { 2 | # id: String! 3 | # name: String 4 | # friends: [Character] 5 | # appearsIn: [Episode] 6 | # } 7 | module Types 8 | CharacterInterface = GraphQL::InterfaceType.define do 9 | name "Character" 10 | description "A sentient actor in Star Wars" 11 | field :id, !types.ID, "The unique ID of this person" 12 | field :name, !types.String, "The name of this person" 13 | connection :friends, CharacterInterface.connection_type, "Friends of this person" 14 | field :appearsIn, types[EpisodeEnum], "Episodes this person appears in", property: :appears_in_names 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /app/graph/types/droid_type.rb: -------------------------------------------------------------------------------- 1 | # type Droid : Character { 2 | # id: String! 3 | # name: String 4 | # friends: [Character] 5 | # appearsIn: [Episode] 6 | # primaryFunction: String 7 | # } 8 | module Types 9 | DroidType = GraphQL::ObjectType.define do 10 | name "Droid" 11 | description "A robotic character in Star Wars" 12 | interfaces [GraphQL::Relay::Node.interface, CharacterInterface] 13 | 14 | global_id_field :id 15 | field :name, !types.String, "The name of this droid" 16 | connection :friends, CharacterInterface.connection_type, "Friends of this droid" 17 | field :appearsIn, types[EpisodeEnum], "Episodes this droid appears in", property: :appears_in 18 | field :primaryFunction, types.String, "What this droid is for", property: :primary_function 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /app/graph/types/episode_enum.rb: -------------------------------------------------------------------------------- 1 | # enum Episode { NEWHOPE, EMPIRE, JEDI } 2 | module Types 3 | EpisodeEnum = GraphQL::EnumType.define do 4 | name "Episode" 5 | description "An part of the Star Wars saga" 6 | Character::EPISODES.each do |episode, name| 7 | value(name, "Part #{episode}", value: episode) 8 | end 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /app/graph/types/human_type.rb: -------------------------------------------------------------------------------- 1 | # type Human : Character { 2 | # id: String! 3 | # name: String 4 | # friends: [Character] 5 | # appearsIn: [Episode] 6 | # homePlanet: String 7 | # } 8 | module Types 9 | HumanType = GraphQL::ObjectType.define do 10 | name "Human" 11 | description "A flesh-and-blood character in Star Wars" 12 | interfaces [GraphQL::Relay::Node.interface, CharacterInterface] 13 | 14 | global_id_field :id 15 | field :name, !types.String, "The name of this person" 16 | connection :friends, CharacterInterface.connection_type, "Friends of this person" 17 | field :appearsIn, types[EpisodeEnum], "Episodes this person appears in", property: :appears_in 18 | field :homePlanet, types.String, "Where this person is from", property: :home_planet 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /app/graph/types/query_type.rb: -------------------------------------------------------------------------------- 1 | # type Query { 2 | # hero: Character 3 | # human(id: String!): Human 4 | # droid(id: String!): Droid 5 | # } 6 | module Types 7 | QueryType = GraphQL::ObjectType.define do 8 | name "Query" 9 | description "The query root for this schema" 10 | 11 | # You can define fields on the fly: 12 | field :hero do 13 | type !CharacterInterface 14 | description "The hero of the saga" 15 | 16 | argument :episode, EpisodeEnum, "If provided, return the hero of that episode" 17 | 18 | resolve -> (obj, args, ctx) do 19 | args["episode"] == 5 ? Human.find(1000) : Droid.find(2001) 20 | end 21 | end 22 | 23 | field :human, HumanType, field: Fields::FetchField.build(type: HumanType, model: Human) 24 | field :droid, DroidType, field: Fields::FetchField.build(type: DroidType, model: Droid) 25 | field :node, GraphQL::Relay::Node.field 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /app/models/character.rb: -------------------------------------------------------------------------------- 1 | module Character 2 | EPISODES = { 3 | 4 => "NEWHOPE", 4 | 5 => "EMPIRE", 5 | 6 => "JEDI", 6 | } 7 | 8 | extend ActiveSupport::Concern 9 | included do 10 | serialize :appears_in, JSON 11 | 12 | def friends 13 | Friendship.where(from_character_id: id).map(&:to_character) 14 | end 15 | end 16 | end 17 | -------------------------------------------------------------------------------- /app/models/droid.rb: -------------------------------------------------------------------------------- 1 | class Droid < ActiveRecord::Base 2 | include Character 3 | end 4 | -------------------------------------------------------------------------------- /app/models/friendship.rb: -------------------------------------------------------------------------------- 1 | class Friendship < ActiveRecord::Base 2 | belongs_to :from_character, polymorphic: true 3 | belongs_to :to_character, polymorphic: true 4 | end 5 | -------------------------------------------------------------------------------- /app/models/human.rb: -------------------------------------------------------------------------------- 1 | class Human < ActiveRecord::Base 2 | include Character 3 | end 4 | -------------------------------------------------------------------------------- /bin/bundle: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) 3 | load Gem.bin_path('bundler', 'bundle') 4 | -------------------------------------------------------------------------------- /bin/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | APP_PATH = File.expand_path('../config/application', __dir__) 3 | require_relative '../config/boot' 4 | require 'rails/commands' 5 | -------------------------------------------------------------------------------- /bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require_relative '../config/boot' 3 | require 'rake' 4 | Rake.application.run 5 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'pathname' 3 | require 'fileutils' 4 | include FileUtils 5 | 6 | # path to your application root. 7 | APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) 8 | 9 | def system!(*args) 10 | system(*args) || abort("\n== Command #{args} failed ==") 11 | end 12 | 13 | chdir APP_ROOT do 14 | # This script is a starting point to setup your application. 15 | # Add necessary setup steps to this file. 16 | 17 | puts '== Installing dependencies ==' 18 | system! 'gem install bundler --conservative' 19 | system('bundle check') || system!('bundle install') 20 | 21 | # puts "\n== Copying sample files ==" 22 | # unless File.exist?('config/database.yml') 23 | # cp 'config/database.yml.sample', 'config/database.yml' 24 | # end 25 | 26 | puts "\n== Preparing database ==" 27 | system! 'bin/rails db:setup' 28 | 29 | puts "\n== Removing old logs and tempfiles ==" 30 | system! 'bin/rails log:clear tmp:clear' 31 | 32 | puts "\n== Restarting application server ==" 33 | system! 'bin/rails restart' 34 | end 35 | -------------------------------------------------------------------------------- /bin/update: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require 'pathname' 3 | require 'fileutils' 4 | include FileUtils 5 | 6 | # path to your application root. 7 | APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) 8 | 9 | def system!(*args) 10 | system(*args) || abort("\n== Command #{args} failed ==") 11 | end 12 | 13 | chdir APP_ROOT do 14 | # This script is a way to update your development environment automatically. 15 | # Add necessary update steps to this file. 16 | 17 | puts '== Installing dependencies ==' 18 | system! 'gem install bundler --conservative' 19 | system('bundle check') || system!('bundle install') 20 | 21 | puts "\n== Updating database ==" 22 | system! 'bin/rails db:migrate' 23 | 24 | puts "\n== Removing old logs and tempfiles ==" 25 | system! 'bin/rails log:clear tmp:clear' 26 | 27 | puts "\n== Restarting application server ==" 28 | system! 'bin/rails restart' 29 | end 30 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require ::File.expand_path('../config/environment', __FILE__) 4 | run Rails.application 5 | -------------------------------------------------------------------------------- /config/application.rb: -------------------------------------------------------------------------------- 1 | require_relative 'boot' 2 | 3 | require 'rails/all' 4 | 5 | # Require the gems listed in Gemfile, including any gems 6 | # you've limited to :test, :development, or :production. 7 | Bundler.require(*Rails.groups) 8 | 9 | module GraphqlRubyDemo 10 | class Application < Rails::Application 11 | # Settings in config/environments/* take precedence over those specified here. 12 | # Application configuration should go into files in config/initializers 13 | # -- all .rb files in that directory are automatically loaded. 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /config/boot.rb: -------------------------------------------------------------------------------- 1 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) 2 | 3 | require 'bundler/setup' # Set up gems listed in the Gemfile. 4 | -------------------------------------------------------------------------------- /config/cable.yml: -------------------------------------------------------------------------------- 1 | development: 2 | adapter: async 3 | 4 | test: 5 | adapter: async 6 | 7 | production: 8 | adapter: redis 9 | url: redis://localhost:6379/1 10 | -------------------------------------------------------------------------------- /config/database.yml: -------------------------------------------------------------------------------- 1 | # PostgreSQL. Versions 8.2 and up are supported. 2 | # 3 | # Install the pg driver: 4 | # gem install pg 5 | # On OS X with Homebrew: 6 | # gem install pg -- --with-pg-config=/usr/local/bin/pg_config 7 | # On OS X with MacPorts: 8 | # gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config 9 | # On Windows: 10 | # gem install pg 11 | # Choose the win32 build. 12 | # Install PostgreSQL and put its /bin directory on your path. 13 | # 14 | # Configure Using Gemfile 15 | # gem 'pg' 16 | # 17 | default: &default 18 | adapter: postgresql 19 | encoding: unicode 20 | # For details on connection pooling, see rails configuration guide 21 | # http://guides.rubyonrails.org/configuring.html#database-pooling 22 | pool: 5 23 | 24 | development: 25 | <<: *default 26 | database: graphql-ruby-demo_development 27 | 28 | # The specified database role being used to connect to postgres. 29 | # To create additional roles in postgres see `$ createuser --help`. 30 | # When left blank, postgres will use the default role. This is 31 | # the same name as the operating system user that initialized the database. 32 | #username: graphql-ruby-demo 33 | 34 | # The password associated with the postgres role (username). 35 | #password: 36 | 37 | # Connect on a TCP socket. Omitted by default since the client uses a 38 | # domain socket that doesn't need configuration. Windows does not have 39 | # domain sockets, so uncomment these lines. 40 | #host: localhost 41 | 42 | # The TCP port the server listens on. Defaults to 5432. 43 | # If your server runs on a different port number, change accordingly. 44 | #port: 5432 45 | 46 | # Schema search path. The server defaults to $user,public 47 | #schema_search_path: myapp,sharedapp,public 48 | 49 | # Minimum log levels, in increasing order: 50 | # debug5, debug4, debug3, debug2, debug1, 51 | # log, notice, warning, error, fatal, and panic 52 | # Defaults to warning. 53 | #min_messages: notice 54 | 55 | # Warning: The database defined as "test" will be erased and 56 | # re-generated from your development database when you run "rake". 57 | # Do not set this db to the same as development or production. 58 | test: 59 | <<: *default 60 | database: graphql-ruby-demo_test 61 | 62 | # As with config/secrets.yml, you never want to store sensitive information, 63 | # like your database password, in your source code. If your source code is 64 | # ever seen by anyone, they now have access to your database. 65 | # 66 | # Instead, provide the password as a unix environment variable when you boot 67 | # the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database 68 | # for a full rundown on how to provide these environment variables in a 69 | # production deployment. 70 | # 71 | # On Heroku and other platform providers, you may have a full connection URL 72 | # available as an environment variable. For example: 73 | # 74 | # DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" 75 | # 76 | # You can use this database configuration with: 77 | # 78 | # production: 79 | # url: <%= ENV['DATABASE_URL'] %> 80 | # 81 | production: 82 | <<: *default 83 | database: graphql-ruby-demo_production 84 | username: graphql-ruby-demo 85 | password: <%= ENV['GRAPHQL-RUBY-DEMO_DATABASE_PASSWORD'] %> 86 | -------------------------------------------------------------------------------- /config/database.yml.travis: -------------------------------------------------------------------------------- 1 | --- 2 | test: 3 | adapter: postgresql 4 | database: travis_ci_test 5 | -------------------------------------------------------------------------------- /config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the Rails application. 2 | require_relative 'application' 3 | 4 | # Initialize the Rails application. 5 | Rails.application.initialize! 6 | -------------------------------------------------------------------------------- /config/environments/development.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # In the development environment your application's code is reloaded on 5 | # every request. This slows down response time but is perfect for development 6 | # since you don't have to restart the web server when you make code changes. 7 | config.cache_classes = false 8 | 9 | # Do not eager load code on boot. 10 | config.eager_load = false 11 | 12 | # Show full error reports. 13 | config.consider_all_requests_local = true 14 | 15 | # Enable/disable caching. By default caching is disabled. 16 | if Rails.root.join('tmp/caching-dev.txt').exist? 17 | config.action_controller.perform_caching = true 18 | 19 | config.cache_store = :memory_store 20 | config.public_file_server.headers = { 21 | 'Cache-Control' => 'public, max-age=172800' 22 | } 23 | else 24 | config.action_controller.perform_caching = false 25 | 26 | config.cache_store = :null_store 27 | end 28 | 29 | # Don't care if the mailer can't send. 30 | config.action_mailer.raise_delivery_errors = false 31 | 32 | config.action_mailer.perform_caching = false 33 | 34 | # Print deprecation notices to the Rails logger. 35 | config.active_support.deprecation = :log 36 | 37 | # Raise an error on page load if there are pending migrations. 38 | config.active_record.migration_error = :page_load 39 | 40 | # Debug mode disables concatenation and preprocessing of assets. 41 | # This option may cause significant delays in view rendering with a large 42 | # number of complex assets. 43 | config.assets.debug = true 44 | 45 | # Suppress logger output for asset requests. 46 | config.assets.quiet = true 47 | 48 | # Raises error for missing translations 49 | # config.action_view.raise_on_missing_translations = true 50 | 51 | # Use an evented file watcher to asynchronously detect changes in source code, 52 | # routes, locales, etc. This feature depends on the listen gem. 53 | # config.file_watcher = ActiveSupport::EventedFileUpdateChecker 54 | end 55 | -------------------------------------------------------------------------------- /config/environments/production.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # Code is not reloaded between requests. 5 | config.cache_classes = true 6 | 7 | # Eager load code on boot. This eager loads most of Rails and 8 | # your application in memory, allowing both threaded web servers 9 | # and those relying on copy on write to perform better. 10 | # Rake tasks automatically ignore this option for performance. 11 | config.eager_load = true 12 | 13 | # Full error reports are disabled and caching is turned on. 14 | config.consider_all_requests_local = false 15 | config.action_controller.perform_caching = true 16 | 17 | # Disable serving static files from the `/public` folder by default since 18 | # Apache or NGINX already handles this. 19 | config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? 20 | 21 | # Compress JavaScripts and CSS. 22 | config.assets.js_compressor = :uglifier 23 | # config.assets.css_compressor = :sass 24 | 25 | # Do not fallback to assets pipeline if a precompiled asset is missed. 26 | config.assets.compile = false 27 | 28 | # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb 29 | 30 | # Enable serving of images, stylesheets, and JavaScripts from an asset server. 31 | # config.action_controller.asset_host = 'http://assets.example.com' 32 | 33 | # Specifies the header that your server uses for sending files. 34 | # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache 35 | # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX 36 | 37 | # Mount Action Cable outside main process or domain 38 | # config.action_cable.mount_path = nil 39 | # config.action_cable.url = 'wss://example.com/cable' 40 | # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ] 41 | 42 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 43 | # config.force_ssl = true 44 | 45 | # Use the lowest log level to ensure availability of diagnostic information 46 | # when problems arise. 47 | config.log_level = :debug 48 | 49 | # Prepend all log lines with the following tags. 50 | config.log_tags = [ :request_id ] 51 | 52 | # Use a different cache store in production. 53 | # config.cache_store = :mem_cache_store 54 | 55 | # Use a real queuing backend for Active Job (and separate queues per environment) 56 | # config.active_job.queue_adapter = :resque 57 | # config.active_job.queue_name_prefix = "graphql_ruby_demo_#{Rails.env}" 58 | config.action_mailer.perform_caching = false 59 | 60 | # Ignore bad email addresses and do not raise email delivery errors. 61 | # Set this to true and configure the email server for immediate delivery to raise delivery errors. 62 | # config.action_mailer.raise_delivery_errors = false 63 | 64 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 65 | # the I18n.default_locale when a translation cannot be found). 66 | config.i18n.fallbacks = true 67 | 68 | # Send deprecation notices to registered listeners. 69 | config.active_support.deprecation = :notify 70 | 71 | # Use default logging formatter so that PID and timestamp are not suppressed. 72 | config.log_formatter = ::Logger::Formatter.new 73 | 74 | # Use a different logger for distributed setups. 75 | # require 'syslog/logger' 76 | # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') 77 | 78 | if ENV["RAILS_LOG_TO_STDOUT"].present? 79 | logger = ActiveSupport::Logger.new(STDOUT) 80 | logger.formatter = config.log_formatter 81 | config.logger = ActiveSupport::TaggedLogging.new(logger) 82 | end 83 | 84 | # Do not dump schema after migrations. 85 | config.active_record.dump_schema_after_migration = false 86 | end 87 | -------------------------------------------------------------------------------- /config/environments/test.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | # Settings specified here will take precedence over those in config/application.rb. 3 | 4 | # The test environment is used exclusively to run your application's 5 | # test suite. You never need to work with it otherwise. Remember that 6 | # your test database is "scratch space" for the test suite and is wiped 7 | # and recreated between test runs. Don't rely on the data there! 8 | config.cache_classes = true 9 | 10 | # Do not eager load code on boot. This avoids loading your whole application 11 | # just for the purpose of running a single test. If you are using a tool that 12 | # preloads Rails for running tests, you may have to set it to true. 13 | config.eager_load = false 14 | 15 | # Configure public file server for tests with Cache-Control for performance. 16 | config.public_file_server.enabled = true 17 | config.public_file_server.headers = { 18 | 'Cache-Control' => 'public, max-age=3600' 19 | } 20 | 21 | # Show full error reports and disable caching. 22 | config.consider_all_requests_local = true 23 | config.action_controller.perform_caching = false 24 | 25 | # Raise exceptions instead of rendering exception templates. 26 | config.action_dispatch.show_exceptions = false 27 | 28 | # Disable request forgery protection in test environment. 29 | config.action_controller.allow_forgery_protection = false 30 | config.action_mailer.perform_caching = false 31 | 32 | # Tell Action Mailer not to deliver emails to the real world. 33 | # The :test delivery method accumulates sent emails in the 34 | # ActionMailer::Base.deliveries array. 35 | config.action_mailer.delivery_method = :test 36 | 37 | # Print deprecation notices to the stderr. 38 | config.active_support.deprecation = :stderr 39 | 40 | # Raises error for missing translations 41 | # config.action_view.raise_on_missing_translations = true 42 | end 43 | -------------------------------------------------------------------------------- /config/initializers/application_controller_renderer.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # ApplicationController.renderer.defaults.merge!( 4 | # http_host: 'example.org', 5 | # https: false 6 | # ) 7 | -------------------------------------------------------------------------------- /config/initializers/assets.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Version of your assets, change this if you want to expire all your assets. 4 | Rails.application.config.assets.version = '1.0' 5 | 6 | # Add additional assets to the asset load path 7 | # Rails.application.config.assets.paths << Emoji.images_path 8 | 9 | # Precompile additional assets. 10 | # application.js, application.css, and all non-JS/CSS in app/assets folder are already added. 11 | Rails.application.config.assets.precompile += ['graphiql.js', 'graphiql.css'] 12 | -------------------------------------------------------------------------------- /config/initializers/backtrace_silencers.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. 4 | # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } 5 | 6 | # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. 7 | # Rails.backtrace_cleaner.remove_silencers! 8 | -------------------------------------------------------------------------------- /config/initializers/cookies_serializer.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Specify a serializer for the signed and encrypted cookie jars. 4 | # Valid options are :json, :marshal, and :hybrid. 5 | Rails.application.config.action_dispatch.cookies_serializer = :json 6 | -------------------------------------------------------------------------------- /config/initializers/filter_parameter_logging.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Configure sensitive parameters which will be filtered from the log file. 4 | Rails.application.config.filter_parameters += [:password] 5 | -------------------------------------------------------------------------------- /config/initializers/graphiql.rb: -------------------------------------------------------------------------------- 1 | GraphiQL::Rails.config.query_params = true 2 | -------------------------------------------------------------------------------- /config/initializers/inflections.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new inflection rules using the following format. Inflections 4 | # are locale specific, and you may define rules for as many different 5 | # locales as you wish. All of these examples are active by default: 6 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 7 | # inflect.plural /^(ox)$/i, '\1en' 8 | # inflect.singular /^(ox)en/i, '\1' 9 | # inflect.irregular 'person', 'people' 10 | # inflect.uncountable %w( fish sheep ) 11 | # end 12 | 13 | # These inflection rules are supported but not enabled by default: 14 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 15 | # inflect.acronym 'RESTful' 16 | # end 17 | -------------------------------------------------------------------------------- /config/initializers/mime_types.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new mime types for use in respond_to blocks: 4 | # Mime::Type.register "text/richtext", :rtf 5 | -------------------------------------------------------------------------------- /config/initializers/new_framework_defaults.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | # 3 | # This file contains migration options to ease your Rails 5.0 upgrade. 4 | # 5 | # Once upgraded flip defaults one by one to migrate to the new default. 6 | # 7 | # Read the Rails 5.0 release notes for more info on each option. 8 | 9 | # Enable per-form CSRF tokens. Previous versions had false. 10 | Rails.application.config.action_controller.per_form_csrf_tokens = false 11 | 12 | # Enable origin-checking CSRF mitigation. Previous versions had false. 13 | Rails.application.config.action_controller.forgery_protection_origin_check = false 14 | 15 | # Make Ruby 2.4 preserve the timezone of the receiver when calling `to_time`. 16 | # Previous versions had false. 17 | ActiveSupport.to_time_preserves_timezone = false 18 | 19 | # Require `belongs_to` associations by default. Previous versions had false. 20 | Rails.application.config.active_record.belongs_to_required_by_default = false 21 | 22 | # Do not halt callback chains when a callback returns false. Previous versions had true. 23 | ActiveSupport.halt_callback_chains_on_return_false = true 24 | -------------------------------------------------------------------------------- /config/initializers/session_store.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | Rails.application.config.session_store :cookie_store, key: '_graphql_ruby_demo_session' 4 | -------------------------------------------------------------------------------- /config/initializers/wrap_parameters.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # This file contains settings for ActionController::ParamsWrapper which 4 | # is enabled by default. 5 | 6 | # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. 7 | ActiveSupport.on_load(:action_controller) do 8 | wrap_parameters format: [:json] 9 | end 10 | 11 | # To enable root element in JSON for ActiveRecord objects. 12 | # ActiveSupport.on_load(:active_record) do 13 | # self.include_root_in_json = true 14 | # end 15 | -------------------------------------------------------------------------------- /config/locales/en.yml: -------------------------------------------------------------------------------- 1 | # Files in the config/locales directory are used for internationalization 2 | # and are automatically loaded by Rails. If you want to use locales other 3 | # than English, add the necessary files in this directory. 4 | # 5 | # To use the locales, use `I18n.t`: 6 | # 7 | # I18n.t 'hello' 8 | # 9 | # In views, this is aliased to just `t`: 10 | # 11 | # <%= t('hello') %> 12 | # 13 | # To use a different locale, set it with `I18n.locale`: 14 | # 15 | # I18n.locale = :es 16 | # 17 | # This would use the information in config/locales/es.yml. 18 | # 19 | # To learn more, please read the Rails Internationalization guide 20 | # available at http://guides.rubyonrails.org/i18n.html. 21 | 22 | en: 23 | hello: "Hello world" 24 | -------------------------------------------------------------------------------- /config/puma.rb: -------------------------------------------------------------------------------- 1 | # Puma can serve each request in a thread from an internal thread pool. 2 | # The `threads` method setting takes two numbers a minimum and maximum. 3 | # Any libraries that use thread pools should be configured to match 4 | # the maximum value specified for Puma. Default is set to 5 threads for minimum 5 | # and maximum, this matches the default thread size of Active Record. 6 | # 7 | threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i 8 | threads threads_count, threads_count 9 | 10 | # Specifies the `port` that Puma will listen on to receive requests, default is 3000. 11 | # 12 | port ENV.fetch("PORT") { 3000 } 13 | 14 | # Specifies the `environment` that Puma will run in. 15 | # 16 | environment ENV.fetch("RAILS_ENV") { "development" } 17 | 18 | # Specifies the number of `workers` to boot in clustered mode. 19 | # Workers are forked webserver processes. If using threads and workers together 20 | # the concurrency of the application would be max `threads` * `workers`. 21 | # Workers do not work on JRuby or Windows (both of which do not support 22 | # processes). 23 | # 24 | # workers ENV.fetch("WEB_CONCURRENCY") { 2 } 25 | 26 | # Use the `preload_app!` method when specifying a `workers` number. 27 | # This directive tells Puma to first boot the application and load code 28 | # before forking the application. This takes advantage of Copy On Write 29 | # process behavior so workers use less memory. If you use this option 30 | # you need to make sure to reconnect any threads in the `on_worker_boot` 31 | # block. 32 | # 33 | # preload_app! 34 | 35 | # The code in the `on_worker_boot` will be called if you are using 36 | # clustered mode by specifying a number of `workers`. After each worker 37 | # process is booted this block will be run, if you are using `preload_app!` 38 | # option you will want to use this block to reconnect to any threads 39 | # or connections that may have been created at application boot, Ruby 40 | # cannot share connections between processes. 41 | # 42 | # on_worker_boot do 43 | # ActiveRecord::Base.establish_connection if defined?(ActiveRecord) 44 | # end 45 | 46 | # Allow puma to be restarted by `rails restart` command. 47 | plugin :tmp_restart 48 | -------------------------------------------------------------------------------- /config/routes.rb: -------------------------------------------------------------------------------- 1 | Rails.application.routes.draw do 2 | mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/queries" 3 | root to: redirect("/graphiql") 4 | resources :queries 5 | resource :sha, only: :show 6 | end 7 | -------------------------------------------------------------------------------- /config/secrets.yml: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Your secret key is used for verifying the integrity of signed cookies. 4 | # If you change this key, all old signed cookies will become invalid! 5 | 6 | # Make sure the secret is at least 30 characters and all random, 7 | # no regular words or you'll be exposed to dictionary attacks. 8 | # You can use `rails secret` to generate a secure secret key. 9 | 10 | # Make sure the secrets in this file are kept private 11 | # if you're sharing your code publicly. 12 | 13 | development: 14 | secret_key_base: 998c01cab4fba122e7041d9a7e87246f06eeb2a0c01c6693ddbe7a6a985e1d92c226844f6a564abfe4efe68dbed6db1e256be99e1b59e51ae0ab5f9194ed38af 15 | 16 | test: 17 | secret_key_base: f6d8d44a32957cc07ca02cb68aa5a1c7ac8c23b137978fbc4a4ad8cb8af6f996fb8f374355c70b3633d0b458b17fc8dcc06e09f3b4e2d06a9acf53fb5a95d30c 18 | 19 | # Do not keep production secrets in the repository, 20 | # instead read values from the environment. 21 | production: 22 | secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> 23 | -------------------------------------------------------------------------------- /db/migrate/20150711200126_create_humen.rb: -------------------------------------------------------------------------------- 1 | class CreateHumen < ActiveRecord::Migration 2 | def change 3 | create_table :humen do |t| 4 | t.string :name 5 | t.text :appears_in 6 | t.string :home_planet 7 | 8 | t.timestamps null: false 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /db/migrate/20150711200156_create_droids.rb: -------------------------------------------------------------------------------- 1 | class CreateDroids < ActiveRecord::Migration 2 | def change 3 | create_table :droids do |t| 4 | t.string :name 5 | t.text :appears_in 6 | t.string :primary_function 7 | 8 | t.timestamps null: false 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /db/migrate/20150711200623_create_friendships.rb: -------------------------------------------------------------------------------- 1 | class CreateFriendships < ActiveRecord::Migration 2 | def change 3 | create_table :friendships do |t| 4 | t.integer :from_character_id 5 | t.string :from_character_type 6 | t.integer :to_character_id 7 | t.string :to_character_type 8 | 9 | t.timestamps null: false 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /db/schema.rb: -------------------------------------------------------------------------------- 1 | # encoding: UTF-8 2 | # This file is auto-generated from the current state of the database. Instead 3 | # of editing this file, please use the migrations feature of Active Record to 4 | # incrementally modify your database, and then regenerate this schema definition. 5 | # 6 | # Note that this schema.rb definition is the authoritative source for your 7 | # database schema. If you need to create the application database on another 8 | # system, you should be using db:schema:load, not running all the migrations 9 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations 10 | # you'll amass, the slower it'll run and the greater likelihood for issues). 11 | # 12 | # It's strongly recommended that you check this file into your version control system. 13 | 14 | ActiveRecord::Schema.define(version: 20150711200623) do 15 | 16 | # These are extensions that must be enabled in order to support this database 17 | enable_extension "plpgsql" 18 | 19 | create_table "droids", force: :cascade do |t| 20 | t.string "name" 21 | t.text "appears_in" 22 | t.string "primary_function" 23 | t.datetime "created_at", null: false 24 | t.datetime "updated_at", null: false 25 | end 26 | 27 | create_table "friendships", force: :cascade do |t| 28 | t.integer "from_character_id" 29 | t.string "from_character_type" 30 | t.integer "to_character_id" 31 | t.string "to_character_type" 32 | t.datetime "created_at", null: false 33 | t.datetime "updated_at", null: false 34 | end 35 | 36 | create_table "humen", force: :cascade do |t| 37 | t.string "name" 38 | t.text "appears_in" 39 | t.string "home_planet" 40 | t.datetime "created_at", null: false 41 | t.datetime "updated_at", null: false 42 | end 43 | 44 | end 45 | -------------------------------------------------------------------------------- /db/seeds.rb: -------------------------------------------------------------------------------- 1 | Human.delete_all 2 | Droid.delete_all 3 | Friendship.delete_all 4 | 5 | luke = { 6 | id: '1000', 7 | name: 'Luke Skywalker', 8 | friends: ['1002', '1003', '2000', '2001'], 9 | appears_in: [4, 5, 6], 10 | home_planet: 'Tatooine', 11 | } 12 | 13 | vader = { 14 | id: '1001', 15 | name: 'Darth Vader', 16 | friends: ['1004'], 17 | appears_in: [4, 5, 6], 18 | home_planet: 'Tatooine', 19 | }; 20 | 21 | han = { 22 | id: '1002', 23 | name: 'Han Solo', 24 | friends: ['1000', '1003', '2001'], 25 | appears_in: [4, 5, 6], 26 | }; 27 | 28 | leia = { 29 | id: '1003', 30 | name: 'Leia Organa', 31 | friends: ['1000', '1002', '2000', '2001'], 32 | appears_in: [4, 5, 6], 33 | home_planet: 'Alderaan', 34 | }; 35 | 36 | tarkin = { 37 | id: '1004', 38 | name: 'Wilhuff Tarkin', 39 | friends: ['1001'], 40 | appears_in: [4], 41 | }; 42 | 43 | threepio = { 44 | id: '2000', 45 | name: 'C-3PO', 46 | friends: ['1000', '1002', '1003', '2001'], 47 | appears_in: [4, 5, 6], 48 | primary_function: 'Protocol', 49 | }; 50 | 51 | artoo = { 52 | id: '2001', 53 | name: 'R2-D2', 54 | friends: ['1000', '1002', '1003'], 55 | appears_in: [4, 5, 6], 56 | primary_function: 'Astromech', 57 | }; 58 | 59 | def type_for_id(id) 60 | id < 2000 ? "Human" : "Droid" 61 | end 62 | 63 | def create_friendship(from_id, to_id) 64 | from_id = from_id.to_i 65 | to_id = to_id.to_i 66 | from_type = type_for_id(from_id) 67 | to_type = type_for_id(to_id) 68 | Friendship.create!({ 69 | from_character_id: from_id, 70 | from_character_type: from_type, 71 | to_character_id: to_id, 72 | to_character_type: to_type, 73 | }) 74 | end 75 | DROIDS = [threepio, artoo] 76 | HUMANS = [luke, leia, han, vader, tarkin] 77 | 78 | DROIDS.each do |droid| 79 | friend_ids = droid.delete(:friends) 80 | friend_ids.map { |to_id| create_friendship(droid[:id], to_id) } 81 | Droid.create!(droid) 82 | end 83 | HUMANS.each do |human| 84 | friend_ids = human.delete(:friends) 85 | friend_ids.map { |to_id| create_friendship(human[:id], to_id) } 86 | Human.create!(human) 87 | end 88 | -------------------------------------------------------------------------------- /public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 |You may have mistyped the address or the page may have moved.
63 |If you are the application owner check the logs for more information.
65 |Maybe you tried to change something you didn't have access to.
63 |If you are the application owner check the logs for more information.
65 |If you are the application owner check the logs for more information.
64 |