graphlient 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.ruby-version +1 -1
- data/CHANGELOG.md +1 -0
- data/README.md +9 -0
- data/graphlient.gemspec +1 -1
- data/lib/graphlient.rb +1 -0
- data/lib/graphlient/client.rb +6 -2
- data/lib/graphlient/extensions/query.rb +3 -3
- data/lib/graphlient/query.rb +12 -12
- data/lib/graphlient/schema.rb +26 -0
- data/lib/graphlient/version.rb +1 -1
- data/spec/graphlient/adapters/http/faraday_adapter_spec.rb +1 -1
- data/spec/graphlient/adapters/http/http_adapter_spec.rb +1 -1
- data/spec/graphlient/client_schema_spec.rb +30 -7
- data/spec/graphlient/schema_spec.rb +44 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3c5a902290bf910c35ad39c06f697c00b54e0156
|
4
|
+
data.tar.gz: c27b7ed166bf0acc803547aad92f5cbc35164da2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e8ca60f3ced8b55dc975bce37727abeeddbb18612c8c33596502e23b3b785613d38bae6e48a94e270d635bf89ef9b3e57e8c9b779a4cca764ef2ce5b2da708ba
|
7
|
+
data.tar.gz: a9474b9a2bef82b9fa4aff02d2e314827e5dc97a8f16b7efe30f4b836c10a2eb5acb967978fed95043a27cf89c2985c7aaf2df6878e63923bcab82a9f2fbf2e3
|
data/.gitignore
CHANGED
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.
|
1
|
+
2.3.0
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -107,6 +107,15 @@ The successful response contains data in `response.data`. The following example
|
|
107
107
|
response.data.create_invoice.first.id
|
108
108
|
```
|
109
109
|
|
110
|
+
### Schema storing and loading on disk
|
111
|
+
|
112
|
+
To reduce requests to graphql API you can cache schema:
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
client = Client.new(url, schema_path: 'config/your_graphql_schema.json')
|
116
|
+
client.schema.dump! # you only need to call this when graphql schema changes
|
117
|
+
```
|
118
|
+
|
110
119
|
### Error Handling
|
111
120
|
|
112
121
|
Unlike graphql-client, Graphlient will always raise an exception unless the query has succeeded.
|
data/graphlient.gemspec
CHANGED
data/lib/graphlient.rb
CHANGED
data/lib/graphlient/client.rb
CHANGED
@@ -49,13 +49,17 @@ module Graphlient
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def schema
|
52
|
-
@schema ||=
|
52
|
+
@schema ||= Graphlient::Schema.new(http, schema_path)
|
53
53
|
end
|
54
54
|
|
55
55
|
private
|
56
56
|
|
57
|
+
def schema_path
|
58
|
+
return options[:schema_path].to_s if options[:schema_path]
|
59
|
+
end
|
60
|
+
|
57
61
|
def client
|
58
|
-
@client ||= GraphQL::Client.new(schema: schema, execute: http).tap do |client|
|
62
|
+
@client ||= GraphQL::Client.new(schema: schema.graphql_schema, execute: http).tap do |client|
|
59
63
|
client.allow_dynamic_queries = @options.key?(:allow_dynamic_queries) ? options[:allow_dynamic_queries] : true
|
60
64
|
end
|
61
65
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
module Graphlient
|
2
2
|
module Extensions
|
3
3
|
module Query
|
4
|
-
def method_missing(
|
4
|
+
def method_missing(method_name, *args, &block)
|
5
5
|
Graphlient::Query.new do
|
6
|
-
send(
|
6
|
+
send(method_name, *args, &block)
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
10
|
-
def respond_to_missing?(
|
10
|
+
def respond_to_missing?(method_name, include_private = false)
|
11
11
|
super
|
12
12
|
end
|
13
13
|
end
|
data/lib/graphlient/query.rb
CHANGED
@@ -18,8 +18,8 @@ module Graphlient
|
|
18
18
|
instance_eval(&block)
|
19
19
|
end
|
20
20
|
|
21
|
-
def method_missing(
|
22
|
-
append_node(
|
21
|
+
def method_missing(method_name, *args, &block)
|
22
|
+
append_node(method_name, args, &block)
|
23
23
|
end
|
24
24
|
|
25
25
|
ROOT_NODES.each do |root_node|
|
@@ -29,7 +29,7 @@ module Graphlient
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
def respond_to_missing?(
|
32
|
+
def respond_to_missing?(method_name, include_private = false)
|
33
33
|
super
|
34
34
|
end
|
35
35
|
|
@@ -71,21 +71,21 @@ module Graphlient
|
|
71
71
|
end.join(', ')
|
72
72
|
end
|
73
73
|
|
74
|
-
def argument_string(
|
75
|
-
"#{
|
74
|
+
def argument_string(key, val)
|
75
|
+
"#{key}: #{argument_value_string(val)}"
|
76
76
|
end
|
77
77
|
|
78
|
-
def variable_string(
|
79
|
-
case
|
78
|
+
def variable_string(val)
|
79
|
+
case val
|
80
80
|
when :id, :id!
|
81
|
-
|
82
|
-
when ->(
|
81
|
+
val.to_s.upcase
|
82
|
+
when ->(v) { SCALAR_TYPES.key?(v.to_s.delete('!').to_sym) }
|
83
83
|
# scalar types
|
84
|
-
|
84
|
+
val.to_s.camelize
|
85
85
|
when Array
|
86
|
-
"[#{variable_string(
|
86
|
+
"[#{variable_string(val.first)}]"
|
87
87
|
else
|
88
|
-
|
88
|
+
val.to_s
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module Graphlient
|
4
|
+
class Schema < SimpleDelegator
|
5
|
+
PATH_ERROR_MESSAGE = 'schema_path is missing. Please add it like this: `Graphlient.new(url, schema_path: YOUR_PATH)`'.freeze
|
6
|
+
|
7
|
+
class MissingConfigurationError < StandardError; end
|
8
|
+
|
9
|
+
alias graphql_schema __getobj__
|
10
|
+
|
11
|
+
attr_reader :http, :path
|
12
|
+
|
13
|
+
def initialize(http, path)
|
14
|
+
schema_source = path || http
|
15
|
+
super(GraphQL::Client.load_schema(schema_source))
|
16
|
+
|
17
|
+
@path = path
|
18
|
+
@http = http
|
19
|
+
end
|
20
|
+
|
21
|
+
def dump!
|
22
|
+
raise MissingConfigurationError, PATH_ERROR_MESSAGE unless path
|
23
|
+
GraphQL::Client.dump_schema(http, path)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/graphlient/version.rb
CHANGED
@@ -1,18 +1,41 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'tempfile'
|
2
3
|
|
3
4
|
describe Graphlient::Client do
|
4
|
-
let(:client) {
|
5
|
+
let(:client) { described_class.new(url) }
|
6
|
+
let(:url) { 'http://graph.biz/graphql' }
|
5
7
|
|
6
8
|
describe '#schema' do
|
7
9
|
before do
|
8
|
-
stub_request(:post,
|
10
|
+
stub_request(:post, url)
|
11
|
+
.to_return(body: DummySchema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_json)
|
9
12
|
end
|
10
13
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
15
|
-
|
14
|
+
context 'when server returns error' do
|
15
|
+
before do
|
16
|
+
stub_request(:post, url).to_return(status: 500)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'fails with an exception' do
|
20
|
+
expect do
|
21
|
+
client.schema
|
22
|
+
end.to raise_error Graphlient::Errors::ServerError do |e|
|
23
|
+
expect(e.to_s).to eq 'the server responded with status 500'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when introspection request is sucessfull' do
|
29
|
+
it 'returns Graphlient::Schema instance' do
|
30
|
+
expect(client.schema).to be_a(Graphlient::Schema)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when schema path option is not String' do
|
35
|
+
let(:client) { described_class.new(url, schema_path: Pathname.new('config/schema.json')) }
|
36
|
+
|
37
|
+
it 'converts path to string' do
|
38
|
+
expect(client.schema.path).to eq 'config/schema.json'
|
16
39
|
end
|
17
40
|
end
|
18
41
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'tempfile'
|
3
|
+
|
4
|
+
describe Graphlient::Schema do
|
5
|
+
let(:client) { Graphlient::Client.new(url) }
|
6
|
+
let(:url) { 'http://graph.biz/graphql' }
|
7
|
+
let(:schema) { client.schema }
|
8
|
+
|
9
|
+
describe '#dump!' do
|
10
|
+
let!(:introspection_query_request) do
|
11
|
+
stub_request(:post, url)
|
12
|
+
.with(body: /query IntrospectionQuery/)
|
13
|
+
.to_return(body: DummySchema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_json)
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when schema path is not given' do
|
17
|
+
it 'raises error' do
|
18
|
+
expect { schema.dump! }.to raise_error(Graphlient::Schema::MissingConfigurationError)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when schema path is given' do
|
23
|
+
let(:client) { Graphlient::Client.new(url, schema_path: @schema_path) }
|
24
|
+
let(:schema_path) { @schema_path }
|
25
|
+
|
26
|
+
around(:each) do |example|
|
27
|
+
Tempfile.open('graphql_schema.json') do |file|
|
28
|
+
@schema_path = file.path
|
29
|
+
example.run
|
30
|
+
@schema_path = nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'makes introspection query' do
|
35
|
+
schema.dump!
|
36
|
+
expect(introspection_query_request).to have_been_made.once
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'updates schema json file' do
|
40
|
+
expect { schema.dump! }.to(change { File.read(@schema_path) })
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphlient
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ashkan Nasseri
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -89,6 +89,7 @@ files:
|
|
89
89
|
- lib/graphlient/extensions.rb
|
90
90
|
- lib/graphlient/extensions/query.rb
|
91
91
|
- lib/graphlient/query.rb
|
92
|
+
- lib/graphlient/schema.rb
|
92
93
|
- lib/graphlient/version.rb
|
93
94
|
- spec/graphlient/adapters/http/faraday_adapter_spec.rb
|
94
95
|
- spec/graphlient/adapters/http/http_adapter_spec.rb
|
@@ -96,6 +97,7 @@ files:
|
|
96
97
|
- spec/graphlient/client_schema_spec.rb
|
97
98
|
- spec/graphlient/extensions/query_spec.rb
|
98
99
|
- spec/graphlient/query_spec.rb
|
100
|
+
- spec/graphlient/schema_spec.rb
|
99
101
|
- spec/graphlient/static_client_query_spec.rb
|
100
102
|
- spec/spec_helper.rb
|
101
103
|
- spec/support/context/dummy_client.rb
|
@@ -125,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
127
|
version: 1.3.6
|
126
128
|
requirements: []
|
127
129
|
rubyforge_project:
|
128
|
-
rubygems_version: 2.
|
130
|
+
rubygems_version: 2.4.8
|
129
131
|
signing_key:
|
130
132
|
specification_version: 4
|
131
133
|
summary: A friendlier Ruby client for consuming GraphQL-based APIs.
|