From 113a4770c495b180e81408ba40a37a74032af92c Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Mon, 13 Nov 2017 15:58:28 -0500 Subject: [PATCH 1/2] Adds graphiql to the dev mode of the server --- Gemfile | 3 ++- Gemfile.lock | 3 +++ Procfile | 1 - app/graph/root_schema.rb | 2 +- app/graph/{queries.rb => types/query_type.rb} | 6 ++++-- config/initializers/graphiql.rb | 1 + config/routes.rb | 4 ++++ 7 files changed, 15 insertions(+), 5 deletions(-) rename app/graph/{queries.rb => types/query_type.rb} (79%) create mode 100644 config/initializers/graphiql.rb diff --git a/Gemfile b/Gemfile index ef7968ba..02cc8702 100644 --- a/Gemfile +++ b/Gemfile @@ -27,7 +27,8 @@ gemini_gem_spec = { git: 'https://github.com/artsy/gemini_upload-rails.git', bra gem 'gemini_upload-rails', gemini_gem_spec # for admins to upload images -gem 'graphql' +gem 'graphql' # A lovely API +gem 'graphiql-rails' # A lovely interface to the API watt_gem_spec = { git: 'https://github.com/artsy/watt.git', branch: 'master' } gem 'watt', watt_gem_spec # artsy bootstrap diff --git a/Gemfile.lock b/Gemfile.lock index 7c1ca07c..6888388b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -121,6 +121,8 @@ GEM futuroscope (0.1.11) globalid (0.4.0) activesupport (>= 4.2.0) + graphiql-rails (1.4.7) + rails graphql (1.6.7) haml (5.0.1) temple (>= 0.8.0) @@ -352,6 +354,7 @@ DEPENDENCIES fabrication foreman gemini_upload-rails! + graphiql-rails graphql haml-rails hyperclient diff --git a/Procfile b/Procfile index eab54de2..c2c566e8 100644 --- a/Procfile +++ b/Procfile @@ -1,2 +1 @@ web: bundle exec puma -C config/puma.rb -worker: bundle exec sidekiq -c ${SIDEKIQ_CONCURRENCY:-5} diff --git a/app/graph/root_schema.rb b/app/graph/root_schema.rb index 35c7e05b..5f48d80e 100644 --- a/app/graph/root_schema.rb +++ b/app/graph/root_schema.rb @@ -1,5 +1,5 @@ RootSchema = GraphQL::Schema.define do - query Queries::Root + query Types::QueryType mutation Mutations::Root max_depth 5 diff --git a/app/graph/queries.rb b/app/graph/types/query_type.rb similarity index 79% rename from app/graph/queries.rb rename to app/graph/types/query_type.rb index dc953233..66fbdbcb 100644 --- a/app/graph/queries.rb +++ b/app/graph/types/query_type.rb @@ -1,11 +1,13 @@ -module Queries - Root = GraphQL::ObjectType.define do +module Types + QueryType = GraphQL::ObjectType.define do name 'Query' description 'Query root for this schema' field :submission, types[Types::SubmissionType] do description 'Find Submissions' argument :ids, types[types.ID] argument :id, types.ID + argument :completed, types.Boolean + resolve ->(_object, args, _context) { args[:ids] ? Submission.where(id: args[:ids]) : [Submission.find(args[:id])] } diff --git a/config/initializers/graphiql.rb b/config/initializers/graphiql.rb new file mode 100644 index 00000000..6480b176 --- /dev/null +++ b/config/initializers/graphiql.rb @@ -0,0 +1 @@ +GraphiQL::Rails.config.headers['Authorization'] = ->(context) { "Bearer #{context.session[:access_token]}" } diff --git a/config/routes.rb b/config/routes.rb index a0c66eed..bd6c0b0f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -24,6 +24,10 @@ post '/graphql', to: 'graphql#execute' end + if Rails.env.development? + mount GraphiQL::Rails::Engine, at: '/graphiql', graphql_path: '/api/graphql' + end + mount ArtsyAuth::Engine => '/' require 'sidekiq/web' From efb21809738b1c77f3493490b29c92d32e97dc11 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Mon, 13 Nov 2017 15:59:46 -0500 Subject: [PATCH 2/2] Adds a note about the graphiql --- README.md | 6 +++++- app/graph/types/query_type.rb | 1 - 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b9652756..2dc1cae7 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,11 @@ easy as: $ foreman start ``` -See the Procfile for more. +See the Procfile to understand other services launched. + +## API + +When running in development, this API has a GraphiQL instance at http://localhost:5000/graphiql ## Creating a Submission diff --git a/app/graph/types/query_type.rb b/app/graph/types/query_type.rb index 66fbdbcb..609fc593 100644 --- a/app/graph/types/query_type.rb +++ b/app/graph/types/query_type.rb @@ -6,7 +6,6 @@ module Types description 'Find Submissions' argument :ids, types[types.ID] argument :id, types.ID - argument :completed, types.Boolean resolve ->(_object, args, _context) { args[:ids] ? Submission.where(id: args[:ids]) : [Submission.find(args[:id])]