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 +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/lib/rack_graphql/middleware.rb +15 -9
- data/lib/rack_graphql/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b83b1b631d47e86696c0e572da7995e1b24effe687f49ac6eafedf1fb0d3e10
|
4
|
+
data.tar.gz: 1119172a1001cb9994c0be9c3dd7954bbbd251e63e44c9259252850172f2e922
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
@@ -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
|
-
|
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
|
-
|
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' => '
|
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[
|
154
|
+
headers[SUBSCRIPTION_ID_HEADER_NAME] = result.context[:subscription_id] if result_subscription?(result)
|
149
155
|
end
|
150
156
|
end
|
151
157
|
|
data/lib/rack_graphql/version.rb
CHANGED