graphql 1.8.1 → 1.8.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/lib/generators/graphql/core.rb +1 -1
  3. data/lib/generators/graphql/enum_generator.rb +3 -3
  4. data/lib/generators/graphql/install_generator.rb +12 -1
  5. data/lib/generators/graphql/mutation_generator.rb +4 -4
  6. data/lib/generators/graphql/templates/base_enum.erb +2 -0
  7. data/lib/generators/graphql/templates/base_input_object.erb +2 -0
  8. data/lib/generators/graphql/templates/base_interface.erb +3 -0
  9. data/lib/generators/graphql/templates/base_object.erb +2 -0
  10. data/lib/generators/graphql/templates/base_union.erb +2 -0
  11. data/lib/generators/graphql/templates/enum.erb +1 -2
  12. data/lib/generators/graphql/templates/interface.erb +2 -3
  13. data/lib/generators/graphql/templates/mutation.erb +4 -5
  14. data/lib/generators/graphql/templates/mutation_type.erb +6 -9
  15. data/lib/generators/graphql/templates/object.erb +2 -3
  16. data/lib/generators/graphql/templates/query_type.erb +6 -8
  17. data/lib/generators/graphql/templates/schema.erb +7 -7
  18. data/lib/generators/graphql/templates/union.erb +1 -2
  19. data/lib/generators/graphql/type_generator.rb +33 -18
  20. data/lib/generators/graphql/union_generator.rb +1 -1
  21. data/lib/graphql/execution/execute.rb +3 -0
  22. data/lib/graphql/schema.rb +1 -0
  23. data/lib/graphql/schema/input_object.rb +22 -1
  24. data/lib/graphql/schema/relay_classic_mutation.rb +6 -2
  25. data/lib/graphql/static_validation/all_rules.rb +1 -0
  26. data/lib/graphql/static_validation/rules/argument_names_are_unique.rb +31 -0
  27. data/lib/graphql/subscriptions/instrumentation.rb +3 -0
  28. data/lib/graphql/version.rb +1 -1
  29. data/spec/generators/graphql/enum_generator_spec.rb +1 -2
  30. data/spec/generators/graphql/install_generator_spec.rb +23 -24
  31. data/spec/generators/graphql/interface_generator_spec.rb +5 -6
  32. data/spec/generators/graphql/mutation_generator_spec.rb +10 -27
  33. data/spec/generators/graphql/object_generator_spec.rb +7 -10
  34. data/spec/generators/graphql/union_generator_spec.rb +3 -6
  35. data/spec/graphql/execution/execute_spec.rb +97 -0
  36. data/spec/graphql/schema/input_object_spec.rb +32 -0
  37. data/spec/graphql/schema/relay_classic_mutation_spec.rb +16 -0
  38. data/spec/graphql/static_validation/rules/argument_names_are_unique_spec.rb +44 -0
  39. data/spec/graphql/subscriptions_spec.rb +21 -1
  40. data/spec/support/jazz.rb +15 -2
  41. metadata +10 -6
  42. data/lib/generators/graphql/function_generator.rb +0 -18
  43. data/lib/generators/graphql/templates/function.erb +0 -17
  44. data/spec/generators/graphql/function_generator_spec.rb +0 -59
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+ require "spec_helper"
3
+
4
+ describe GraphQL::StaticValidation::ArgumentNamesAreUnique do
5
+ include StaticValidationHelpers
6
+
7
+ describe "field arguments" do
8
+ let(:query_string) { <<-GRAPHQL
9
+ query GetStuff {
10
+ c1: cheese(id: 1, id: 2) { flavor }
11
+ c2: cheese(id: 2) { flavor }
12
+ }
13
+ GRAPHQL
14
+ }
15
+
16
+ it "finds duplicate names" do
17
+ assert_equal 1, errors.size
18
+
19
+ error = errors.first
20
+ assert_equal 'There can be only one argument named "id"', error["message"]
21
+ assert_equal [{ "line" => 2, "column" => 18}, { "line" => 2, "column" => 25 }], error["locations"]
22
+ assert_equal ["query GetStuff", "c1"], error["fields"]
23
+ end
24
+ end
25
+
26
+ describe "directive arguments" do
27
+ let(:query_string) { <<-GRAPHQL
28
+ query GetStuff {
29
+ c1: cheese(id: 1) @include(if: true, if: true) { flavor }
30
+ c2: cheese(id: 2) @include(if: true) { flavor }
31
+ }
32
+ GRAPHQL
33
+ }
34
+
35
+ it "finds duplicate names" do
36
+ assert_equal 1, errors.size
37
+
38
+ error = errors.first
39
+ assert_equal 'There can be only one argument named "if"', error["message"]
40
+ assert_equal [{ "line" => 2, "column" => 34}, { "line" => 2, "column" => 44 }], error["locations"]
41
+ assert_equal ["query GetStuff", "c1"], error["fields"]
42
+ end
43
+ end
44
+ end
@@ -132,6 +132,10 @@ class ClassBasedInMemoryBackend < InMemoryBackend
132
132
  def my_event(type: nil)
