graphql_rails 0.2.4 → 0.3.0

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
  SHA256:
3
- metadata.gz: a8937aa0faf2ed71af96882a43adb9518a454591784d17a1cf22e6c7486a5e90
4
- data.tar.gz: a4ade684332f5d86f9bfeaf782dfad0a3d9732ef80f731ec071dd0f782ab420e
3
+ metadata.gz: 84843dfb2dd6c8069ceca77154dfe4f4c81322ef312c8aff9be52256fce1f24b
4
+ data.tar.gz: df3429323db4fd4810ea247e9336d5ede5fb8642683f777f89ce73b901144b26
5
5
  SHA512:
6
- metadata.gz: 3297c4a8ecf0613b6458b10aa9ac19537e4697e95b9d984864e9ff8b28f06d4e2777402340fded49db42d48729f1d3fea6de36037a62069c80da96b0c8a9aec4
7
- data.tar.gz: 2704a4e4417461da176ef7d3feb8f716b79b6677c3f25a1fcebcb42745461a444289d0b1f267a025f67db81cb8347323b05797d739bdbfedfa0f4f6eb81a65ac
6
+ metadata.gz: ff6b7212f18149590204f784755df54eeb2aa8fa93f2f8e242b86f203983cd2b556c5b3ae93ebef3e441cc094d750b6d3d935518eda15e54e8b775b366c979cc
7
+ data.tar.gz: a00e8e0f625dbfe527cfc3fcc9ec89ab83f3256e72b1ec4efc986a752dcb78c53f412880cf1b1426125be019b95126bcf6bc62025c9824006f0f02eba3017544
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graphql_rails (0.2.4)
4
+ graphql_rails (0.3.0)
5
5
  activesupport (>= 4)
6
6
  graphql (~> 1)
7
7
 
data/README.md CHANGED
@@ -47,8 +47,8 @@ class User # works with any class including ActiveRecord
47
47
  # most common attributes, like :id, :name, :title has default type, so you don't have to specify it (but you can!)
48
48
  c.attribute :id
49
49
 
50
- c.attribute :email, :string
51
- c.attribute :surname, :string
50
+ c.attribute :email, type: :string
51
+ c.attribute :surname, type: :string
52
52
  end
53
53
  end
