From 70e23f16e4d8f5c53ef8b6fede98e4b8dd4bc218 Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 21 Dec 2020 16:55:59 -0700 Subject: feat: process first graphql query --- Gemfile | 1 + Gemfile.lock | 2 ++ bin/setup | 3 +-- lib/server.rb | 25 ++++++++++++++++++++++++- test/integration/server_test.rb | 25 +++++++++++++++++++++++++ 5 files changed, 53 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index b5d62d4..7b0759d 100644 --- a/Gemfile +++ b/Gemfile @@ -7,4 +7,5 @@ gem "rack", "~> 2.2" group :development, :test do gem "rack-test", "~> 1.1" + gem "minitest" end diff --git a/Gemfile.lock b/Gemfile.lock index 3a83a38..c38569c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,6 +2,7 @@ GEM remote: https://rubygems.org/ specs: graphql (1.11.6) + minitest (5.14.2) rack (2.2.3) rack-test (1.1.0) rack (>= 1.0, < 3) @@ -11,6 +12,7 @@ PLATFORMS DEPENDENCIES graphql (~> 1.11) + minitest rack (~> 2.2) rack-test (~> 1.1) diff --git a/bin/setup b/bin/setup index 08afd37..7f5e1b8 100755 --- a/bin/setup +++ b/bin/setup @@ -4,5 +4,4 @@ set -e cd "$(dirname "$0")/.." -gem install --conservative bundler -bundle install +bundle check || bundle install diff --git a/lib/server.rb b/lib/server.rb index 6b14bf7..600dce5 100644 --- a/lib/server.rb +++ b/lib/server.rb @@ -1,7 +1,30 @@ require 'rack' +require 'json' +require 'graphql' + +module Types + class QueryType < GraphQL::Schema::Object + field :me, String, null: false + + def me + 'mo' + end + end +end + +class MySchema < GraphQL::Schema + max_complexity 400 + query Types::QueryType +end class Server def call(env) - [200, {}, []] + query = '{ me }' + + [ + 200, + { 'Content-Type' => 'application/graphql' }, + [MySchema.execute(query).to_json] + ] end end diff --git a/test/integration/server_test.rb b/test/integration/server_test.rb index 3ca8b72..20d870e 100644 --- a/test/integration/server_test.rb +++ b/test/integration/server_test.rb @@ -6,9 +6,34 @@ class ServerTest < Minitest::Test end def test_get + skip "for now" get '/' assert_equal 200, last_response.status assert_empty last_response.body end + + def test_get_graphql_with_query_string + header 'Content-Type', 'application/graphql' + get '/graphql', query: '{me}' + + assert last_response.ok? + assert_equal 200, last_response.status + refute_empty last_response.body + + json = JSON.parse(last_response.body) + assert_equal 'mo', json['data']['me'] + end + + def test_get_graphql_with_post_body + header 'Content-Type', 'application/graphql' + post '/graphql', '{me}' + + assert last_response.ok? + assert_equal 200, last_response.status + refute_empty last_response.body + + json = JSON.parse(last_response.body) + assert_equal 'mo', json['data']['me'] + end end -- cgit v1.2.3