graphql-remote_loader 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -4
- data/lib/graphql/remote_loader/loader.rb +35 -0
- data/lib/graphql/remote_loader/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 373c88fb1ed204a41f90ca7d23968d4c29a62df9
|
4
|
+
data.tar.gz: b535426a4ac97d8ce0bd0fd0ab1a76d6a292f3bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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
|
-
|
13
|
-
|
14
|
-
|
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
|
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.
|
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-
|
11
|
+
date: 2018-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: graphql
|