54
54
  ```
@@ -3,6 +3,7 @@
3
3
  require 'active_support/hash_with_indifferent_access'
4
4
  require 'graphql_rails/controller/configuration'
5
5
  require 'graphql_rails/controller/request'
6
+ require 'graphql_rails/controller/format_results'
6
7
 
7
8
  module GraphqlRails
8
9
  # base class for all graphql_rails controllers
@@ -26,15 +27,14 @@ module GraphqlRails
26
27
  end
27
28
 
28
29
  def call(method_name)
29
- begin
30
- self.class.controller_configuration.before_actions.each { |action_name| send(action_name) }
31
- response = public_send(method_name)
32
- render response if graphql_request.no_object_to_return?
33
- rescue StandardError => error
34
- render error: error
35
- end
30
+ call_with_rendering(method_name)
36
31
 
37
- graphql_request.object_to_return
32
+ FormatResults.new(
33
+ graphql_request.object_to_return,
34
+ action_config: self.class.action(method_name),
35
+ params: params,
36
+ graphql_context: graphql_request.context
37
+ ).call
38
38
  end
39
39
 
40
40
  protected
@@ -55,6 +55,14 @@ module GraphqlRails
55
55
 
56
56
  private
57
57
 
58
+ def call_with_rendering(method_name)
59
+ self.class.controller_configuration.before_actions.each { |action_name| send(action_name) }
60
+ response = public_send(method_name)
61
+ render response if graphql_request.no_object_to_return?
62
+ rescue StandardError => error
63
+ render error: error
64
+ end
65
+
58
66
  def grapqhl_errors_from_render_params(rendering_params)
59
67
  return [] unless rendering_params.is_a?(Hash)
60
68
  return [] if rendering_params.keys.count != 1
@@ -59,9 +59,22 @@ module GraphqlRails
59
59
  end
60
60
 
61
61
  def default_type
62
- type = default_inner_return_type
63
- type = type.to_list_type.to_non_null_type if route.collection?
64
- type
62
+ return default_collection_type if route.collection?
63
+ default_inner_return_type
64
+ end
65
+
66
+ def default_collection_type
67
+ if action_config.paginated?
68
+ default_connection_type
69
+ else
70
+ default_inner_return_type.to_list_type.to_non_null_type
71
+ end
72
+ end
73
+
74
+ def default_connection_type
75
+ model_graphql_type.define_connection do
76
+ field :total, types.Int, resolve: ->(obj, _args, _ctx) { obj.nodes.size }
77
+ end
65
78
  end
66
79
 
67
80
  def action_relative_path
@@ -7,7 +7,7 @@ module GraphqlRails
7
7
  class Controller
8
8
  # stores all graphql_rails contoller specific config
9
9
  class ActionConfiguration
10
- attr_reader :attributes, :return_type
10
+ attr_reader :attributes, :return_type, :pagination_options
11
11
 
12
12
  def initialize
13
13
  @attributes = {}
@@ -20,6 +20,11 @@ module GraphqlRails
20
20
  self
21
21
  end
22
22
 
23
+ def paginated(pagination_options = {})
24
+ @pagination_options = pagination_options
25
+ permit(:before, :after, first: :int, last: :int)
26
+ end
27
+
23
28
  def description(new_description = nil)
24
29
  if new_description
25
30
  @description = new_description
@@ -43,6 +48,10 @@ module GraphqlRails
43
48
  @can_return_nil
44
49
  end
45
50
 
51
+ def paginated?
52
+ !!pagination_options # rubocop:disable Style/DoubleNegation
53
+ end
54
+
46
55
  private
47
56
 
48
57
  def permit_attribute(name, type = nil)
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlRails
4
+ class Controller
5
+ # Convers raw controller results in to graphql-friendly format
6
+ class FormatResults
7
+ def initialize(original_result, action_config:, params:, graphql_context:)
8
+ @original_result = original_result
9
+ @action_config = action_config
10
+ @controller_params = params
11
+ @graphql_context = graphql_context
12
+ end
13
+
14
+ def call
15
+ if action_config.paginated? && original_result
16
+ paginated_result
17
+ else
18
+ original_result
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :original_result, :action_config, :controller_params, :graphql_context
25
+
26
+ def paginated_result
27
+ pagination_params = controller_params.slice(:first, :last, :before, :after)
28
+ pagination_options = action_config.pagination_options.merge(context: graphql_context)
29
+
30
+ GraphQL::Relay::BaseConnection
31
+ .connection_for_nodes(original_result)
32
+ .new(original_result, pagination_params, pagination_options)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -10,7 +10,9 @@ require 'graphql_rails/router/resource_routes_builder'
10
10
  module GraphqlRails
11
11
  # graphql router that mimics Rails.application.routes
12
12
  class Router
13
- RAW_ACTION_NAMES = %i[rescue_from query_analyzer instrument default_max_page_size].freeze
13
+ RAW_ACTION_NAMES = %i[
14
+ rescue_from query_analyzer instrument cursor_encoder default_max_page_size
15
+ ].freeze
14
16
 
15
17
  def self.draw(&block)
16
18
  router = new
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GraphqlRails
4
- VERSION = '0.2.4'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Povilas Jurčys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-11 00:00:00.000000000 Z
11
+ date: 2018-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -110,6 +110,7 @@ files:
110
110
  - lib/graphql_rails/controller/action_configuration.rb
111
111
  - lib/graphql_rails/controller/configuration.rb
112
112
  - lib/graphql_rails/controller/controller_function.rb
113
+ - lib/graphql_rails/controller/format_results.rb
113
114
  - lib/graphql_rails/controller/request.rb
114
115
  - lib/graphql_rails/errors/error.rb
115
116
  - lib/graphql_rails/errors/execution_error.rb