133
133
  object
134
134
  end
135
+
136
+ field :failed_event, Payload, null: false, resolve: ->(o, a, c) { raise GraphQL::ExecutionError.new("unauthorized") } do
137
+ argument :id, ID, required: true
138
+ end
135
139
  end
136
140
 
137
141
  class Query < GraphQL::Schema::Object
@@ -151,6 +155,7 @@ class FromDefinitionInMemoryBackend < InMemoryBackend
151
155
  payload(id: ID!): Payload!
152
156
  event(stream: StreamInput): Payload
153
157
  myEvent(type: PayloadType): Payload
158
+ failedEvent(id: ID!): Payload!
154
159
  }
155
160
 
156
161
  type Payload {
@@ -180,6 +185,7 @@ class FromDefinitionInMemoryBackend < InMemoryBackend
180
185
  "payload" => ->(o,a,c) { o },
181
186
  "myEvent" => ->(o,a,c) { o },
182
187
  "event" => ->(o,a,c) { o },
188
+ "failedEvent" => ->(o,a,c) { raise GraphQL::ExecutionError.new("unauthorized") },
183
189
  },
184
190
  }
185
191
  Schema = GraphQL::Schema.from_definition(SchemaDefinition, default_resolve: Resolvers).redefine do
@@ -410,7 +416,21 @@ describe GraphQL::Subscriptions do
410
416
  end
411
417
  end
412
418
 
413
- it "lets unhandled errors crash "do
419
+ it "avoid subscription on resolver error" do
420
+ res = schema.execute(<<-GRAPHQL, context: { socket: "1" }, variables: { "id" => "100" })
421
+ subscription ($id: ID!){
422
+ failedEvent(id: $id) { str, int }
423
+ }
424
+ GRAPHQL
425
+
426
+ assert_equal nil, res["data"]
427
+ assert_equal "unauthorized", res["errors"][0]["message"]
428
+
429
+ # this is to make sure nothing actually got subscribed.. but I don't have any idea better than checking its instance variable
430
+ assert_equal 0, schema.subscriptions.instance_variable_get(:@subscriptions).size
431
+ end
432
+
433
+ it "lets unhandled errors crash" do
414
434
  query_str = <<-GRAPHQL
