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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 2e6a1b6dd31d8be1b55aa0efbba64dce194bdee6f55a10c6d010148ff56a2274
4
- data.tar.gz: 5740a5829835cf1af03733cccf2ac5a9b2621b57849d1b001d7fc517aafe6740
2
+ SHA1:
3
+ metadata.gz: 3c5a902290bf910c35ad39c06f697c00b54e0156
4
+ data.tar.gz: c27b7ed166bf0acc803547aad92f5cbc35164da2
5
5
  SHA512:
6
- metadata.gz: 5ad460a9adba6796cbba204ddc7b843d1acee5396c2b24f018a54c2de5d168535e54ed07fc4241c16f8dd1f23967a73e8d576b748b27e0f134305984503e9bf9
7
- data.tar.gz: 880bf215ab25b9519bec4cd1c4d6ca2c66139cb8d6beddb615b43b184eab99dcde3c8b666d9c750bc4b6842f89e0a0e826a585a7a04c75717b96b61ba8a7ee6a
6
+ metadata.gz: e8ca60f3ced8b55dc975bce37727abeeddbb18612c8c33596502e23b3b785613d38bae6e48a94e270d635bf89ef9b3e57e8c9b779a4cca764ef2ce5b2da708ba
7
+ data.tar.gz: a9474b9a2bef82b9fa4aff02d2e314827e5dc97a8f16b7efe30f4b836c10a2eb5acb967978fed95043a27cf89c2985c7aaf2df6878e63923bcab82a9f2fbf2e3
data/.gitignore CHANGED
@@ -10,6 +10,7 @@
10
10
  /test/version_tmp/
11
11
  /tmp/
12
12
  .byebug_history
13
+ .history/
13
14
  .DS_Store
14
15
  # Used by dotenv library to load environment variables.
15
16
  # .env
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.5.0
1
+ 2.3.0
data/CHANGELOG.md CHANGED
@@ -1,5 +1,6 @@
1
1
  ### 0.3.1 (Next)
2
2
 
3
+ * [#43](https://github.com/ashkan18/graphlient/pull/38): Allow to load and dump schema to json - [@povilasjurcys](https://github.com/povilasjurcys).
3
4
  * Your contribution here.
4
5
 
5
6
  ### 0.3.0 (02/22/2018)
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
@@ -1,4 +1,4 @@
1
- $LOAD_PATH.push File.expand_path('../lib', __FILE__)
1
+ $LOAD_PATH.push File.expand_path('lib', __dir__)
2
2
 
3
3
  require 'graphlient/version'
4
4
 
data/lib/graphlient.rb CHANGED
@@ -5,3 +5,4 @@ require 'graphlient/errors'
5
5
  require 'graphlient/query'
6
6
  require 'graphlient/adapters'
7
7
  require 'graphlient/client'
8
+ require 'graphlient/schema'
@@ -49,13 +49,17 @@ module Graphlient
49
49
  end
50
50
 
51
51
  def schema
52
- @schema ||= GraphQL::Client.load_schema(http)
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(m, *args, &block)
4
+ def method_missing(method_name, *args, &block)
5
5
  Graphlient::Query.new do
6
- send(m, *args, &block)
6
+ send(method_name, *args, &block)
7
7
  end
8
8
  end
9
9
 
10
- def respond_to_missing?(m, include_private = false)
10
+ def respond_to_missing?(method_name, include_private = false)
11
11
  super
12
12
  end
13
13
  end
@@ -18,8 +18,8 @@ module Graphlient
18
18
  instance_eval(&block)
19
19
  end
20
20
 
21
- def method_missing(m, *args, &block)
22
- append_node(m, args, &block)
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?(m, include_private = false)
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(k, v)
75
- "#{k}: #{argument_value_string(v)}"
74
+ def argument_string(key, val)
75
+ "#{key}: #{argument_value_string(val)}"
76
76
  end
77
77
 
78
- def variable_string(v)
79
- case v
78
+ def variable_string(val)
79
+ case val
80
80
  when :id, :id!
81
- v.to_s.upcase
82
- when ->(value) { SCALAR_TYPES.key?(value.to_s.delete('!').to_sym) }
81
+ val.to_s.upcase
82
+ when ->(v) { SCALAR_TYPES.key?(v.to_s.delete('!').to_sym) }
83
83
  # scalar types
84
- v.to_s.camelize
84
+ val.to_s.camelize
85
85
  when Array
86
- "[#{variable_string(v.first)}]"
86
+ "[#{variable_string(val.first)}]"
87
87
  else
88
- v.to_s
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
@@ -1,3 +1,3 @@
1
1
  module Graphlient
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.3.1'.freeze
3
3
  end
@@ -54,7 +54,7 @@ describe Graphlient::Adapters::HTTP::FaradayAdapter do
54
54
  end
55
55
 
56
56
  it 'retrieves schema' do
57
- expect(client.schema).to be_a GraphQL::Schema
57
+ expect(client.schema).to be_a Graphlient::Schema
58
58
  end
59
59
  end
60
60
  end
@@ -35,7 +35,7 @@ describe Graphlient::Adapters::HTTP::HTTPAdapter do
35
35
  end
36
36
 
37
37
  it 'retrieves schema' do
38
- expect(client.schema).to be_a GraphQL::Schema
38
+ expect(client.schema).to be_a Graphlient::Schema
39
39
  end
40
40
  end
41
41
  end
@@ -1,18 +1,41 @@
1
1
  require 'spec_helper'
2
+ require 'tempfile'
2
3
 
3
4
  describe Graphlient::Client do
4
- let(:client) { Graphlient::Client.new('http://graph.biz/graphql') }
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, 'http://graph.biz/graphql').to_return(status: 500)
10
+ stub_request(:post, url)
11
+ .to_return(body: DummySchema.execute(GraphQL::Introspection::INTROSPECTION_QUERY).to_json)
9
12
  end
10
13
 
11
- it 'fails with an exception' do
12
- expect do
13
- client.schema
14
- end.to raise_error Graphlient::Errors::ServerError do |e|
15
- expect(e.to_s).to eq 'the server responded with status 500'
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.0
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-02-22 00:00:00.000000000 Z
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.7.4
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.