graphql-rails 0.0.4 → 0.0.5

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
  SHA1:
3
- metadata.gz: f1ed634405b822417ac8233e7142da5423577846
4
- data.tar.gz: 23f85a17aa056fa70e98be01a10599185213e0a2
3
+ metadata.gz: 057f13274ec2fa2303ce2a0e8c9cfc5a9aa9be78
4
+ data.tar.gz: 0a33325845d6af90994ad94e164e6f1a56fa14c8
5
5
  SHA512:
6
- metadata.gz: f61c5ffc5274b98cb37c88c33c9d7bfa79f812e8f0da07bd71e7a9473b0b7ebbb5e660d74a85898f42da042a508d73077d7c1203b776ba1fae4e9b9e2860888e
7
- data.tar.gz: 61110d68962bba071490b8d86bfe7dc71675bab7aa35e0c3f1f6bb24827eb807ab917c27fac20e697384fa5e20f771dc518c752397725a82eb9507629dbd73a8
6
+ metadata.gz: 757acce6e810e6b54dfeb031803f1c84579fd5c628f85d95234b4a187e61333f4e08157fc3b2409be9c8868d09b4298d0c1dd57abe3f1bfa01e208a32a4132e2
7
+ data.tar.gz: 5b61fef40d244b6971086d7c431d640ed5fbc34c57886e1dd526a69140aee0edb2b77839efe106e82632bd5743d1fcb1aa9bb4cdee09c520d7c03b3390547d9d
@@ -11,6 +11,7 @@ require 'graphql/rails/engine'
11
11
 
12
12
  require 'graphql/rails/dsl'
13
13
  require 'graphql/rails/types'
14
+ require 'graphql/rails/fields'
14
15
  require 'graphql/rails/schema'
15
16
  require 'graphql/rails/callbacks'
16
17
  require 'graphql/rails/operations'
@@ -0,0 +1,11 @@
1
+ module GraphQL
2
+ module Rails
3
+ # Object that transforms key lookups on an object to the active field
4
+ # naming convention, delegating all remaining methods as-is.
5
+ class Fields < SimpleDelegator
6
+ def [](key)
7
+ __getobj__[Types.to_field_name(key)]
8
+ end
9
+ end
10
+ end
11
+ end
@@ -62,7 +62,7 @@ module GraphQL
62
62
  def self.mutation(hash, &block)
63
63
  hash = extract_pair(hash)
64
64
  unless hash[:type].is_a?(Hash)
65
- raise 'Mutations must be specified with Hash results'
65
+ raise 'Mutation must be specified with a Hash result type'
66
66
  end
67
67
  Rails.logger.debug "Adding mutation: #{Types.to_field_name(hash[:name])}"
68
68
 
@@ -112,11 +112,11 @@ module GraphQL
112
112
  end
113
113
 
114
114
  def resolve(&block)
115
- field.resolve = -> (obj, args, ctx) do
115
+ @field.resolve = -> (obj, args, ctx) do
116
116
  # Instantiate the Operations class with state on this query.
117
117
  instance = @klass.new({
118
118
  op: :query, name: @name, type: @type,
119
- obj: obj, args: args, ctx: ctx, context: ctx
119
+ obj: obj, args: Fields.new(args), ctx: ctx, context: ctx
120
120
  })
121
121
 
122
122
  begin
@@ -165,8 +165,9 @@ module GraphQL
165
165
  end
166
166
 
167
167
  def field
168
+ # Build input object according to mutation specification.
168
169
  input = @input
169
- input.name = "#{@field.name}Input"
170
+ input.name = "#{@name.to_s.camelize(:upper)}Input"
170
171
  input.description = "Generated input type for #{@field.name}"
171
172
  input.arguments['clientMutationId'] = ::GraphQL::Argument.define do
172
173
  name 'clientMutationId'
@@ -174,8 +175,9 @@ module GraphQL
174
175
  description 'Unique identifier for client performing mutation'
175
176
  end
176
177
 
178
+ # Build compound output object according to mutation specification.
177
179
  output = @output
178
- output.name = "#{@field.name}Output"
180
+ output.name = "#{@name.to_s.camelize(:upper)}Output"
179
181
  output.description = "Generated output type for #{@field.name}"
180
182
  output.fields['clientMutationId'] = ::GraphQL::Field.define do
181
183
  name 'clientMutationId'
@@ -192,11 +194,11 @@ module GraphQL
192
194
  end
193
195
 
194
196
  def resolve(&block)
195
- field.resolve = -> (obj, args, ctx) do
197
+ @field.resolve = -> (obj, args, ctx) do
196
198
  # Instantiate the Operations class with state on this query.
197
199
  instance = @klass.new({
198
200
  op: :mutation, name: @name, type: @type,
199
- obj: obj, args: args[:input], ctx: ctx, context: ctx
201
+ obj: obj, args: Fields.new(args[:input]), ctx: ctx, context: ctx
200
202
  })
201
203
 
202
204
  begin
@@ -204,7 +206,18 @@ module GraphQL
204
206
  instance.run_callbacks(:perform_operation) do
205
207
  # Call out to the app-defined resolver.
206
208
  result = instance.instance_eval(&block)
207
- result[:clientMutationId] = args[:clientMutationId]
209
+
210
+ # Transform the result keys to the expected convention.
211
+ unless result.is_a?(::Hash)
212
+ raise 'Mutation must resolve to a Hash result'
213
+ end
214
+
215
+ result = result.inject({
216
+ 'clientMutationId' => args['clientMutationId']
217
+ }) do |hash, (key, value)|
218
+ hash[Types.to_field_name(key)] = value
219
+ hash
220
+ end
208
221
  ::OpenStruct.new(result)
209
222
  end
210
223
  rescue => e
@@ -26,7 +26,6 @@ module GraphQL
26
26
  @schema ||= GraphQL::Schema.new begin
27
27
  TYPES.reduce({
28
28
  max_depth: Rails.config.max_depth,
29
- types: [ExerciseTaskType]
30
29
  }) do |schema, type|
31
30
  fields = @fields[type]
32
31
  unless fields.empty?
@@ -1,5 +1,5 @@
1
1
  module GraphQL
2
2
  module Rails
3
- VERSION = '0.0.4'
3
+ VERSION = '0.0.5'
4
4
  end
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.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Reggio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-03 00:00:00.000000000 Z
11
+ date: 2016-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -104,6 +104,7 @@ files:
104
104
  - lib/graphql/rails/engine.rb
105
105
  - lib/graphql/rails/extensions/cancan.rb
106
106
  - lib/graphql/rails/extensions/mongoid.rb
107
+ - lib/graphql/rails/fields.rb
107
108
  - lib/graphql/rails/node_identification.rb
108
109
  - lib/graphql/rails/operations.rb
109
110
  - lib/graphql/rails/schema.rb