rack-graphql 3.0.1 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3be75efa580d83c1718e86502c69740c85b9f7991e805d1bc96aa3b46d6d87fc
4
- data.tar.gz: be59d7bcb60808d5608b1b0dd7cb23d2f9ec523a00c016ee7a01a403effecd88
3
+ metadata.gz: 8b83b1b631d47e86696c0e572da7995e1b24effe687f49ac6eafedf1fb0d3e10
4
+ data.tar.gz: 1119172a1001cb9994c0be9c3dd7954bbbd251e63e44c9259252850172f2e922
5
5
  SHA512:
6
- metadata.gz: 3d6e49e759346df6b5ab7613b881b75d68a67317e467ecfd71c556d99b2dee70d1d3390f509f3a5234e783d220a4d1f94263f502b18414b8ec952ed89e3edd30
7
- data.tar.gz: 78af0b95d79497c5ccb3a83908cc14096f56fa856886aac01e306c104f6ba72e2757b24b8e7eb773260200b2517599ed778216d306c83be09a947d84db415525
6
+ metadata.gz: e1e2e395822dfdee07aa6fd716232bdbe3e2f91978eca8d86c97e2b5326009f1f4633b16ef36af37d72178a762b6484b4166f3792438fae009713b1daa14ad15
7
+ data.tar.gz: 7e8f995d024bf781f1f68734de4d482e1f8a16d3bc1407d68b9a6aa46a2964e12d526d09d688e2dadb0740c632338234bd626fb0f44b11b596079b11dc96c1f8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 3.1.0 - 2023-01-17
4
+
5
+ - Add X-Http-Status-Code to every response, so clients can recognize graphql errors w/o parsing gql response.
6
+
3
7
  ## 3.0.1 - 2023-01-17
4
8
 
5
9
  - allow running on rack 2.x, so it can work with apps having sinatra installed (sinatra doesn't support rack 3.x)
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rack-graphql (3.0.1)
4
+ rack-graphql (3.1.1)
5
5
  graphql (~> 2.0)
6
6
  oj
7
7
  rack (>= 2.2.6)
@@ -2,6 +2,8 @@ module RackGraphql
2
2
  class Middleware
3
3
  DEFAULT_STATUS_CODE = 200
4
4
  DEFAULT_ERROR_STATUS_CODE = 500
5
+ STATUS_CODE_HEADER_NAME = 'X-Http-Status-Code'.freeze
6
+ SUBSCRIPTION_ID_HEADER_NAME = 'X-Subscription-ID'.freeze
5
7
  NULL_BYTE = '\u0000'.freeze
6
8
 
7
9
  def initialize(
@@ -36,10 +38,11 @@ module RackGraphql
36
38
 
37
39
  log("Executing with params: #{params.inspect}, operationName: #{operation_name}, variables: #{variables.inspect}")
38
40
  result = execute(params: params, operation_name: operation_name, variables: variables, context: context)
41
+ status_code = response_status(result)
39
42
 
40
43
  [
41
- response_status(result),
42
- response_headers(result),
44
+ status_code,
45
+ response_headers(result, status_code: status_code),
43
46
  [response_body(result)]
44
47
  ]
45
48
  rescue AmbiguousParamError => e
@@ -49,7 +52,7 @@ module RackGraphql
49
52
  env[Rack::RACK_ERRORS].flush
50
53
  [
51
54
  400,
52
- { 'Content-Type' => 'application/json' },
55
+ { 'Content-Type' => 'application/json', STATUS_CODE_HEADER_NAME => 400 },
53
56
  [Oj.dump({})]
54
57
  ]
55
58
  rescue StandardError, LoadError, SyntaxError => e
@@ -64,9 +67,11 @@ module RackGraphql
64
67
 
65
68
  env[Rack::RACK_ERRORS].puts(exception_string)
66
69
  env[Rack::RACK_ERRORS].flush
70
+
71
+ status_code = error_status_code_map[e.class] || DEFAULT_ERROR_STATUS_CODE
67
72
  [
68
- error_status_code_map[e.class] || DEFAULT_ERROR_STATUS_CODE,
69
- { 'Content-Type' => 'application/json' },
73
+ status_code,
74
+ { 'Content-Type' => 'application/json', STATUS_CODE_HEADER_NAME => status_code },
70
75
  [Oj.dump('errors' => [exception_hash(e)])]
71
76
  ]
72
77
  ensure
@@ -140,12 +145,13 @@ module RackGraphql
140
145
  schema.multiplex(queries)
141
146
  end
142
147
 
143
- def response_headers(result = nil)
148
+ def response_headers(result = nil, status_code: DEFAULT_STATUS_CODE)
144
149
  {
145
- 'Access-Control-Expose-Headers' => 'X-Subscription-ID',
146
- 'Content-Type' => 'application/json'
150
+ 'Access-Control-Expose-Headers' => [SUBSCRIPTION_ID_HEADER_NAME, STATUS_CODE_HEADER_NAME].join(', '),
151
+ 'Content-Type' => 'application/json',
152
+ STATUS_CODE_HEADER_NAME => status_code,
147
153
  }.tap do |headers|
148
- headers['X-Subscription-ID'] = result.context[:subscription_id] if result_subscription?(result)
154
+ headers[SUBSCRIPTION_ID_HEADER_NAME] = result.context[:subscription_id] if result_subscription?(result)
149
155
  end
150
156
  end
151
157
 
@@ -1,3 +1,3 @@
1
1
  module RackGraphql
2
- VERSION = '3.0.1'.freeze
2
+ VERSION = '3.1.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Krzysztof Knapik