415
435
  subscription($type: PayloadType) {
416
436
  myEvent(type: $type) { int }
data/spec/support/jazz.rb CHANGED
@@ -45,9 +45,9 @@ module Jazz
45
45
  end
46
46
 
47
47
  class BaseArgument < GraphQL::Schema::Argument
48
- def initialize(name, type, desc = nil, custom: nil, **kwargs)
48
+ def initialize(*args, custom: nil, **kwargs)
49
49
  @custom = custom
50
- super(name, type, desc, **kwargs)
50
+ super(*args, **kwargs)
51
51
  end
52
52
 
53
53
  def to_graphql
@@ -446,12 +446,25 @@ module Jazz
446
446
  end
447
447
  end
448
448
 
449
+ class AddSitar < GraphQL::Schema::RelayClassicMutation
450
+ null true
451
+ description "Get Sitar to musical instrument"
452
+
453
+ field :instrument, InstrumentType, null: false
454
+
455
+ def resolve
456
+ instrument = Models::Instrument.new("Sitar", :str)
457
+ { instrument: instrument }
458
+ end
459
+ end
460
+
449
461
  class Mutation < BaseObject
450
462
  field :add_ensemble, Ensemble, null: false do
451
463
  argument :input, EnsembleInput, required: true
452
464
  end
453
465
 
454
466
  field :add_instrument, mutation: AddInstrument
467
+ field :add_sitar, mutation: AddSitar
455
468
 
456
469
  def add_ensemble(input:)
457
470
  ens = Models::Ensemble.new(input.name)
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: 1.8.1
4
+ version: 1.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Mosolgo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-01 00:00:00.000000000 Z
11
+ date: 2018-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: benchmark-ips
@@ -315,14 +315,17 @@ files:
315
315
  - MIT-LICENSE
316
316
  - lib/generators/graphql/core.rb
317
317
  - lib/generators/graphql/enum_generator.rb
318
- - lib/generators/graphql/function_generator.rb
319
318
  - lib/generators/graphql/install_generator.rb
320
319
  - lib/generators/graphql/interface_generator.rb
321
320
  - lib/generators/graphql/loader_generator.rb
322
321
  - lib/generators/graphql/mutation_generator.rb
323
322
  - lib/generators/graphql/object_generator.rb
323
+ - lib/generators/graphql/templates/base_enum.erb
324
+ - lib/generators/graphql/templates/base_input_object.erb
325
+ - lib/generators/graphql/templates/base_interface.erb
326
+ - lib/generators/graphql/templates/base_object.erb
327
+ - lib/generators/graphql/templates/base_union.erb
324
328
  - lib/generators/graphql/templates/enum.erb
325
- - lib/generators/graphql/templates/function.erb
326
329
  - lib/generators/graphql/templates/graphql_controller.erb
327
330
  - lib/generators/graphql/templates/interface.erb
328
331
  - lib/generators/graphql/templates/loader.erb
@@ -540,6 +543,7 @@ files:
540
543
  - lib/graphql/static_validation/literal_validator.rb
541
544
  - lib/graphql/static_validation/message.rb
542
545
  - lib/graphql/static_validation/rules/argument_literals_are_compatible.rb
546
+ - lib/graphql/static_validation/rules/argument_names_are_unique.rb
543
547
  - lib/graphql/static_validation/rules/arguments_are_defined.rb
544
548
  - lib/graphql/static_validation/rules/directives_are_defined.rb
545
549
  - lib/graphql/static_validation/rules/directives_are_in_valid_locations.rb
@@ -942,7 +946,6 @@ files:
942
946
  - spec/fixtures/upgrader/type_x.original.rb
943
947
  - spec/fixtures/upgrader/type_x.transformed.rb
944
948
  - spec/generators/graphql/enum_generator_spec.rb
945
- - spec/generators/graphql/function_generator_spec.rb
946
949
  - spec/generators/graphql/install_generator_spec.rb
947
950
  - spec/generators/graphql/interface_generator_spec.rb
948
951
  - spec/generators/graphql/loader_generator_spec.rb
@@ -1056,6 +1059,7 @@ files:
1056
1059
  - spec/graphql/schema/warden_spec.rb
1057
1060
  - spec/graphql/schema_spec.rb
1058
1061
  - spec/graphql/static_validation/rules/argument_literals_are_compatible_spec.rb
1062
+ - spec/graphql/static_validation/rules/argument_names_are_unique_spec.rb
1059
1063
  - spec/graphql/static_validation/rules/arguments_are_defined_spec.rb
1060
1064
  - spec/graphql/static_validation/rules/directives_are_defined_spec.rb
1061
1065
  - spec/graphql/static_validation/rules/directives_are_in_valid_locations_spec.rb
@@ -1489,7 +1493,6 @@ test_files:
1489
1493
  - spec/fixtures/upgrader/type_x.original.rb
1490
1494
  - spec/fixtures/upgrader/type_x.transformed.rb
1491
1495
  - spec/generators/graphql/enum_generator_spec.rb
1492
- - spec/generators/graphql/function_generator_spec.rb
1493
1496
  - spec/generators/graphql/install_generator_spec.rb
1494
1497
  - spec/generators/graphql/interface_generator_spec.rb
1495
1498
  - spec/generators/graphql/loader_generator_spec.rb
@@ -1603,6 +1606,7 @@ test_files:
1603
1606
  - spec/graphql/schema/warden_spec.rb
1604
1607
  - spec/graphql/schema_spec.rb
1605
1608
  - spec/graphql/static_validation/rules/argument_literals_are_compatible_spec.rb
1609
+ - spec/graphql/static_validation/rules/argument_names_are_unique_spec.rb
1606
1610
  - spec/graphql/static_validation/rules/arguments_are_defined_spec.rb
1607
1611
  - spec/graphql/static_validation/rules/directives_are_defined_spec.rb
1608
1612
  - spec/graphql/static_validation/rules/directives_are_in_valid_locations_spec.rb
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
- require "rails/generators/named_base"
3
- require_relative "core"
4
-
5
- module Graphql
6
- module Generators
7
- class FunctionGenerator < Rails::Generators::NamedBase
8
- include Core
9
-
10
- desc "Create a GraphQL::Function by name"
11
- source_root File.expand_path('../templates', __FILE__)
12
-
13
- def create_function_file
14
- template "function.erb", "#{options[:directory]}/functions/#{file_path}.rb"
15
- end
16
- end
17
- end
18
- end
@@ -1,17 +0,0 @@
1
- class Functions::<%= class_name %> < GraphQL::Function
2
- # Define `initialize` to store field-level options, eg
3
- #
4
- # field :myField, function: Functions::<%= class_name %>.new(type: MyType)
5
- #
6
- # attr_reader :type
7
- # def initialize(type:)
8
- # @type = type
9
- # end
10
-
11
- # add arguments by type:
12
- # argument :id, !types.ID
13
-
14
- # Resolve function:
15
- def call(obj, args, ctx)
16
- end
17
- end
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
- require "spec_helper"
3
- require "generators/graphql/function_generator"
4
-
5
- class GraphQLGeneratorsFunctionGeneratorTest < BaseGeneratorTest
6
- tests Graphql::Generators::FunctionGenerator
7
-
8
- test "it generates an empty function by name" do
9
- run_generator(["FindRecord"])
10
-
11
- expected_content = <<-RUBY
12
- class Functions::FindRecord < GraphQL::Function
13
- # Define `initialize` to store field-level options, eg
14
- #
15
- # field :myField, function: Functions::FindRecord.new(type: MyType)
16
- #
17
- # attr_reader :type
18
- # def initialize(type:)
19
- # @type = type
20
- # end
21
-
22
- # add arguments by type:
23
- # argument :id, !types.ID
24
-
25
- # Resolve function:
26
- def call(obj, args, ctx)
27
- end
28
- end
29
- RUBY
30
-
31
- assert_file "app/graphql/functions/find_record.rb", expected_content
32
- end
33
-
34
- test "it generates a namespaced function by name" do
35
- run_generator(["finders::find_record"])
36
-
37
- expected_content = <<-RUBY
38
- class Functions::Finders::FindRecord < GraphQL::Function
39
- # Define `initialize` to store field-level options, eg
40
- #
41
- # field :myField, function: Functions::Finders::FindRecord.new(type: MyType)
42
- #
43
- # attr_reader :type
44
- # def initialize(type:)
45
- # @type = type
46
- # end
47
-
48
- # add arguments by type:
49
- # argument :id, !types.ID
50
-
51
- # Resolve function:
52
- def call(obj, args, ctx)
53
- end
54
- end
55
- RUBY
56
-
57
- assert_file "app/graphql/functions/finders/find_record.rb", expected_content
58
- end
59
- end