graphql-remote_loader 1.0.0 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f7f2b1ab4cf983200d1c98e42324a2965677466
4
- data.tar.gz: 6cd70f26a9b81d8be0899e519300fbdb30a3fb53
3
+ metadata.gz: 373c88fb1ed204a41f90ca7d23968d4c29a62df9
4
+ data.tar.gz: b535426a4ac97d8ce0bd0fd0ab1a76d6a292f3bb
5
5
  SHA512:
6
- metadata.gz: 1b5dbdc65a0fbcad465a567793044d4eb7cdb9b3728f44b6339ea956a00b46d97ca1cd499433e74847c78d3bde420cd7d60558990c6b97082934aea294e15886
7
- data.tar.gz: 8b796e95b900b6afbab007932bd41b9841f32d7284724e709d01108be5abf785affa9a547bbb5fda81520efc2431936081ff52360e4c4d1be7a21cd8befd9ccd
6
+ metadata.gz: edee071404cb25b0d65f9bd7247f91f088d0b9b1a87dbb2082d14e137848b7150f47c66a5fda0ca8bab45c76eaafd33e0a7f8b0bc4cc0d2e499370db6a8e5da1
7
+ data.tar.gz: 2586f4f3b17e2dc6cc4f99cbd83b23575f78389bbbefac08e620a5919f642fcb2e7332036544c8b1f7c643f49c67a3d482772c00cd3c2a72d64576676fd62159
data/README.md CHANGED
@@ -6,14 +6,30 @@ Performant, batched GraphQL queries from within the resolvers of a [`graphql-rub
6
6
 
7
7
  ## Snippet
8
8
 
9
+ For default error handling(null if errors) and no post-processing:
10
+
9
11
  ```ruby
10
- field :login, String, null: false, description: "The currently authenticated GitHub user's login."
12
+ field :login, String, null: true, description: "The currently authenticated GitHub user's login."
13
+
14
+ def login
15
+ GitHubLoader.load_value("viewer", "login")
16
+ end
17
+ ```
11
18
 
12
- def login
13
- GitHubLoader.load("viewer { login }").then do |results|
14
- results["viewer"]["login"]
19
+ If you need error handling, post-processing, or complex queries:
20
+
21
+ ```ruby
22
+ field :login, String, null: false, description: "The currently authenticated GitHub user's login."
23
+
24
+ def login
25
+ GitHubLoader.load("viewer { login }").then do |results|
26
+ if results["errors"].present?
27
+ ""
28
+ else
29
+ results["data"]["viewer"]["login"].upcase
15
30
  end
16
31
  end
32
+ end
17
33
  ```
18
34
 
19
35
  ## Full example
@@ -20,6 +20,18 @@ module GraphQL
20
20
  self.for.load([query, prime])
21
21
  end
22
22
 
23
+ # Loads the value, then if the query was successful, fulfills promise with
24
+ # the leaf value instead of the full results hash.
25
+ #
26
+ # If errors are present, returns nil.
27
+ def self.load_value(*path)
28
+ load(query_from_path(path)).then do |results|
29
+ next nil if results["errors"] && !results["errors"].empty?
30
+
31
+ value_from_hash(results["data"])
32
+ end
33
+ end
34
+
23
35
  def self.reset_index
24
36
  @index = nil
25
37
  end
@@ -123,6 +135,29 @@ module GraphQL
123
135
  tr("-", "_").
124
136
  downcase
125
137
  end
138
+
139
+ def self.query_from_path(path)
140
+ if path.length == 1
141
+ path.first
142
+ else
143
+ "#{path.first} { #{query_from_path(path[1..-1])} }"
144
+ end
145
+ end
146
+
147
+ # Input is a hash where all nested hashes have only one key.
148
+ #
149
+ # Output is the leaf at the end of the hash.
150
+ #
151
+ # e.g. {foo: {bar: 5}} => 5
152
+ def self.value_from_hash(hash_or_value)
153
+ case hash_or_value
154
+ when Hash
155
+ # {foo: {bar: 5}}.first[1] => {bar: 5}
156
+ value_from_hash(hash_or_value.first[1])
157
+ else
158
+ hash_or_value
159
+ end
160
+ end
126
161
  end
127
162
  end
128
163
  end
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module RemoteLoader
3
- VERSION = "1.0.0"
3
+ VERSION = "1.0.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-remote_loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathaniel Woodthorpe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-19 00:00:00.000000000 Z
11
+ date: 2018-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql