graphql_rails 0.2.2 → 0.2.3

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
2
  SHA256:
3
- metadata.gz: cab765aab19541db1305cc192a5654bb804e16ddfd9d32f7bbb6ea79e6b679c4
4
- data.tar.gz: f76c484cc5205b4e3a16cefe7d5a8a4fbac9f87eec54895316df35fc33e076a5
3
+ metadata.gz: 20a26e41a67193a0518f0883d6437afccaca80ccfdad3d633e8b75b101f386e5
4
+ data.tar.gz: c8731427ea2d337ab81032558b4838a27dfb3e90eca7fdf7b77ccd30df2795fd
5
5
  SHA512:
6
- metadata.gz: ac72dfbe00f7586cff4f194c7e6efb4d78e3dc25ba10e31dae92ea482b35a70ccfdf6eb87e8f57b147c88914cbda1dc39d31bba7313074f76d0f3f587e955d9b
7
- data.tar.gz: 2008679abf3acc2874c1564db1ebf022892e23189240a7ca9602beab323748620661c19d39bd41ca8441104c01101409ff5cd196be62ff25fcfcc1c7bf835f4a
6
+ metadata.gz: 04d7c8d897f828cfab27a7f55dc894689dd67fc06512d9f8a9ae2a2f45615fbcdbbebf4b6ed3e3314c514f5ad74aadb0816b11f25dbb36f74a54685e9ed7173c
7
+ data.tar.gz: 66a91e7cf1fe70cf4e604aad2660a3907ab65e57f68d40273ced867b2e2623f77ccb7d806c687e4a7dbb35b1b0dd8de93e76e0b33973594ce827a1b1451208dc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graphql_rails (0.2.2)
4
+ graphql_rails (0.2.3)
5
5
  activesupport (>= 4)
6
6
  graphql (~> 1)
7
7
 
@@ -6,20 +6,17 @@ require 'graphql_rails/attribute/attribute_type_parser'
6
6
  module GraphqlRails
7
7
  # contains info about single graphql attribute
8
8
  class Attribute
9
- attr_reader :name, :graphql_field_type, :property, :type_name
9
+ attr_reader :name, :graphql_field_type, :property, :type_name, :description
10
10
 
11
- def initialize(name, type = nil, hidden: false, property: name)
12
- @name = name.to_s
11
+ def initialize(name, type = nil, description: nil, property: name)
12
+ @original_name = name.to_s
13
+ @name = @original_name.tr('!', '')
13
14
  @type_name = type.to_s
14
15
  @graphql_field_type = parse_type(type || type_by_attribute_name)
15
- @hidden = hidden
16
+ @description = description
16
17
  @property = property.to_s
17
18
  end
18
19
 
19
- def hidden?
20
- @hidden
21
- end
22
-
23
20
  def field_name
24
21
  field =
25
22
  if name.end_with?('?')
@@ -31,17 +28,26 @@ module GraphqlRails
31
28
  field.camelize(:lower)
32
29
  end
33
30
 
31
+ def field_args
32
+ [field_name, graphql_field_type, { property: property.to_sym, description: description }]
33
+ end
34
+
34
35
  private
35
36
 
37
+ attr_reader :original_name
38
+
36
39
  def type_by_attribute_name
37
- case name
38
- when 'id', /_id\Z/
39
- GraphQL::ID_TYPE
40
- when /\?\Z/
41
- GraphQL::BOOLEAN_TYPE
42
- else
43
- GraphQL::STRING_TYPE
44
- end
40
+ type = \
41
+ case name
42
+ when 'id', /_id\Z/
43
+ GraphQL::ID_TYPE
44
+ when /\?\Z/
45
+ GraphQL::BOOLEAN_TYPE
46
+ else
47
+ GraphQL::STRING_TYPE
48
+ end
49
+
50
+ original_name['!'] ? type.to_non_null_type : type
45
51
  end
46
52
 
47
53
  def parse_type(type)
@@ -1,12 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'active_support/core_ext/string/inflections'
4
+ require 'graphql_rails/errors/error'
5
+ require 'graphql_rails/model'
4
6
  require_relative 'request'
5
7
 
6
8
  module GraphqlRails
7
9
  class Controller
8
10
  # analyzes route and extracts controller action related data
9
11
  class Action
12
+ class MissingConfigurationError < GraphqlRails::Error; end
13
+
10
14
  def initialize(route)
11
15
  @route = route
12
16
  end
@@ -27,11 +31,17 @@ module GraphqlRails
27
31
  @name ||= action_relative_path.split('#').last
28
32
  end
29
33
 
34
+ def description
35
+ action_config.description
36
+ end
37
+
30
38
  private
31
39
 
32
40
  attr_reader :route
33
41
 
34
42
  def default_inner_return_type
43
+ raise_missing_return_type_error if model_graphql_type.nil?
44
+
35
45
  if action_config.can_return_nil?
36
46
  model_graphql_type
37
47
  else
@@ -39,6 +49,15 @@ module GraphqlRails
39
49
  end
40
50
  end
41
51
 
52
+ def raise_missing_return_type_error
53
+ error_message = \
54
+ "Return type for #{route.path.inspect} is not defined. " \
55
+ "To do so, add `action(:#{name}).returns(YourType)` in #{controller.name} " \
56
+ "or make sure that you have model named #{namespaced_model_name}"
57
+
58
+ raise MissingConfigurationError, error_message
59
+ end
60
+
42
61
  def default_type
43
62
  type = default_inner_return_type
44
63
  type = type.to_list_type.to_non_null_type if route.collection?
