graphql-api 0.1.3 → 0.1.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: d652e6c292fe53b4eaa24a3dfb39b249da991a2c
4
- data.tar.gz: 3432f617ad13ad0ab88e9e97e9ffa9f4796646d9
3
+ metadata.gz: 477daabc28be110eb038b74769ba03c0aafa0362
4
+ data.tar.gz: fbe73ba70e1eaf7f31c3b6e63eeb6c3f0f0d5be2
5
5
  SHA512:
6
- metadata.gz: f3e0045fa5285925525d48e3d5f33fdddb100add30c96dbb0165ce2da8f14d9a215d71edba21be5597302847339df686b75e6a378a37405771480919c212e487
7
- data.tar.gz: 73bf40bf5b59c763b5c1906e5933d78a1ba9c357a4ee2c63296e90b2713f7f6c08d0c114d0eff119fca6c3a2a5f12faa470c4c331f8a5c37c706c7346a7b32d0
6
+ metadata.gz: ad96e49af75b0e1e957d681b07e0654da9f92bddcc9f191e373ac97cb80bc2398cdd346ef229e772a46210d0a176515cdfbe845e072fc9fbbba53cc10a212e55
7
+ data.tar.gz: 63d532a93ea3d90fe477d8111e257831ebbc25bce61b840208c8b313d75f13710dd01adba7feae304eceaa6ebc2156ffc34f9d6aa077869115a1e34719d85ca0
data/README.md CHANGED
@@ -183,7 +183,35 @@ They take a set of inputs as well as a graphql context and provide a
183
183
  `perform` method that returns a Graphql understandable type. These objects
184
184
  give you an object oriented abstraction for handling mutations.
185
185
 
186
- Command objects must implement the interface defined in `GraphQL::Api::CommandType`
186
+ Command objects must implement the interface defined in `GraphQL::Api::CommandType`.
187
+
188
+ To better model controllers, you can define the commands `actions` this
189
+ will allow a command to respond to multiple methods on the same class. For
190
+ example, the following code will model a restful controller using commands.
191
+ The mutation will be prefixed with the action name. For example, the code
192
+ below will create a `updateBlogCommand` mutation as well as a `deleteBlogCommand`.
193
+
194
+ ```ruby
195
+ class BlogCommand < GraphQL::Api::CommandType
196
+ inputs name: :string, tags: [:string], id: :integer
197
+ returns blog: Blog
198
+
199
+ # this tells GraphQL-Api to make two mutations that call the below methods.
200
+ actions :update, :delete
201
+
202
+ def update
203
+ blog = Blog.find(inputs[:id])
204
+ blog.update!(inputs.to_h)
205
+ {blog: blog}
206
+ end
207
+
208
+ def delete
209
+ blog = Blog.find(inputs[:id]).destroy!
210
+ {blog: blog}
211
+ end
212
+
213
+ end
214
+ ```
187
215
 
188
216
  ### Query Objects
189
217
 
@@ -17,6 +17,11 @@ module GraphQL::Api
17
17
  @returns || {}
18
18
  end
19
19
 
20
+ def self.actions(*actions)
21
+ @actions = actions if actions.any?
22
+ @actions || []
23
+ end
24
+
20
25
  def perform
21
26
  end
22
27
 
@@ -0,0 +1,16 @@
1
+ module GraphQL::Api
2
+ module Resolvers
3
+ class CommandMutation
4
+
5
+ def initialize(command, action)
6
+ @command = command
7
+ @action = action
8
+ end
9
+
10
+ def call(inputs, ctx)
11
+ @command.new(inputs, ctx).send(@action)
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,20 @@
1
+ module GraphQL::Api
2
+ module Resolvers
3
+ class Field
4
+
5
+ def initialize(model, name)
6
+ @model = model
7
+ @name = name
8
+ end
9
+
10
+ def call(obj, args, ctx)
11
+ if obj.respond_to?("access_#{@name}?")
12
+ obj.send(@name) if obj.send("access_#{@name}?", ctx)
13
+ else
14
+ obj.send(@name)
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,16 @@
1
+ module GraphQL::Api
2
+ module Resolvers
3
+ class ModelCreateMutation
4
+
5
+ def initialize(model)
6
+ @model = model
7
+ end
8
+
9
+ def call(inputs, ctx)
10
+ item = @model.create!(inputs.to_h)
11
+ {@model.name.underscore.to_sym => item}
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module GraphQL::Api
2
+ module Resolvers
3
+ class ModelDeleteMutation
4
+
5
+ def initialize(model)
6
+ @model = model
7
+ end
8
+
9
+ def call(inputs, ctx)
10
+ item = @model.find(inputs[:id])
11
+ item.destroy!
12
+ {"#{@model.name.underscore.to_sym}_id".to_sym => inputs[:id]}
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ module GraphQL::Api
2
+ module Resolvers
3
+ class ModelFindQuery
4
+
5
+ def initialize(model)
6
+ @model = model
7
+ end
8
+
9
+ def call(obj, args, ctx)
10
+ if @model.respond_to?(:graph_find)
11
+ @model.graph_find(args, ctx)
12
+ else
13
+ @model.find_by!(args.to_h)
14
+ end
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ module GraphQL::Api
2
+ module Resolvers
3
+ class ModelListQuery
4
+
5
+ def initialize(model)
6
+ @model = model
7
+ end
8
+
9
+ def call(obj, args, ctx)
10
+ if @model.respond_to?(:graph_where)
11
+ @model.graph_where(args, ctx)
12
+ else
13
+ eager_load = []
14
+ ctx.irep_node.children.each do |child|
15
+ eager_load << child[0] if @model.reflections.find { |name, _| name == child[0] }
16
+ end
17
+
18
+ query_args = args.to_h
19
+ query_args.delete('limit')
20
+
21
+ q = @model.where(query_args)
22
+ q.eager_load(*eager_load) if eager_load.any?
23
+ q.limit(args[:limit] || 30)
24
+ end
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ module GraphQL::Api
2
+ module Resolvers
3
+ class ModelUpdateMutation
4
+
5
+ def initialize(model)
6
+ @model = model
7
+ end
8
+
9
+ def call(inputs, ctx)
10
+ item = @model.find(inputs[:id])
11
+ item.update!(inputs.to_h)
12
+ {@model.name.underscore.to_sym => item}
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module GraphQL::Api
2
+ module Resolvers
3
+ class QueryObjectQuery
4
+
5
+ def initialize(query_object)
6
+ @query_object = query_object
7
+ end
8
+
9
+ def call(obj, args, ctx)
10
+ @query_object.new(args, ctx).execute
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -2,6 +2,14 @@ require 'graphql/api/command_type'
2
2
  require 'graphql/api/query_type'
