rspec-graphql_response 0.4.0 → 0.4.1
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 +4 -4
- data/Gemfile.lock +11 -2
- data/README.md +1 -1
- data/docs/operation.md +1 -36
- data/docs/response.md +1 -1
- data/docs/response_data.md +146 -0
- data/lib/rspec/graphql_response.rb +1 -0
- data/lib/rspec/graphql_response/dig_dug/dig_dug.rb +83 -0
- data/lib/rspec/graphql_response/helpers.rb +3 -2
- data/lib/rspec/graphql_response/helpers/operation.rb +1 -0
- data/lib/rspec/graphql_response/helpers/response_data.rb +13 -0
- data/lib/rspec/graphql_response/version.rb +1 -1
- data/rspec-graphql_response.gemspec +1 -0
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 647c52ffa5ae347bc9c2166bd94af15ee82556d10f37fa5a11a0cf0229d40b31
|
4
|
+
data.tar.gz: a93873e4d4a40a9a4702026321d96d913056188dd51eec86836232add5e7c4b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ad72db1bdd814a65f02cf4ee9a59c9836c452003fe840e9c28903aa805ab0553fcd7afa4558d610c63c5ae1959fea61690f037641bd3fa31b417c378ed1805a
|
7
|
+
data.tar.gz: 46ec7e724d5784feb01e33fbb5024d9c8713ddb8df77a1efb2273f1dd94c2bf8c3658ecb1f11ad9065741119b6e4a71815f9911ba3e87a779caafe97563b2dd8
|
data/Gemfile.lock
CHANGED
@@ -1,18 +1,22 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rspec-graphql_response (0.4.
|
4
|
+
rspec-graphql_response (0.4.1)
|
5
5
|
graphql (>= 1.0)
|
6
6
|
rspec (>= 3.0)
|
7
7
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
+
attr_extras (6.2.4)
|
11
12
|
byebug (11.1.3)
|
12
13
|
coderay (1.1.3)
|
13
14
|
diff-lcs (1.4.4)
|
14
|
-
graphql (1.12.
|
15
|
+
graphql (1.12.6)
|
15
16
|
method_source (1.0.0)
|
17
|
+
optimist (3.0.1)
|
18
|
+
patience_diff (1.2.0)
|
19
|
+
optimist (~> 3.0)
|
16
20
|
pry (0.14.0)
|
17
21
|
coderay (~> 1.1)
|
18
22
|
method_source (~> 1.0)
|
@@ -33,6 +37,10 @@ GEM
|
|
33
37
|
diff-lcs (>= 1.2.0, < 2.0)
|
34
38
|
rspec-support (~> 3.10.0)
|
35
39
|
rspec-support (3.10.2)
|
40
|
+
super_diff (0.6.1)
|
41
|
+
attr_extras (>= 6.2.4)
|
42
|
+
diff-lcs
|
43
|
+
patience_diff
|
36
44
|
|
37
45
|
PLATFORMS
|
38
46
|
ruby
|
@@ -43,6 +51,7 @@ DEPENDENCIES
|
|
43
51
|
pry-byebug (~> 3.8)
|
44
52
|
rake (>= 12.0)
|
45
53
|
rspec-graphql_response!
|
54
|
+
super_diff (~> 0.6)
|
46
55
|
|
47
56
|
BUNDLED WITH
|
48
57
|
1.17.2
|
data/README.md
CHANGED
@@ -55,7 +55,7 @@ Spec Helper Methods:
|
|
55
55
|
|
56
56
|
- [execute_graphql](/docs/execute_graphql.md) - executes a graphql call with the registered schema, query, variables and context
|
57
57
|
- [response](/docs/response.md) - the response, as JSON, of the executed graphql query
|
58
|
-
- [
|
58
|
+
- [response_data](/docs/response_data.md) - digs through the graphql response to return data from the specified node(s)
|
59
59
|
|
60
60
|
API / Development
|
61
61
|
|
data/docs/operation.md
CHANGED
@@ -1,38 +1,3 @@
|
|
1
1
|
# Using the `operation` helper
|
2
2
|
|
3
|
-
|
4
|
-
structure that looks like,
|
5
|
-
|
6
|
-
```ruby
|
7
|
-
{
|
8
|
-
"data" => {
|
9
|
-
operation_name
|
10
|
-
}
|
11
|
-
}
|
12
|
-
```
|
13
|
-
|
14
|
-
## Basic Use
|
15
|
-
|
16
|
-
```ruby
|
17
|
-
it "has characters" do
|
18
|
-
characters = operation(:characters)
|
19
|
-
|
20
|
-
expect(character).to include(
|
21
|
-
{ id: 1, name: "Jam" },
|
22
|
-
# ...
|
23
|
-
)
|
24
|
-
end
|
25
|
-
```
|
26
|
-
|
27
|
-
## Handling Nil
|
28
|
-
|
29
|
-
If there is no `"data"` or no named operation for the name supplied, the
|
30
|
-
`operation` helper will return `nil`
|
31
|
-
|
32
|
-
```ruby
|
33
|
-
it "returns nil if operation doesn't exist" do
|
34
|
-
character = operation(:something_that_does_not_exist)
|
35
|
-
|
36
|
-
expect(operation).to be_nil
|
37
|
-
end
|
38
|
-
```
|
3
|
+
Deprecated. See [response_data](response_data.md) instead.
|
data/docs/response.md
CHANGED
@@ -1 +1 @@
|
|
1
|
-
#
|
1
|
+
# The GraphQL Response, via Helper `response`
|
@@ -0,0 +1,146 @@
|
|
1
|
+
# Using the `response_data` Helper
|
2
|
+
|
3
|
+
The `response_data` helper will dig through a graphql response, through
|
4
|
+
the outer hash, into the response data for an operation, and through any
|
5
|
+
and all layers of hash and array.
|
6
|
+
|
7
|
+
## Syntax
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
response_data *[dig_pattern]
|
11
|
+
```
|
12
|
+
|
13
|
+
Data returned via this helper will assume a `"data" => ` key at the root of
|
14
|
+
the `response` object. This root does not need to be specified in the list
|
15
|
+
of attributes for the `dig_pattern`.
|
16
|
+
|
17
|
+
### Params
|
18
|
+
|
19
|
+
* `*[dig_pattern]` - an array of attributes (`:symbol`, `"string"`, or `key: :value` pair) that describes
|
20
|
+
the data structure to dig through, and the final data set to retrieve from the graphql response.
|
21
|
+
|
22
|
+
#### dig_pattern
|
23
|
+
|
24
|
+
Each attribute added to the `dig_pattern` represents an attribute at the given level of the
|
25
|
+
data structure, in numeric order from left to right. The first attribute provides will dig into
|
26
|
+
that attribute at the first level of data (just below the `"data" =>` key). The second attribute
|
27
|
+
will dig through data just below that first level, etc. etc. etc.
|
28
|
+
|
29
|
+
For example, with a data structure as shown below, in "Basic Use", you could specifiy these
|
30
|
+
attributes for the dig pattern:
|
31
|
+
|
32
|
+
* :characters
|
33
|
+
* :name
|
34
|
+
|
35
|
+
Like this:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
response_data :characters, :name
|
39
|
+
```
|
40
|
+
|
41
|
+
This dig pattern will find the `"characters"` key just below `"data"`, then iterate through
|
42
|
+
the array of characters and retrieve the `"name"` of each character.
|
43
|
+
|
44
|
+
For more details and options for the dig pattern, see the examples below.
|
45
|
+
|
46
|
+
## Basic Use
|
47
|
+
|
48
|
+
A `response` data structure may look something like the following.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
{
|
52
|
+
"data" => {
|
53
|
+
"characters" => [
|
54
|
+
{ "id" => "1", "name" => "Jam" },
|
55
|
+
{ "id" => "2", "name" => "Redemption" },
|
56
|
+
{ "id" => "3", "name" => "Pet" }
|
57
|
+
]
|
58
|
+
}
|
59
|
+
}
|
60
|
+
```
|
61
|
+
|
62
|
+
The `response_data` helper will dig through to give you simplified
|
63
|
+
results that are easier to verify.
|
64
|
+
|
65
|
+
For example, if only the names of the characters need to be checked:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
response_data :characters, :name
|
69
|
+
|
70
|
+
# => ["Jam", "Redemption", "Pet"]
|
71
|
+
```
|
72
|
+
|
73
|
+
Or perhaps only the name for 2nd character is needed:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
response_data {characters: [1]}, :name
|
77
|
+
|
78
|
+
# => "Redemption"
|
79
|
+
```
|
80
|
+
|
81
|
+
## List Every Item in an Array
|
82
|
+
|
83
|
+
Many responses from a graphql call will include an array of data somewhere
|
84
|
+
in the data structure. If you need to return all of the items in an array,
|
85
|
+
you only need to specify that array's key:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
it "has characters" do
|
89
|
+
characters = response_data(:characters)
|
90
|
+
|
91
|
+
expect(character).to include(
|
92
|
+
{ id: 1, name: "Jam" },
|
93
|
+
# ...
|
94
|
+
)
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
## Dig a Field From Every Item in an Array
|
99
|
+
|
100
|
+
When validation only needs to occur on a specific field for items found in
|
101
|
+
an array, there are two options.
|
102
|
+
|
103
|
+
1. Specify a list of fields as already shown
|
104
|
+
2. change the array's key to a hash and provide a `:symbol` wrapped in an array as the value
|
105
|
+
|
106
|
+
The first option was already shown in the Basic Use section above.
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
response_data :characters, :name
|
110
|
+
|
111
|
+
# => ["Jam", "Redemption", "Pet"]
|
112
|
+
```
|
113
|
+
|
114
|
+
For the second option, the code would look like this:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
response_data characters: [:name]
|
118
|
+
|
119
|
+
# => ["Jam", "Redemption", "Pet"]
|
120
|
+
```
|
121
|
+
|
122
|
+
Both of these options are functionaly the same. The primary difference will be
|
123
|
+
how you wish to express the data structure in your code. Changing the list of
|
124
|
+
attributes to a hash with an array wrapping the value will provide a better
|
125
|
+
indication that an array is expected at that point in the data structure.
|
126
|
+
|
127
|
+
## Dig Out an Item By Index, From an Array
|
128
|
+
|
129
|
+
There may be times when only a single piece of a returned array needs to be
|
130
|
+
validated. To handle this, switch the key of the array to a hash, as in the
|
131
|
+
previous example. Rather than specifying a child node's key in the value, though,
|
132
|
+
specify the index of the item you wish to extract.
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
response_data characters: [1]
|
136
|
+
```
|
137
|
+
|
138
|
+
This will return the character at index 1, from the array of characters.
|
139
|
+
|
140
|
+
## Handling Nil
|
141
|
+
|
142
|
+
If there is no data the key supplied, the helper will return `nil`
|
143
|
+
|
144
|
+
```ruby
|
145
|
+
response_data(:something_that_does_not_exist) #=> nil
|
146
|
+
```
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module RSpec
|
2
|
+
module GraphQLResponse
|
3
|
+
class DigDug
|
4
|
+
attr_reader :dig_pattern
|
5
|
+
|
6
|
+
def initialize(*dig_pattern)
|
7
|
+
@dig_pattern = parse_dig_pattern(*dig_pattern)
|
8
|
+
end
|
9
|
+
|
10
|
+
def dig(data)
|
11
|
+
dig_data(data, dig_pattern)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def dig_data(data, patterns)
|
17
|
+
return data if patterns.nil?
|
18
|
+
return data if patterns.empty?
|
19
|
+
|
20
|
+
node = patterns[0]
|
21
|
+
node_key = node[:key]
|
22
|
+
node_key = node_key.to_s if node_key.is_a? Symbol
|
23
|
+
node_value = node[:value]
|
24
|
+
|
25
|
+
if node[:type] == :symbol
|
26
|
+
result = dig_symbol(data, node_key)
|
27
|
+
elsif node[:type] == :array
|
28
|
+
if data.is_a? Hash
|
29
|
+
child_data = data[node_key]
|
30
|
+
result = dig_symbol(child_data, node_value)
|
31
|
+
elsif data.is_a? Array
|
32
|
+
result = data.map { |value|
|
33
|
+
child_data = value[node_key]
|
34
|
+
dig_symbol(child_data, node_value)
|
35
|
+
}.compact
|
36
|
+
else
|
37
|
+
result = data
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
dig_data(result, patterns.drop(1))
|
42
|
+
end
|
43
|
+
|
44
|
+
def parse_dig_pattern(*pattern)
|
45
|
+
pattern_config = pattern.map do |pattern_item|
|
46
|
+
if pattern_item.is_a? Symbol
|
47
|
+
{
|
48
|
+
type: :symbol,
|
49
|
+
key: pattern_item
|
50
|
+
}
|
51
|
+
elsif pattern_item.is_a? Hash
|
52
|
+
pattern_item.map do |key, value|
|
53
|
+
{
|
54
|
+
type: :array,
|
55
|
+
key: key,
|
56
|
+
value: value[0]
|
57
|
+
}
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
pattern_config.flatten
|
63
|
+
end
|
64
|
+
|
65
|
+
def dig_symbol(data, key)
|
66
|
+
key = key.to_s if key.is_a? Symbol
|
67
|
+
return data[key] if data.is_a? Hash
|
68
|
+
|
69
|
+
if data.is_a? Array
|
70
|
+
if key.is_a? Numeric
|
71
|
+
mapped_data = data[key]
|
72
|
+
else
|
73
|
+
mapped_data = data.map { |value| value[key] }.flatten
|
74
|
+
end
|
75
|
+
|
76
|
+
return mapped_data
|
77
|
+
end
|
78
|
+
|
79
|
+
return data
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -40,11 +40,12 @@ module RSpec
|
|
40
40
|
end
|
41
41
|
|
42
42
|
# describe level helpers
|
43
|
+
require_relative "helpers/graphql_context"
|
43
44
|
require_relative "helpers/graphql_operation"
|
44
45
|
require_relative "helpers/graphql_variables"
|
45
|
-
require_relative "helpers/graphql_context"
|
46
46
|
|
47
47
|
# spec level helpers
|
48
|
+
require_relative "helpers/execute_graphql"
|
48
49
|
require_relative "helpers/operation"
|
49
50
|
require_relative "helpers/response"
|
50
|
-
require_relative "helpers/
|
51
|
+
require_relative "helpers/response_data"
|
@@ -0,0 +1,13 @@
|
|
1
|
+
RSpec::GraphQLResponse.add_helper :response_data do |*fields|
|
2
|
+
next nil unless response.is_a? Hash
|
3
|
+
|
4
|
+
response_data = response["data"]
|
5
|
+
next nil if response_data.nil?
|
6
|
+
next nil if response_data.empty?
|
7
|
+
|
8
|
+
fields = fields.compact
|
9
|
+
next response_data if fields.empty?
|
10
|
+
|
11
|
+
dig_dug = RSpec::GraphQLResponse::DigDug.new(*fields)
|
12
|
+
dig_dug.dig(response_data)
|
13
|
+
end
|
@@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.add_development_dependency "rake", ">= 12.0"
|
30
30
|
spec.add_development_dependency "pry", "~> 0.14"
|
31
31
|
spec.add_development_dependency "pry-byebug", "~> 3.8"
|
32
|
+
spec.add_development_dependency "super_diff", "~> 0.6"
|
32
33
|
|
33
34
|
spec.add_runtime_dependency "rspec", ">= 3.0"
|
34
35
|
spec.add_runtime_dependency "graphql", ">= 1.0"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-graphql_response
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- River Lynn Bailey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-03-
|
11
|
+
date: 2021-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '3.8'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: super_diff
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.6'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.6'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: rspec
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -129,8 +143,10 @@ files:
|
|
129
143
|
- docs/have_operation.md
|
130
144
|
- docs/operation.md
|
131
145
|
- docs/response.md
|
146
|
+
- docs/response_data.md
|
132
147
|
- lib/rspec/graphql_response.rb
|
133
148
|
- lib/rspec/graphql_response/configuration.rb
|
149
|
+
- lib/rspec/graphql_response/dig_dug/dig_dug.rb
|
134
150
|
- lib/rspec/graphql_response/helpers.rb
|
135
151
|
- lib/rspec/graphql_response/helpers/execute_graphql.rb
|
136
152
|
- lib/rspec/graphql_response/helpers/graphql_context.rb
|
@@ -138,6 +154,7 @@ files:
|
|
138
154
|
- lib/rspec/graphql_response/helpers/graphql_variables.rb
|
139
155
|
- lib/rspec/graphql_response/helpers/operation.rb
|
140
156
|
- lib/rspec/graphql_response/helpers/response.rb
|
157
|
+
- lib/rspec/graphql_response/helpers/response_data.rb
|
141
158
|
- lib/rspec/graphql_response/matchers.rb
|
142
159
|
- lib/rspec/graphql_response/matchers/have_errors.rb
|
143
160
|
- lib/rspec/graphql_response/matchers/have_operation.rb
|