graphql 0.14.0 → 0.14.1

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: 3686533835888eeb91ed91024ede7ea3f2937d5c
4
- data.tar.gz: 767760791707ac1722002fc7e8b6b182686abeaa
3
+ metadata.gz: fb9a61168074bd76592f38e2eced93e30a79bcbd
4
+ data.tar.gz: 4533e8af54fb08af8ec72012fb20d5be69f544e9
5
5
  SHA512:
6
- metadata.gz: 514195ebd2aff35a181e013af868e78af66fa0ca5cceaafe318c92d30d200241f9531cd630d6c36155dd521cab3cf1f7a4b8fee600dc6bbbd8c8f6430d3dd47f
7
- data.tar.gz: 3f5018af47c4e5225eb5f2c00ba8e4a1335e0ac56723908bb7bf8feffde7e567d7ac51f8eca8153249ec56a377b94ac257754694533727537b9d22403a3f12d9
6
+ metadata.gz: 4c02839e76d229ac5f886544c03711a7d515209a280ac2629ecc79645182fc8e134a02a9f257c4ee3393e23e0d273fe3f2462ba1616f70d09d1ab70a147f7953
7
+ data.tar.gz: d235b4fe8ad2500e21f0da5b0a4102e916fa2575815ac7f9601c9d5febbcef91a0fa00c093b0239d982c8a60b7319a962d8026ea51ecf5f684eff0633006f17e
@@ -2,7 +2,7 @@ module GraphQL
2
2
  module Define
3
3
  # Turn field configs into a {GraphQL::Field} and attach it to a {GraphQL::ObjectType} or {GraphQL::InterfaceType}
4
4
  module AssignObjectField
5
- def self.call(fields_type, name, type = nil, desc = nil, field: nil, property: nil, &block)
5
+ def self.call(fields_type, name, type = nil, desc = nil, field: nil, deprecation_reason: nil, property: nil, &block)
6
6
  if block_given?
7
7
  field = GraphQL::Field.define(&block)
8
8
  else
@@ -11,6 +11,7 @@ module GraphQL
11
11
  type && field.type = type
12
12
  desc && field.description = desc
13
13
  property && field.property = property
14
+ deprecation_reason && field.deprecation_reason = deprecation_reason
14
15
  field.name ||= name.to_s
15
16
  fields_type.fields[name.to_s] = field
16
17
  end
@@ -30,6 +30,18 @@ module GraphQL
30
30
  def to_s
31
31
  "<GraphQL::Directive #{name}>"
32
32
  end
33
+
34
+ def on_field?
35
+ locations.include?(FIELD)
36
+ end
37
+
38
+ def on_fragment?
39
+ locations.include?(FRAGMENT_SPREAD) && locations.include?(INLINE_FRAGMENT)
40
+ end
41
+
42
+ def on_operation?
43
+ locations.include?(QUERY) && locations.include?(MUTATION) && locations.include?(SUBSCRIPTION)
44
+ end
33
45
  end
34
46
  end
35
47
 
@@ -67,6 +67,7 @@ module GraphQL
67
67
  # @param value_name [String] the string representation of this enum value
68
68
  # @return [Object] the underlying value for this enum value
69
69
  def coerce_non_null_input(value_name)
70
+ return nil unless @values_by_name.key?(value_name)
70
71
  @values_by_name.fetch(value_name).value
71
72
  end
72
73
 
@@ -5,4 +5,7 @@ GraphQL::Introspection::DirectiveType = GraphQL::ObjectType.define do
5
5
  field :description, types.String, "The description for this type"
6
6
  field :args, field: GraphQL::Introspection::ArgumentsField
7
7
  field :locations, !types[!GraphQL::Introspection::DirectiveLocationEnum]
8
+ field :onOperation, !types.Boolean, "Does this directive apply to operations?", deprecation_reason: "Moved to 'locations' field", property: :on_operation?
9
+ field :onFragment, !types.Boolean, "Does this directive apply to fragments?", deprecation_reason: "Moved to 'locations' field", property: :on_fragment?
10
+ field :onField, !types.Boolean, "Does this directive apply to fields?", deprecation_reason: "Moved to 'locations' field", property: :on_field?
8
11
  end
@@ -15,15 +15,21 @@ module GraphQL
15
15
  # @param query_string [String]
16
16
  # @param context [#[]] an arbitrary hash of values which you can access in {GraphQL::Field#resolve}
17
17
  # @param variables [Hash] values for `$variables` in the query
18
- # @param debug [Boolean] if true, errors are raised, if false, errors are put in the `errors` key
18
+ # @param debug [Boolean] DEPRECATED if true, errors are raised, if false, errors are put in the `errors` key
19
19
  # @param validate [Boolean] if true, `query_string` will be validated with {StaticValidation::Validator}
20
20
  # @param operation_name [String] if the query string contains many operations, this is the one which should be executed
21
21
  # @param root_value [Object] the object used to resolve fields on the root type
