autographql 0.0.3 → 0.0.4

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
  SHA1:
3
- metadata.gz: 2a344ec93114c212ee1fd878531ebac02deb094b
4
- data.tar.gz: f4ada4a255109550cb3f35a56ec7197419a0b2e5
3
+ metadata.gz: 1e503466113c7bc7a2cc79a2157d98c2e7a07949
4
+ data.tar.gz: 3589c398d5a468a75d66ae3f77c366b47e2ffcc8
5
5
  SHA512:
6
- metadata.gz: 6f786b1fee44164b78d3528fd43d36c982c59988022b160a1a7e6e3a851bb1c8c33ac878347a5ab0d158d7bfc5e03a799db078180cb1f016142a8135e32f3b67
7
- data.tar.gz: 9e55f2c354eb9093eb338f7c7ad3e8cba4b33915ade4d5f9948f78fece6ee9d637e78a8a5fc7a627e04cf1c91ab143589910504d90d14b2e88c854370b2f6c0c
6
+ metadata.gz: dfec84b8d5bce5c5d32ee0fcb0c5628b6a79116590fc93ea394ce42aff91e968595b6503d309c7ef8e78f32873c12c23e03d6cfcecc471fcc0f337174ebe795b
7
+ data.tar.gz: 3e421cb7d47f3752dd99b4fd82cfcb2c0288dc97ea25ac85d896e4475f74e17fd301cface6ec86e5a40518e281efb77d83f9e09656327887d9c7d193fda8a937
@@ -5,7 +5,7 @@ require_relative 'autographql/object_type'
5
5
 
6
6
 
7
7
  module AutoGraphQL
8
- VERSION = '0.0.3'
8
+ VERSION = '0.0.4'
9
9
  end
10
10
 
11
11
 
@@ -5,9 +5,15 @@ module AutoGraphQL
5
5
  extend self
6
6
 
7
7
  @@models = {}
8
+ @@query_type = nil
9
+ @@type_map = nil
8
10
 
9
11
 
10
12
  def register model, options = {}
13
+ unless @@query_type.nil?
14
+ raise RuntimeError, 'registration not allowed after generation. please register model sooner'
15
+ end
16
+
11
17
  # sanitize options
12
18
 
13
19
  name = options.fetch(:name, model.name)
@@ -47,15 +53,28 @@ module AutoGraphQL
47
53
  end
48
54
 
49
55
 
50
- # dynamically generate ::QueryType
56
+ protected
57
+
51
58
  def const_missing const
52
59
  case const
60
+ when :Types
61
+ gen_types
53
62
  when :QueryType
54
- AutoGraphQL::QueryBuilder.build @@models
63
+ gen_query
55
64
  else
56
65
  super
57
66
  end
58
67
  end
59
68
 
60
69
 
70
+ private
71
+
72
+ def gen_types
73
+ @@type_map ||= AutoGraphQL::TypeBuilder.build @@models
74
+ end
75
+
76
+ def gen_query
77
+ @@query_type ||= AutoGraphQL::QueryBuilder.build gen_types
78
+ end
79
+
61
80
  end
@@ -9,16 +9,16 @@ module AutoGraphQL
9
9
  extend self
10
10
 
11
11
 
12
- def build models_and_opts
12
+ def build type_map
13
13
  # dynamically create a QueryType subclass
14
14
  Class.new GraphQL::Schema::Object do
15
15
  def self.name
16
16
  'AutoGraphQL::QueryType'
17
17
  end
18
18
 
19
- type_map = AutoGraphQL::TypeBuilder.build(models_and_opts)
20
-
21
19
  type_map.each do |model, type|
20
+ # for each model, create an lookup function by ID
21
+
22
22
  # define field for this type
23
23
  field type.name.downcase, type, null: true do
24
24
  argument :id, GraphQL::Types::ID, required: true
@@ -32,7 +32,5 @@ module AutoGraphQL
32
32
  end
33
33
 
34
34
  end
35
-
36
-
37
35
  end
38
36
  end
@@ -37,7 +37,15 @@ module AutoGraphQL
37
37
  def build_type model, opts
38
38
  column_types = Hash[model.columns_hash.map do |name, column|
39
39
  next nil unless opts[:fields].include? name.to_sym
40
- [ name.to_sym, convert_type(model, column) ]
40
+
41
+ type = convert_type(column.type)
42
+ unless type
43
+ raise TypeError.new(
44
+ "unsupported type: '#{column.type}' for #{model.name}::#{column.name}"
45
+ )
46
+ end
47
+
48
+ [ name.to_sym, type ]
41
49
  end.compact]
42
50
 
43
51
  # create type
@@ -59,8 +67,25 @@ module AutoGraphQL
59
67
  methods.each do |name, type|
60
68
  name = name.to_s
61
69
 
62
- if type_map.include? type
63
- type = type_map[type]
70
+ if type.is_a? Array
71
+ list_type = true
72
+ unless type.length == 1
73
+ raise ArgumentError, "invalid type: #{type}"
74
+ end
75
+
76
+ type = type.first
77
+ else
78
+ list_type = false
79
+ end
80
+
81
+ type = if type_map.include? type
82
+ type_map[type]
83
+ else
84
+ convert_type type
85
+ end
86
+
87
+ if list_type
88
+ type = type.to_list_type
64
89
  end
65
90
 
66
91
  field = GraphQL::Field.define do
@@ -102,22 +127,25 @@ module AutoGraphQL
102
127
 
103
128
 
104
129
  # convert Active Record type to GraphQL type
105
- def convert_type model, column
130
+ def convert_type type
131
+ return type if type.is_a? GraphQL::BaseType
132
+
133
+ unless type.is_a? Symbol
134
+ type = type.to_s.downcase.to_sym
135
+ end
136
+
106
137
  {
107
138
  boolean: GraphQL::BOOLEAN_TYPE,
108
139
  date: GraphQL::Types::DATE,
109
140
  datetime: GraphQL::Types::ISO8601DateTime,
110
141
  decimal: GraphQL::Types::DECIMAL,
111
142
  float: GraphQL::FLOAT_TYPE,
143
+ int: GraphQL::INT_TYPE,
112
144
  integer: GraphQL::INT_TYPE,
113
145
  json: GraphQL::Types::JSON,
114
146
  string: GraphQL::STRING_TYPE,
115
147
  text: GraphQL::STRING_TYPE,
116
- }.fetch column.type do
117
- raise TypeError.new(
118
- "unsupported type: '#{column.type}' for #{model.name}::#{column.name}"
119
- )
120
- end
148
+ }[type]
121
149
  end
122
150
 
123
151
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autographql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Pepper
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-14 00:00:00.000000000 Z
11
+ date: 2018-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord