From 4099d23759bbb90667c273e4d4a064acf6bb72bb Mon Sep 17 00:00:00 2001 From: Anton Davydov Date: Thu, 27 Jul 2017 03:46:46 +0300 Subject: [PATCH] Add endpoint for GraphQL --- apps/api/config/routes.rb | 1 + apps/api/controllers/graphql/index.rb | 9 ++++++ config.ru | 1 - lib/graphql/schema.rb | 36 ---------------------- spec/api/controllers/graphql/index_spec.rb | 11 +++++++ 5 files changed, 21 insertions(+), 37 deletions(-) create mode 100644 apps/api/controllers/graphql/index.rb create mode 100644 spec/api/controllers/graphql/index_spec.rb diff --git a/apps/api/config/routes.rb b/apps/api/config/routes.rb index 22d8edc..b7ba383 100644 --- a/apps/api/config/routes.rb +++ b/apps/api/config/routes.rb @@ -1 +1,2 @@ +get '/graphql', to: 'graphql#index' resources :contributors, only: %i[index show] diff --git a/apps/api/controllers/graphql/index.rb b/apps/api/controllers/graphql/index.rb new file mode 100644 index 0000000..c154d34 --- /dev/null +++ b/apps/api/controllers/graphql/index.rb @@ -0,0 +1,9 @@ +module Api::Controllers::Graphql + class Index + include Api::Action + + def call(params) + self.body = Graphql::Schema.execute(params[:query]).to_json + end + end +end diff --git a/config.ru b/config.ru index c902c93..2863511 100644 --- a/config.ru +++ b/config.ru @@ -16,5 +16,4 @@ map '/sidekiq' do run Sidekiq::Web end - run Hanami.app diff --git a/lib/graphql/schema.rb b/lib/graphql/schema.rb index 32b42d1..3d9be5e 100644 --- a/lib/graphql/schema.rb +++ b/lib/graphql/schema.rb @@ -3,39 +3,3 @@ module Graphql query Types::Query end end - -# query_all = <<-QUERY -# { -# contributors { -# id -# github -# full_name -# avatar_url -# } -# } -# QUERY -# -# Graphql::Schema.execute(query_all) -# # => [{"data"=>{"contributor"=>{"id"=>"101", "github"=>"jodosha", "full_name"=>nil, "avatar_url"=>"https://avatars2.githubusercontent.com/u/5089?v=3"}}}, ...] - -# query_first = <<-QUERY -# { -# contributor(id: "301") { -# id -# github -# full_name -# avatar_url -# -# commits { -# url -# -# project { -# name -# } -# } -# } -# } -# QUERY -# -# Graphql::Schema.execute(query_first) -# => {"data"=>{"contributor"=>{"id"=>"301", "github"=>"janx", "full_name"=>nil, "avatar_url"=>"https://avatars3.githubusercontent.com/u/5958?v=3", "commits"=>[{"url"=>"https://github.com/hanami/view/commit/3e94750830a9905390dc823ff91f5ccd485d99ea"}]}}} diff --git a/spec/api/controllers/graphql/index_spec.rb b/spec/api/controllers/graphql/index_spec.rb new file mode 100644 index 0000000..fac0d62 --- /dev/null +++ b/spec/api/controllers/graphql/index_spec.rb @@ -0,0 +1,11 @@ +require_relative '../../../../apps/api/controllers/graphql/index' + +RSpec.describe Api::Controllers::Graphql::Index do + let(:action) { described_class.new } + let(:params) { Hash[] } + + it 'is successful' do + response = action.call(params) + expect(response[0]).to eq 200 + end +end