Commit 70e23f1

mo khan <mo.khan@gmail.com>
2020-12-21 23:55:59
feat: process first graphql query
1 parent 9d50919
Changed files (5)
bin/setup
@@ -4,5 +4,4 @@ set -e
 
 cd "$(dirname "$0")/.."
 
-gem install --conservative bundler
-bundle install
+bundle check || bundle install
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
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
Gemfile
@@ -7,4 +7,5 @@ gem "rack", "~> 2.2"
 
 group :development, :test do
   gem "rack-test", "~> 1.1"
+  gem "minitest"
 end
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)