3
3
  require 'graphql/api/helpers'
4
4
  require 'graphql/api/schema_error'
5
+ require 'graphql/api/resolvers/model_create_mutation'
6
+ require 'graphql/api/resolvers/model_delete_mutation'
7
+ require 'graphql/api/resolvers/model_update_mutation'
8
+ require 'graphql/api/resolvers/model_find_query'
9
+ require 'graphql/api/resolvers/model_list_query'
10
+ require 'graphql/api/resolvers/query_object_query'
11
+ require 'graphql/api/resolvers/command_mutation'
12
+ require 'graphql/api/resolvers/field'
5
13
  require 'graphql'
6
14
 
7
15
  include GraphQL::Api::Helpers
@@ -45,7 +53,7 @@ module GraphQL::Api
45
53
  model_class.columns.each do |column|
46
54
  field column.name do
47
55
  type graphql_type(column)
48
- resolve -> (obj, args, ctx) { graphql_fetch(obj, ctx, column.name) }
56
+ resolve Resolvers::Field.new(model_class, column.name)
49
57
  end
50
58
  end
51
59
  end
@@ -64,7 +72,7 @@ module GraphQL::Api
64
72
  else
65
73
  type object_types[association.class_name.constantize]
66
74
  end
67
- resolve -> (obj, args, ctx) { graphql_fetch(obj, ctx, name) }
75
+ resolve Resolvers::Field.new(model_class, name)
68
76
  end
69
77
  end
70
78
  end
@@ -72,12 +80,12 @@ module GraphQL::Api
72
80
  end
73
81
  end
74
82
 
75
- def create_command_type(object_type)
83
+ def create_command_type(object_type, action)
76
84
  object_types = @types
77
85
 
78
86
  GraphQL::Relay::Mutation.define do
79
87
  name object_type.name
80
- description "Command #{object_type.name}"
88
+ description "Command #{object_type.name} #{action}"
81
89
 
82
90
  object_type.inputs.each do |input, type|
83
91
  input_field input, graphql_type_of(type)
@@ -87,9 +95,7 @@ module GraphQL::Api
87
95
  return_field return_name, graphql_type_for_object(return_type, object_types)
88
96
  end
89
97
 
90
- resolve -> (inputs, ctx) {
91
- object_type.new(inputs, ctx).perform
92
- }
98
+ resolve Resolvers::CommandMutation.new(object_type, action)
93
99
  end
94
100
  end
95
101
 
@@ -107,11 +113,7 @@ module GraphQL::Api
107
113
  end
108
114
 
109
115
  return_field model_class.name.underscore.to_sym, object_types[model_class]
110
-
111
- resolve -> (inputs, ctx) {
112
- item = model_class.create!(inputs.to_h)
113
- {model_class.name.underscore.to_sym => item}
114
- }
116
+ resolve Resolvers::ModelCreateMutation.new(model_class)
115
117
  end
116
118
  end
117
119
 
@@ -130,12 +132,7 @@ module GraphQL::Api
130
132
  end
131
133
 
132
134
  return_field model_class.name.underscore.to_sym, object_types[model_class]
133
-
134
- resolve -> (inputs, ctx) {
135
- item = model_class.find(inputs[:id])
136
- item.update!(inputs.to_h)
137
- {model_class.name.underscore.to_sym => item}
138
- }
135
+ resolve Resolvers::ModelUpdateMutation.new(model_class)
139
136
  end
