rack-graphql 3.0.1 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3be75efa580d83c1718e86502c69740c85b9f7991e805d1bc96aa3b46d6d87fc
4
- data.tar.gz: be59d7bcb60808d5608b1b0dd7cb23d2f9ec523a00c016ee7a01a403effecd88
3
+ metadata.gz: 78d6b07dd0b65feaf7bf1f42b8f5ed607e47c865a6fb1c774b8c79265a699618
4
+ data.tar.gz: c733bce94eae77c43c12ce94213c049ca20dea104da4a46de5a3ae182cbf3a44
5
5
  SHA512:
6
- metadata.gz: 3d6e49e759346df6b5ab7613b881b75d68a67317e467ecfd71c556d99b2dee70d1d3390f509f3a5234e783d220a4d1f94263f502b18414b8ec952ed89e3edd30
7
- data.tar.gz: 78af0b95d79497c5ccb3a83908cc14096f56fa856886aac01e306c104f6ba72e2757b24b8e7eb773260200b2517599ed778216d306c83be09a947d84db415525
6
+ metadata.gz: de1e1d0ad8a05da26ab297161a06df874d0a2192c3ea3c9d98e6501ee0b4f6af1844c0d5b79ddad668f25bda11bb5eafb16c97fe2627c4b6783e05cb973e03b0
7
+ data.tar.gz: b421f740d27f958728fb07f287ae4666cec11e7312362ae863a7d5f120b1241f2fdd163fc640f7565e369fc9bd81d4956893a2342b2f47ee08d9a10aae5de355
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.0)
5
5
  graphql (~> 2.0)
6
6
  oj
7
7
  rack (>= 2.2.6)
@@ -2,6 +2,7 @@ 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
5
6
  NULL_BYTE = '\u0000'.freeze
6
7
 
7
8
  def initialize(
@@ -36,10 +37,11 @@ module RackGraphql
36
37
 
37
38
  log("Executing with params: #{params.inspect}, operationName: #{operation_name}, variables: #{variables.inspect}")
38
39
  result = execute(params: params, operation_name: operation_name, variables: variables, context: context)
40
+ status_code = response_status(result)
39
41
 
40
42
  [
41
- response_status(result),
42
- response_headers(result),
43
+ status_code,
44
+ response_headers(result, status_code: status_code),
43
45
  [response_body(result)]
44
46
  ]
45
47
  rescue AmbiguousParamError => e
@@ -49,7 +51,7 @@ module RackGraphql
49
51
  env[Rack::RACK_ERRORS].flush
50
52
  [
51
53
  400,
52
- { 'Content-Type' => 'application/json' },
54
+ { 'Content-Type' => 'application/json', STATUS_CODE_HEADER_NAME => 400 },
53
55
  [Oj.dump({})]
54
56
  ]
55
57
  rescue StandardError, LoadError, SyntaxError => e
@@ -64,9 +66,11 @@ module RackGraphql
64
66
 
65
67
  env[Rack::RACK_ERRORS].puts(exception_string)
66
68
  env[Rack::RACK_ERRORS].flush
69
+
70
+ status_code = error_status_code_map[e.class] || DEFAULT_ERROR_STATUS_CODE
67
71
  [
68
- error_status_code_map[e.class] || DEFAULT_ERROR_STATUS_CODE,
69
- { 'Content-Type' => 'application/json' },
72
+ status_code,
73
+ { 'Content-Type' => 'application/json', STATUS_CODE_HEADER_NAME => status_code },
70
74
  [Oj.dump('errors' => [exception_hash(e)])]
71
75
  ]
72
76
  ensure
@@ -140,10 +144,11 @@ module RackGraphql
140
144
  schema.multiplex(queries)
141
145
  end
142
146
 
143
- def response_headers(result = nil)
147
+ def response_headers(result = nil, status_code: DEFAULT_STATUS_CODE)
144
148
  {
145
149
  'Access-Control-Expose-Headers' => 'X-Subscription-ID',
146
- 'Content-Type' => 'application/json'
150
+ 'Content-Type' => 'application/json',
151
+ STATUS_CODE_HEADER_NAME => status_code,
147
152
  }.tap do |headers|
148
153
  headers['X-Subscription-ID'] = result.context[:subscription_id] if result_subscription?(result)
149
154
  end
@@ -1,3 +1,3 @@
1
1
  module RackGraphql
2
- VERSION = '3.0.1'.freeze
2
+ VERSION = '3.1.0'.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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Krzysztof Knapik