22
- def initialize(schema, query_string = nil, document: nil, context: nil, variables: {}, debug: false, validate: true, operation_name: nil, root_value: nil, max_depth: nil)
22
+ def initialize(schema, query_string = nil, document: nil, context: nil, variables: {}, debug: nil, validate: true, operation_name: nil, root_value: nil, max_depth: nil)
23
23
  fail ArgumentError, "a query string or document is required" unless query_string || document
24
24
 
25
25
  @schema = schema
26
- @debug = debug
26
+ if debug == false
27
+ warn("Muffling errors with `debug: false` is deprecated and will be removed. For a similar behavior, use `MySchema.middleware << GraphQL::Schema::CatchallMiddleware`.")
28
+ elsif debug == true
29
+ warn("`debug:` will be removed from a future GraphQL version (and raising errors will be the default behavior, like `debug: true`)")
30
+ end
31
+ @debug = debug || false
32
+
27
33
  @max_depth = max_depth || schema.max_depth
28
34
  @context = Context.new(query: self, values: context)
29
35
  @root_value = root_value
@@ -21,7 +21,7 @@ module GraphQL
21
21
  arg_value = coerce(arg_defn.type, ast_arg.value, variables)
22
22
  end
23
23
  if arg_value.nil?
24
- arg_value = arg_defn.default_value
24
+ arg_value = arg_defn.type.coerce_input(arg_defn.default_value)
25
25
  end
26
26
  values_hash[arg_name] = arg_value
27
27
  end
@@ -1,3 +1,4 @@
1
+ require "graphql/schema/catchall_middleware"
1
2
  require "graphql/schema/invalid_type_error"
2
3
  require "graphql/schema/middleware_chain"
3
4
  require "graphql/schema/rescue_middleware"
@@ -51,7 +52,7 @@ module GraphQL
51
52
  # @return [GraphQL::Schema::TypeMap] `{ name => type }` pairs of types in this schema
52
53
  def types
53
54
  @types ||= begin
54
- all_types = @orphan_types + [query, mutation, GraphQL::Introspection::SchemaType]
55
+ all_types = @orphan_types + [query, mutation, subscription, GraphQL::Introspection::SchemaType]
55
56
  GraphQL::Schema::ReduceTypes.reduce(all_types.compact)
56
57
  end
57
58
  end
@@ -0,0 +1,34 @@
1
+ module GraphQL
2
+ class Schema
3
+ # In early GraphQL versions, errors would be "automatically"
4
+ # rescued and replaced with `"Internal error"`. That behavior
5
+ # was undesirable but this middleware is offered for people who
6
+ # want to preserve it.
7
+ #
8
+ # It has a couple of differences from the previous behavior:
9
+ #
10
+ # - Other parts of the query _will_ be run (previously,
11
+ # execution would stop when the error was raised and the result
12
+ # would have no `"data"` key at all)
13
+ # - The entry in {Query::Context#errors} is a {GraphQL::ExecutionError}, _not_
14
+ # the originally-raised error.
15
+ # - The entry in the `"errors"` key includes the location of the field
16
+ # which raised the errors.
17
+ #
18
+ # @example Use CatchallMiddleware with your schema
19
+ # # All errors will be suppressed and replaced with "Internal error" messages
20
+ # MySchema.middleware << GraphQL::Schema::CatchallMiddleware
21
+ #
22
+ module CatchallMiddleware
23
+ MESSAGE = "Internal error"
24
+
25
+ # Rescue any error and replace it with a {GraphQL::ExecutionError}
26
+ # whose message is {MESSAGE}
27
+ def self.call(parent_type, parent_object, field_definition, field_args, query_context, next_middleware)
28
+ next_middleware.call
29
+ rescue StandardError => err
30
+ GraphQL::ExecutionError.new(MESSAGE)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module GraphQL
2
- VERSION = "0.14.0"
2
+ VERSION = "0.14.1"
3
3
  end
@@ -8,6 +8,10 @@ describe GraphQL::EnumType do
8
8
  assert_equal(1, enum.coerce_input("COW"))
9
9
  end
10
10
 
11
+ it "coerces invalid names to nil" do
12
+ assert_equal(nil, enum.coerce_input("YAKKITY"))
13
+ end
14
+
11
15
  it "coerces result values to value's value" do
12
16
  assert_equal("YAK", enum.coerce_result("YAK"))
13
17
  assert_equal("COW", enum.coerce_result(1))
@@ -4,7 +4,15 @@ describe GraphQL::Introspection::DirectiveType do
4
4
  let(:query_string) {%|
5
5
  query getDirectives {
6
6
  __schema {
7
- directives { name, args { name, type { name, ofType { name } } }, locations }
7
+ directives {
8
+ name,
9
+ args { name, type { name, ofType { name } } },
10
+ locations
11
+ # Deprecated fields:
12
+ onField
13
+ onFragment
14
+ onOperation
15
+ }
8
16
  }
9
17
  }
10
18
  |}
@@ -20,6 +28,9 @@ describe GraphQL::Introspection::DirectiveType do
20
28
  {"name"=>"if", "type"=>{"name"=>"Non-Null", "ofType"=>{"name"=>"Boolean"}}}
21
29
  ],
22
30
  "locations"=>["FIELD", "FRAGMENT_SPREAD", "INLINE_FRAGMENT"],