140
137
  end
141
138
 
@@ -149,18 +146,14 @@ module GraphQL::Api
149
146
  input_field :id, !types.ID
150
147
 
151
148
  return_field "#{model_class.name.underscore}_id".to_sym, types.ID
152
-
153
- resolve -> (inputs, ctx) {
154
- item = model_class.find(inputs[:id]).destroy!
155
- {"#{model_class.name.underscore}_id".to_sym => item.id}
156
- }
149
+ resolve Resolvers::ModelDeleteMutation.new(model_class)
157
150
  end
158
151
  end
159
152
 
160
153
  def query(&block)
161
154
  object_types = @types
162
155
 
163
- @query ||= GraphQL::ObjectType.define do
156
+ GraphQL::ObjectType.define do
164
157
  name 'Query'
165
158
  description 'The query root for this schema'
166
159
 
@@ -179,13 +172,7 @@ module GraphQL::Api
179
172
  end
180
173
  end
181
174
 
182
- resolve -> (obj, args, ctx) {
183
- if object_class.respond_to?(:graph_find)
184
- object_class.graph_find(args, ctx)
185
- else
186
- object_class.find_by!(args.to_h)
187
- end
188
- }
175
+ resolve Resolvers::ModelFindQuery.new(object_class)
189
176
  end
190
177
 
191
178
  field(object_class.name.camelize(:lower).pluralize) do
@@ -198,23 +185,7 @@ module GraphQL::Api
198
185
  end
199
186
  end
200
187
 
201
- resolve -> (obj, args, ctx) {
202
- if object_class.respond_to?(:graph_where)
203
- object_class.graph_where(args, ctx)
204
- else
205
- eager_load = []
206
- ctx.irep_node.children.each do |child|
207
- eager_load << child[0] if object_class.reflections.find { |name, _| name == child[0] }
208
- end
209
-
210
- query_args = args.to_h
211
- query_args.delete('limit')
212
-
213
- q = object_class.where(query_args)
214
- q.eager_load(*eager_load) if eager_load.any?
215
- q.limit(args[:limit] || 30)
216
- end
217
- }
188
+ resolve Resolvers::ModelListQuery.new(object_class)
218
189
  end
219
190
 
220
191
  elsif object_class.respond_to?(:arguments) && object_class.respond_to?(:return_type)
@@ -226,9 +197,7 @@ module GraphQL::Api
226
197
  argument argument_name, graphql_type_of(argument_type)
227
198
  end
228
199
 
229
- resolve -> (obj, args, ctx) {
230
- object_class.new(args, ctx).execute
231
- }
200
+ resolve Resolvers::QueryObjectQuery.new(object_class)
232
201
  end
233
202
 
234
203
  end
@@ -240,7 +209,7 @@ module GraphQL::Api
240
209
  def mutation(&block)
241
210
  mutations = @mutations
242
211
 
243
- @mutation ||= GraphQL::ObjectType.define do
212
+ GraphQL::ObjectType.define do
244
213
  name 'Mutation'
245
214
  instance_eval(&block) if block
246
215
 
@@ -274,9 +243,16 @@ module GraphQL::Api
274
243
  end
275
244
 
276
245
  all_commands.each do |command|
277
- @mutations[command] = [
278
- [command.name.camelize(:lower), create_command_type(command)]
279
- ]
246
+ if command.respond_to?(:actions) && command.actions.any?
247
+ @mutations[command] = []
248
+ command.actions.each do |action|
249
+ @mutations[command] << ["#{action}#{command.name}", create_command_type(command, action)]
250
+ end
251
+ else
252
+ @mutations[command] = [
253
+ [command.name.camelize(:lower), create_command_type(command, :perform)]
254
+ ]
255
+ end
280
256
  end
281
257
  end
282
258
 
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Api
3
- VERSION = '0.1.3'
3
+ VERSION = '0.1.4'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin Walker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-01 00:00:00.000000000 Z
11
+ date: 2016-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -72,6 +72,14 @@ files:
72
72
  - lib/graphql/api/command_type.rb
73
73
  - lib/graphql/api/helpers.rb
74
74
  - lib/graphql/api/query_type.rb
75
+ - lib/graphql/api/resolvers/command_mutation.rb
76
+ - lib/graphql/api/resolvers/field.rb
77
+ - lib/graphql/api/resolvers/model_create_mutation.rb
78
+ - lib/graphql/api/resolvers/model_delete_mutation.rb
79
+ - lib/graphql/api/resolvers/model_find_query.rb
80
+ - lib/graphql/api/resolvers/model_list_query.rb
81
+ - lib/graphql/api/resolvers/model_update_mutation.rb
82
+ - lib/graphql/api/resolvers/query_object_query.rb
75
83
  - lib/graphql/api/schema.rb
76
84
  - lib/graphql/api/schema_error.rb
77
85
  - lib/graphql/api/version.rb