@@ -62,7 +81,7 @@ module GraphqlRails
62
81
  end
63
82
 
64
83
  def action_model
65
- namespace = namespaced_controller_name.singularize.classify.split('::')
84
+ namespace = namespaced_model_name.split('::')
66
85
  model_name = namespace.pop
67
86
  model = nil
68
87
 
@@ -71,7 +90,7 @@ module GraphqlRails
71
90
  namespace.pop
72
91
  end
73
92
 
74
- model || model_name.constantize
93
+ model || namespaced_model(namespace, model_name)
75
94
  end
76
95
 
77
96
  def namespaced_model(namespace, model_name)
@@ -81,7 +100,14 @@ module GraphqlRails
81
100
  nil
82
101
  end
83
102
 
103
+ def namespaced_model_name
104
+ namespaced_controller_name.singularize.classify
105
+ end
106
+
84
107
  def model_graphql_type
108
+ return unless action_model
109
+ return unless action_model < GraphqlRails::Model
110
+
85
111
  action_model.graphql.graphql_type
86
112
  end
87
113
  end
@@ -20,6 +20,15 @@ module GraphqlRails
20
20
  self
21
21
  end
22
22
 
23
+ def description(new_description = nil)
24
+ if new_description
25
+ @description = new_description
26
+ self
27
+ else
28
+ @description
29
+ end
30
+ end
31
+
23
32
  def can_return_nil
24
33
  @can_return_nil = true
25
34
  self
@@ -20,19 +20,23 @@ module GraphqlRails
20
20
  def self.from_route(route)
21
21
  action = Action.new(route)
22
22
 
23
- action_function = Class.new(self) do
24
- action.arguments.each do |attribute|
25
- argument(attribute.field_name, attribute.graphql_field_type)
26
- end
27
- end
28
-
29
- action_function.new(
23
+ action_function_class(action).new(
30
24
  controller: action.controller,
31
25
  action_name: action.name,
32
26
  type: action.return_type
33
27
  )
34
28
  end
35
29
 
30
+ def self.action_function_class(action)
31
+ Class.new(self) do
32
+ description(action.description)
33
+
34
+ action.arguments.each do |attribute|
35
+ argument(attribute.field_name, attribute.graphql_field_type)
36
+ end
37
+ end
38
+ end
39
+
36
40
  def call(object, inputs, ctx)
37
41
  request = Request.new(object, inputs, ctx)
38
42
  controller.new(request).call(action_name)
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GraphqlRails
4
+ class Error < StandardError
5
+ end
6
+ end
@@ -18,12 +18,19 @@ module GraphqlRails
18
18
  @name ||= type_name
19
19
  end
20
20
 
21
- def description(description = nil)
22
- @description ||= description
21
+ def description(new_description = nil)
22
+ @description = new_description if new_description
23
+ @description
23
24
  end
24
25
 
25
- def attribute(attribute_name, type: nil, hidden: false, property: attribute_name)
26
- attributes[attribute_name.to_s] = Attribute.new(attribute_name, type, hidden: hidden, property: property)
26
+ def attribute(attribute_name, type: nil, description: nil, property: attribute_name)
27
+ attributes[attribute_name.to_s] = \
28
+ Attribute.new(
29
+ attribute_name,
30
+ type,
31
+ description: description,
32
+ property: property
33
+ )
27
34
  end
28
35
 
29
36
  def graphql_type
@@ -13,14 +13,14 @@ module GraphqlRails
13
13
  def call
14
14
  type_name = name
15
15
  type_description = description
16
- type_attributes = visible_attributes
16
+ type_attributes = attributes
17
17
 
18
18
  GraphQL::ObjectType.define do
19
19
  name(type_name)
20
20
  description(type_description)
21
21
 
22
22
  type_attributes.each_value do |attribute|
23
- field(attribute.field_name, attribute.graphql_field_type, property: attribute.property.to_sym)
23
+ field(*attribute.field_args)
24
24
  end
25
25
  end
26
26
  end
@@ -28,10 +28,6 @@ module GraphqlRails
28
28
  private
29
29
 
30
30
  attr_reader :model_configuration, :attributes, :name, :description
31
-
32
- def visible_attributes
33
- attributes.reject { |_name, attribute| attribute.hidden? }
34
- end
35
31
  end
36
32
  end
37
33
  end
@@ -63,7 +63,7 @@ module GraphqlRails
63
63
  end
64
64
 
65
65
  def build_route(builder, action, prefix: action, on: :member, **custom_options)
66
- action_options = options.merge(custom_options)
66
+ action_options = options.merge(custom_options).merge(on: on)
67
67
  action_name = [prefix, resource_name(on)].reject(&:empty?).join('_')
68
68
  builder.new(action_name, to: "#{name}##{action}", **action_options)
69
69
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GraphqlRails
4
- VERSION = '0.2.2'
4
+ VERSION = '0.2.3'
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.2
4
+ version: 0.2.3
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-05-28 00:00:00.000000000 Z
11
+ date: 2018-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: graphql
@@ -111,6 +111,7 @@ files:
111
111
  - lib/graphql_rails/controller/configuration.rb
112
112
  - lib/graphql_rails/controller/controller_function.rb
113
113
  - lib/graphql_rails/controller/request.rb
114
+ - lib/graphql_rails/errors/error.rb
114
115
  - lib/graphql_rails/errors/execution_error.rb
115
116
  - lib/graphql_rails/errors/validation_error.rb
116
117
  - lib/graphql_rails/model.rb