31
+ "onField" => true,
32
+ "onFragment" => true,
33
+ "onOperation" => false,
23
34
  },
24
35
  {
25
36
  "name" => "include",
@@ -27,6 +38,9 @@ describe GraphQL::Introspection::DirectiveType do
27
38
  {"name"=>"if", "type"=>{"name"=>"Non-Null", "ofType"=>{"name"=>"Boolean"}}}
28
39
  ],
29
40
  "locations"=>["FIELD", "FRAGMENT_SPREAD", "INLINE_FRAGMENT"],
41
+ "onField" => true,
42
+ "onFragment" => true,
43
+ "onOperation" => false,
30
44
  },
31
45
  ]
32
46
  }
@@ -217,7 +217,9 @@ describe GraphQL::Query do
217
217
  variableDefault: searchDairy(product: $searchWithDefault) {
218
218
  ... cheeseFields
219
219
  }
220
-
220
+ convertedDefault: fromSource {
221
+ ... cheeseFields
222
+ }
221
223
  }
222
224
  fragment cheeseFields on Cheese { flavor }
223
225
  |}
@@ -244,6 +246,12 @@ describe GraphQL::Query do
244
246
  assert_equal("Brie", result["data"]["variableDefault"]["flavor"])
245
247
  end
246
248
  end
249
+
250
+ describe "when the variable has a default needing conversion" do
251
+ it "uses the converted variable default" do
252
+ assert_equal([{"flavor" => "Brie"}, {"flavor" => "Gouda"}], result["data"]["convertedDefault"])
253
+ end
254
+ end
247
255
  end
248
256
 
249
257
  describe "query variables" do
@@ -0,0 +1,32 @@
1
+ require "spec_helper"
2
+
3
+ describe GraphQL::Schema::CatchallMiddleware do
4
+ let(:result) { DummySchema.execute(query_string) }
5
+ let(:query_string) {%| query noMilk { error }|}
6
+
7
+ before do
8
+ DummySchema.middleware << GraphQL::Schema::CatchallMiddleware
9
+ end
10
+
11
+ after do
12
+ DummySchema.middleware.delete(GraphQL::Schema::CatchallMiddleware)
13
+ end
14
+
15
+ describe "rescuing errors" do
16
+ let(:errors) { query.context.errors }
17
+
18
+ it "turns into error messages" do
19
+ expected = {
20
+ "data" => { "error" => nil },
21
+ "errors"=> [
22
+ {
23
+ "message"=>"Internal error",
24
+ "locations"=>[{"line"=>1, "column"=>17}],
25
+ },
26
+ ]
27
+ }
28
+ assert_equal(expected, result)
29
+ end
30
+ end
31
+
32
+ end
@@ -71,6 +71,9 @@ type __Directive {
71
71
  description: String
72
72
  args: [__InputValue!]!
73
73
  locations: [__DirectiveLocation!]!
74
+ onOperation: Boolean!
75
+ onFragment: Boolean!
76
+ onField: Boolean!
74
77
  }
75
78
 
76
79
  enum __DirectiveLocation {
@@ -185,7 +185,7 @@ end
185
185
  SourceFieldDefn = Proc.new {
186
186
  type GraphQL::ListType.new(of_type: CheeseType)
187
187
  description "Cheese from source"
188
- argument :source, !DairyAnimalEnum
188
+ argument :source, DairyAnimalEnum, default_value: "COW"
189
189
  resolve -> (target, arguments, context) {
190
190
  CHEESES.values.select{ |c| c.source == arguments["source"] }
191
191
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 0.14.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Mosolgo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-31 00:00:00.000000000 Z
11
+ date: 2016-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: codeclimate-test-reporter
@@ -262,6 +262,7 @@ files:
262
262
  - lib/graphql/query/variables.rb
263
263
  - lib/graphql/scalar_type.rb
264
264
  - lib/graphql/schema.rb
265
+ - lib/graphql/schema/catchall_middleware.rb
265
266
  - lib/graphql/schema/invalid_type_error.rb
266
267
  - lib/graphql/schema/middleware_chain.rb
267
268
  - lib/graphql/schema/possible_types.rb
@@ -334,6 +335,7 @@ files:
334
335
  - spec/graphql/query/variables_spec.rb
335
336
  - spec/graphql/query_spec.rb
336
337
  - spec/graphql/scalar_type_spec.rb
338
+ - spec/graphql/schema/catchall_middleware_spec.rb
337
339
  - spec/graphql/schema/middleware_chain_spec.rb
338
340
  - spec/graphql/schema/printer_spec.rb
339
341
  - spec/graphql/schema/reduce_types_spec.rb
@@ -426,6 +428,7 @@ test_files:
426
428
  - spec/graphql/query/variables_spec.rb
427
429
  - spec/graphql/query_spec.rb
428
430
  - spec/graphql/scalar_type_spec.rb
431
+ - spec/graphql/schema/catchall_middleware_spec.rb
429
432
  - spec/graphql/schema/middleware_chain_spec.rb
430
433
  - spec/graphql/schema/printer_spec.rb
431
434
  - spec/graphql/schema/reduce_types_spec.rb