graphql 1.7.14 → 1.8.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +5 -5
  2. data/lib/generators/graphql/function_generator.rb +1 -1
  3. data/lib/generators/graphql/loader_generator.rb +1 -1
  4. data/lib/generators/graphql/mutation_generator.rb +1 -6
  5. data/lib/generators/graphql/templates/function.erb +2 -2
  6. data/lib/generators/graphql/templates/loader.erb +2 -2
  7. data/lib/graphql.rb +2 -0
  8. data/lib/graphql/argument.rb +0 -1
  9. data/lib/graphql/backwards_compatibility.rb +2 -3
  10. data/lib/graphql/base_type.rb +18 -16
  11. data/lib/graphql/compatibility/query_parser_specification.rb +0 -117
  12. data/lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb +0 -14
  13. data/lib/graphql/define/assign_object_field.rb +5 -12
  14. data/lib/graphql/deprecated_dsl.rb +28 -0
  15. data/lib/graphql/directive.rb +0 -1
  16. data/lib/graphql/enum_type.rb +1 -3
  17. data/lib/graphql/execution.rb +0 -1
  18. data/lib/graphql/execution/multiplex.rb +29 -12
  19. data/lib/graphql/field.rb +5 -20
  20. data/lib/graphql/function.rb +12 -0
  21. data/lib/graphql/input_object_type.rb +1 -3
  22. data/lib/graphql/internal_representation/node.rb +14 -26
  23. data/lib/graphql/internal_representation/visit.rb +6 -3
  24. data/lib/graphql/introspection/arguments_field.rb +0 -1
  25. data/lib/graphql/introspection/enum_values_field.rb +0 -1
  26. data/lib/graphql/introspection/fields_field.rb +0 -1
  27. data/lib/graphql/introspection/input_fields_field.rb +0 -1
  28. data/lib/graphql/introspection/interfaces_field.rb +0 -1
  29. data/lib/graphql/introspection/of_type_field.rb +0 -1
  30. data/lib/graphql/introspection/possible_types_field.rb +0 -1
  31. data/lib/graphql/introspection/schema_field.rb +0 -1
  32. data/lib/graphql/introspection/type_by_name_field.rb +0 -1
  33. data/lib/graphql/introspection/typename_field.rb +0 -1
  34. data/lib/graphql/language.rb +0 -3
  35. data/lib/graphql/language/generation.rb +182 -3
  36. data/lib/graphql/language/lexer.rb +69 -144
  37. data/lib/graphql/language/lexer.rl +4 -15
  38. data/lib/graphql/language/nodes.rb +76 -136
  39. data/lib/graphql/language/parser.rb +621 -668
  40. data/lib/graphql/language/parser.y +11 -17
  41. data/lib/graphql/language/token.rb +3 -10
  42. data/lib/graphql/object_type.rb +6 -1
  43. data/lib/graphql/query.rb +13 -8
  44. data/lib/graphql/query/arguments.rb +33 -48
  45. data/lib/graphql/query/context.rb +1 -0
  46. data/lib/graphql/query/literal_input.rb +1 -4
  47. data/lib/graphql/relay/connection_resolve.rb +3 -0
  48. data/lib/graphql/relay/global_id_resolve.rb +5 -1
  49. data/lib/graphql/relay/relation_connection.rb +19 -14
  50. data/lib/graphql/schema.rb +219 -12
  51. data/lib/graphql/schema/argument.rb +33 -0
  52. data/lib/graphql/schema/build_from_definition.rb +18 -64
  53. data/lib/graphql/schema/enum.rb +76 -0
  54. data/lib/graphql/schema/field.rb +127 -0
  55. data/lib/graphql/schema/field/dynamic_resolve.rb +63 -0
  56. data/lib/graphql/schema/field/unwrapped_resolve.rb +20 -0
  57. data/lib/graphql/schema/input_object.rb +61 -0
  58. data/lib/graphql/schema/interface.rb +32 -0
  59. data/lib/graphql/schema/loader.rb +2 -2
  60. data/lib/graphql/schema/member.rb +97 -0
  61. data/lib/graphql/schema/member/build_type.rb +106 -0
  62. data/lib/graphql/schema/member/has_fields.rb +56 -0
  63. data/lib/graphql/schema/member/instrumentation.rb +113 -0
  64. data/lib/graphql/schema/member/list_type_proxy.rb +21 -0
  65. data/lib/graphql/schema/member/non_null_type_proxy.rb +21 -0
  66. data/lib/graphql/schema/object.rb +65 -0
  67. data/lib/graphql/schema/printer.rb +266 -33
  68. data/lib/graphql/schema/scalar.rb +25 -0
  69. data/lib/graphql/schema/traversal.rb +26 -17
  70. data/lib/graphql/schema/union.rb +48 -0
  71. data/lib/graphql/static_validation/rules/fields_have_appropriate_selections.rb +1 -5
  72. data/lib/graphql/static_validation/rules/fields_will_merge.rb +8 -15
  73. data/lib/graphql/static_validation/rules/variables_are_used_and_defined.rb +1 -11
  74. data/lib/graphql/subscriptions/action_cable_subscriptions.rb +5 -7
  75. data/lib/graphql/tracing.rb +0 -1
  76. data/lib/graphql/tracing/platform_tracing.rb +7 -20
  77. data/lib/graphql/tracing/scout_tracing.rb +2 -2
  78. data/lib/graphql/unresolved_type_error.rb +2 -3
  79. data/lib/graphql/version.rb +1 -1
  80. data/readme.md +1 -1
  81. data/spec/dummy/app/channels/graphql_channel.rb +1 -22
  82. data/spec/dummy/log/development.log +0 -239
  83. data/spec/dummy/log/test.log +0 -204
  84. data/spec/dummy/test/system/action_cable_subscription_test.rb +0 -4
  85. data/spec/dummy/tmp/screenshots/failures_test_it_handles_subscriptions.png +0 -0
  86. data/spec/generators/graphql/function_generator_spec.rb +0 -26
  87. data/spec/generators/graphql/loader_generator_spec.rb +0 -24
  88. data/spec/graphql/analysis/max_query_complexity_spec.rb +3 -3
  89. data/spec/graphql/analysis/max_query_depth_spec.rb +3 -3
  90. data/spec/graphql/backtrace_spec.rb +0 -10
  91. data/spec/graphql/base_type_spec.rb +5 -19
  92. data/spec/graphql/boolean_type_spec.rb +3 -3
  93. data/spec/graphql/directive_spec.rb +1 -3
  94. data/spec/graphql/enum_type_spec.rb +5 -18
  95. data/spec/graphql/execution/execute_spec.rb +1 -1
  96. data/spec/graphql/execution/multiplex_spec.rb +2 -2
  97. data/spec/graphql/float_type_spec.rb +2 -2
  98. data/spec/graphql/id_type_spec.rb +1 -1
  99. data/spec/graphql/input_object_type_spec.rb +2 -15
  100. data/spec/graphql/int_type_spec.rb +2 -2
  101. data/spec/graphql/internal_representation/rewrite_spec.rb +2 -2
  102. data/spec/graphql/introspection/schema_type_spec.rb +0 -1
  103. data/spec/graphql/language/generation_spec.rb +186 -21
  104. data/spec/graphql/language/lexer_spec.rb +1 -21
  105. data/spec/graphql/language/nodes_spec.rb +12 -21
  106. data/spec/graphql/language/parser_spec.rb +1 -1
  107. data/spec/graphql/query/arguments_spec.rb +15 -37
  108. data/spec/graphql/query/serial_execution/value_resolution_spec.rb +2 -2
  109. data/spec/graphql/query/variables_spec.rb +1 -1
  110. data/spec/graphql/query_spec.rb +5 -31
  111. data/spec/graphql/rake_task_spec.rb +1 -3
  112. data/spec/graphql/relay/base_connection_spec.rb +1 -1
  113. data/spec/graphql/relay/connection_instrumentation_spec.rb +2 -2
  114. data/spec/graphql/relay/connection_resolve_spec.rb +1 -1
  115. data/spec/graphql/relay/connection_type_spec.rb +1 -1
  116. data/spec/graphql/relay/mutation_spec.rb +3 -3
  117. data/spec/graphql/relay/relation_connection_spec.rb +1 -65
  118. data/spec/graphql/schema/build_from_definition_spec.rb +4 -86
  119. data/spec/graphql/schema/enum_spec.rb +60 -0
  120. data/spec/graphql/schema/field_spec.rb +14 -0
  121. data/spec/graphql/schema/input_object_spec.rb +43 -0
  122. data/spec/graphql/schema/interface_spec.rb +98 -0
  123. data/spec/graphql/schema/object_spec.rb +119 -0
  124. data/spec/graphql/schema/printer_spec.rb +15 -92
  125. data/spec/graphql/schema/scalar_spec.rb +40 -0
  126. data/spec/graphql/schema/union_spec.rb +35 -0
  127. data/spec/graphql/schema/validation_spec.rb +1 -1
  128. data/spec/graphql/schema/warden_spec.rb +11 -11
  129. data/spec/graphql/schema_spec.rb +25 -23
  130. data/spec/graphql/static_validation/rules/fields_have_appropriate_selections_spec.rb +2 -10
  131. data/spec/graphql/static_validation/rules/fields_will_merge_spec.rb +2 -2
  132. data/spec/graphql/string_type_spec.rb +3 -3
  133. data/spec/graphql/subscriptions_spec.rb +1 -1
  134. data/spec/graphql/tracing/platform_tracing_spec.rb +1 -60
  135. data/spec/support/dummy/schema.rb +25 -39
  136. data/spec/support/jazz.rb +334 -0
  137. data/spec/support/lazy_helpers.rb +21 -23
  138. data/spec/support/star_wars/data.rb +7 -6
  139. data/spec/support/star_wars/schema.rb +109 -142
  140. metadata +39 -33
  141. data/lib/graphql/execution/instrumentation.rb +0 -82
  142. data/lib/graphql/language/block_string.rb +0 -47
  143. data/lib/graphql/language/document_from_schema_definition.rb +0 -277
  144. data/lib/graphql/language/printer.rb +0 -351
  145. data/lib/graphql/tracing/data_dog_tracing.rb +0 -49
  146. data/spec/graphql/execution/instrumentation_spec.rb +0 -165
  147. data/spec/graphql/language/block_string_spec.rb +0 -70
  148. data/spec/graphql/language/document_from_schema_definition_spec.rb +0 -770
  149. data/spec/graphql/language/printer_spec.rb +0 -203
@@ -88,7 +88,7 @@ describe GraphQL::Query::SerialExecution::ValueResolution do
88
88
  err = assert_raises(GraphQL::UnresolvedTypeError) { result }
89
89
  expected_message = "The value from \"resolvesToNilInterface\" on \"Query\" could not be resolved to \"SomeInterface\". " \
90
90
  "(Received: `nil`, Expected: [SomeObject]) " \
91
- "Make sure you have defined a `resolve_type` proc on your schema and that value `1337` " \
91
+ "Make sure you have defined a `type_from_object` proc on your schema and that value `1337` " \
92
92
  "gets resolved to a valid type."
93
93
  assert_equal expected_message, err.message
94
94
  end
@@ -105,7 +105,7 @@ describe GraphQL::Query::SerialExecution::ValueResolution do
105
105
  err = assert_raises(GraphQL::UnresolvedTypeError) { result }
106
106
  expected_message = "The value from \"resolvesToWrongTypeInterface\" on \"Query\" could not be resolved to \"SomeInterface\". " \
107
107
  "(Received: `OtherObject`, Expected: [SomeObject]) " \
108
- "Make sure you have defined a `resolve_type` proc on your schema and that value `:something` " \
108
+ "Make sure you have defined a `type_from_object` proc on your schema and that value `:something` " \
109
109
  "gets resolved to a valid type."
110
110
  assert_equal expected_message, err.message
111
111
  end
@@ -111,7 +111,7 @@ describe GraphQL::Query::Variables do
111
111
  let(:provided_variables) { { "ids" => [nil] } }
112
112
  it "returns an error" do
113
113
  assert_equal 1, result["errors"].length
114
- assert_nil result["data"]
114
+ assert_equal nil, result["data"]
115
115
  end
116
116
  end
117
117
  end
@@ -85,7 +85,7 @@ describe GraphQL::Query do
85
85
  operation_name: operation_name,
86
86
  max_depth: max_depth,
87
87
  )
88
- query.query_string = '{ __type(name: """Cheese""") { name } }'
88
+ query.query_string = '{ __type(name: "Cheese") { name } }'
89
89
  assert_equal "Cheese", query.result["data"] ["__type"]["name"]
90
90
  end
91
91
  end
@@ -111,7 +111,7 @@ describe GraphQL::Query do
111
111
  }
112
112
 
113
113
  it "returns nil" do
114
- assert_nil query.operation_name
114
+ assert_equal nil, query.operation_name
115
115
  end
116
116
  end
117
117
 
@@ -148,7 +148,7 @@ describe GraphQL::Query do
148
148
  }
149
149
 
150
150
  it "returns the inferred operation name" do
151
- assert_nil query.selected_operation_name
151
+ assert_equal nil, query.selected_operation_name
152
152
  end
153
153
  end
154
154
  end
@@ -253,32 +253,6 @@ describe GraphQL::Query do
253
253
  assert_equal [nil], Instrumenter::ERROR_LOG
254
254
  end
255
255
  end
256
-
257
- describe "when an error propagated through execution" do
258
- module ExtensionsInstrumenter
259
- LOG = []
260
- def self.before_query(q); end;
261
-
262
- def self.after_query(q)
263
- q.result["extensions"] = { "a" => 1 }
264
- LOG << :ok
265
- end
266
- end
267
-
268
- let(:schema) {
269
- Dummy::Schema.redefine {
270
- instrument(:query, ExtensionsInstrumenter)
271
- }
272
- }
273
-
274
- it "can add to extensions" do
275
- ExtensionsInstrumenter::LOG.clear
276
- assert_raises(RuntimeError) do
277
- schema.execute "{ error }"
278
- end
279
- assert_equal [:ok], ExtensionsInstrumenter::LOG
280
- end
281
- end
282
256
  end
283
257
 
284
258
  it "uses root_value as the object for the root type" do
@@ -558,7 +532,7 @@ describe GraphQL::Query do
558
532
 
559
533
  it "overrides the schema's max_depth" do
560
534
  assert result["data"].key?("cheese")
561
- assert_nil result["errors"]
535
+ assert_equal nil, result["errors"]
562
536
  end
563
537
  end
564
538
  end
@@ -725,7 +699,7 @@ describe GraphQL::Query do
725
699
 
726
700
  it "returns nil when there is no selected operation" do
727
701
  query = GraphQL::Query.new(schema, '# Only a comment')
728
- assert_nil query.irep_selection
702
+ assert_equal nil, query.irep_selection
729
703
  end
730
704
  end
731
705
 
@@ -34,9 +34,7 @@ describe GraphQL::RakeTask do
34
34
  end
35
35
  dumped_json = File.read("./schema.json")
36
36
  expected_json = JSON.pretty_generate(RakeTaskSchema.execute(GraphQL::Introspection::INTROSPECTION_QUERY))
37
-
38
- # Test that that JSON is logically equivalent, not serialized the same
39
- assert_equal(JSON.parse(expected_json), JSON.parse(dumped_json))
37
+ assert_equal(expected_json, dumped_json)
40
38
 
41
39
  dumped_idl = File.read("./schema.graphql")
42
40
  expected_idl = rake_task_schema_defn.chomp
@@ -38,7 +38,7 @@ describe GraphQL::Relay::BaseConnection do
38
38
  first: nil,
39
39
  }
40
40
  conn = GraphQL::Relay::BaseConnection.new([], args, context: context)
41
- assert_nil conn.first
41
+ assert_equal nil, conn.first
42
42
  end
43
43
  end
44
44
 
@@ -12,7 +12,7 @@ describe GraphQL::Relay::ConnectionInstrumentation do
12
12
  end
13
13
 
14
14
  it "keeps a reference to the function" do
15
- conn_field = StarWars::Faction.fields["shipsWithMaxPageSize"]
15
+ conn_field = StarWars::Faction.graphql_definition.fields["shipsWithMaxPageSize"]
16
16
  assert_instance_of StarWars::ShipsWithMaxPageSize, conn_field.function
17
17
  end
18
18
 
@@ -77,7 +77,7 @@ describe GraphQL::Relay::ConnectionInstrumentation do
77
77
  # Before the object is wrapped in a connection, the instrumentation sees `Array`
78
78
  assert_equal ["StarWars::FactionRecord", "Array", "GraphQL::Relay::ArrayConnection"], ctx[:before_built_ins]
79
79
  # After the object is wrapped in a connection, it sees the connection object
80
- assert_equal ["StarWars::FactionRecord", "GraphQL::Relay::ArrayConnection", "GraphQL::Relay::ArrayConnection"], ctx[:after_built_ins]
80
+ assert_equal ["StarWars::Faction", "GraphQL::Relay::ArrayConnection", "GraphQL::Relay::ArrayConnection"], ctx[:after_built_ins]
81
81
  end
82
82
  end
83
83
  end
@@ -57,7 +57,7 @@ describe GraphQL::Relay::ConnectionResolve do
57
57
  it "becomes null" do
58
58
  result = star_wars_query(query_string, { "name" => "null" })
59
59
  conn = result["data"]["rebels"]["ships"]
60
- assert_nil conn
60
+ assert_equal nil, conn
61
61
  end
62
62
  end
63
63
  end
@@ -73,7 +73,7 @@ describe GraphQL::Relay::ConnectionType do
73
73
 
74
74
  it "nullifies the parent and adds an error" do
75
75
  result = star_wars_query(query_string)
76
- assert_nil result["data"]["basesWithNullName"]["edges"][0]["node"]
76
+ assert_equal nil, result["data"]["basesWithNullName"]["edges"][0]["node"]
77
77
  assert_equal "Boom!", result["errors"][0]["message"]
78
78
  end
79
79
  end
@@ -193,17 +193,17 @@ describe GraphQL::Relay::Mutation do
193
193
  end
194
194
 
195
195
  it "doesn't get a mutation in the metadata" do
196
- assert_nil custom_return_type.mutation
196
+ assert_equal nil, custom_return_type.mutation
197
197
  end
198
198
 
199
199
  it "supports input fields with nil default value" do
200
200
  assert input.arguments['nullDefault'].default_value?
201
- assert_nil input.arguments['nullDefault'].default_value
201
+ assert_equal nil, input.arguments['nullDefault'].default_value
202
202
  end
203
203
 
204
204
  it "supports input fields with no default value" do
205
205
  assert !input.arguments['noDefault'].default_value?
206
- assert_nil input.arguments['noDefault'].default_value
206
+ assert_equal nil, input.arguments['noDefault'].default_value
207
207
  end
208
208
 
209
209
  it "supports input fields with non-nil default value" do
@@ -74,35 +74,6 @@ describe GraphQL::Relay::RelationConnection do
74
74
  )
75
75
  end
76
76
 
77
- it "makes one sql query for items and another for count" do
78
- query_str = <<-GRAPHQL
79
- {
80
- empire {
81
- bases(first: 2) {
82
- totalCount
83
- edges {
84
- cursor
85
- node {
86
- name
87
- }
88
- }
89
- }
90
- }
91
- }
92
- GRAPHQL
93
- io = StringIO.new
94
- begin
95
- prev_logger = ActiveRecord::Base.logger
96
- ActiveRecord::Base.logger = Logger.new(io)
97
- result = star_wars_query(query_str, "first" => 2)
98
- ensure
99
- ActiveRecord::Base.logger = prev_logger
100
- end
101
- assert_equal 2, io.string.scan("\n").count, "Two log entries"
102
- assert_equal 3, result["data"]["empire"]["bases"]["totalCount"]
103
- assert_equal 2, result["data"]["empire"]["bases"]["edges"].size
104
- end
105
-
106
77
  it "provides bidirectional_pagination" do
107
78
  result = star_wars_query(query_string, "first" => 1)
108
79
  last_cursor = get_last_cursor(result)
@@ -117,13 +88,6 @@ describe GraphQL::Relay::RelationConnection do
117
88
  assert_equal true, get_page_info(result)["hasNextPage"]
118
89
  assert_equal true, get_page_info(result)["hasPreviousPage"]
119
90
 
120
- last_cursor = get_last_cursor(result)
121
- result = with_bidirectional_pagination {
122
- star_wars_query(query_string, "last" => 1, "before" => last_cursor)
123
- }
124
- assert_equal true, get_page_info(result)["hasNextPage"]
125
- assert_equal false, get_page_info(result)["hasPreviousPage"]
126
-
127
91
  result = star_wars_query(query_string, "first" => 100)
128
92
  last_cursor = get_last_cursor(result)
129
93
 
@@ -136,6 +100,7 @@ describe GraphQL::Relay::RelationConnection do
136
100
  }
137
101
  assert_equal true, get_page_info(result)["hasNextPage"]
138
102
  assert_equal true, get_page_info(result)["hasPreviousPage"]
103
+
139
104
  end
140
105
 
141
106
  it 'slices the result' do
@@ -599,35 +564,6 @@ describe GraphQL::Relay::RelationConnection do
599
564
  result = star_wars_query(query_string, "last" => 1, "nameIncludes" => "ea", "before" => before)
600
565
  assert_equal(["Death Star"], get_names(result))
601
566
  end
602
-
603
- it "makes one sql query for items and another for count" do
604
- query_str = <<-GRAPHQL
605
- {
606
- empire {
607
- basesAsSequelDataset(first: 2) {
608
- totalCount
609
- edges {
610
- cursor
611
- node {
612
- name
613
- }
614
- }
615
- }
616
- }
617
- }
618
- GRAPHQL
619
- result = nil
620
- io = StringIO.new
621
- begin
622
- StarWars::DB.loggers << Logger.new(io)
623
- result = star_wars_query(query_str, "first" => 2)
624
- ensure
625
- StarWars::DB.loggers.pop
626
- end
627
- assert_equal 2, io.string.scan("SELECT").count
628
- assert_equal 3, result["data"]["empire"]["basesAsSequelDataset"]["totalCount"]
629
- assert_equal 2, result["data"]["empire"]["basesAsSequelDataset"]["edges"].size
630
- end
631
567
  end
632
568
  end
633
569
 
@@ -30,20 +30,6 @@ type HelloScalars {
30
30
  build_schema_and_compare_output(schema.chop)
31
31
  end
32
32
 
33
- it 'can build a schema with default input object values' do
34
- schema = <<-SCHEMA
35
- input InputObject {
36
- a: Int
37
- }
38
-
39
- type Query {
40
- a(input: InputObject = {a: 1}): String
41
- }
42
- SCHEMA
43
-
44
- build_schema_and_compare_output(schema.chop)
45
- end
46
-
47
33
  it 'can build a schema with directives' do
48
34
  schema = <<-SCHEMA
49
35
  schema {
@@ -308,7 +294,7 @@ schema {
308
294
  }
309
295
 
310
296
  type Hello {
311
- str(bool: Boolean, int: Int): String
297
+ str(int: Int, bool: Boolean): String
312
298
  }
313
299
  SCHEMA
314
300
 
@@ -505,7 +491,7 @@ type HelloScalars {
505
491
  }
506
492
 
507
493
  type Mutation {
508
- addHelloScalars(bool: Boolean, int: Int, str: String): HelloScalars
494
+ addHelloScalars(str: String, int: Int, bool: Boolean): HelloScalars
509
495
  }
510
496
  SCHEMA
511
497
 
@@ -520,7 +506,7 @@ enum Color {
520
506
  }
521
507
 
522
508
  type Mutation {
523
- hello(color: Color = RED, int: Int, nullDefault: Int = null, str: String): String
509
+ hello(str: String, int: Int, color: Color = RED, nullDefault: Int = null): String
524
510
  }
525
511
 
526
512
  type Query {
@@ -545,7 +531,7 @@ type HelloScalars {
545
531
  }
546
532
 
547
533
  type Subscription {
548
- subscribeHelloScalars(bool: Boolean, int: Int, str: String): HelloScalars
534
+ subscribeHelloScalars(str: String, int: Int, bool: Boolean): HelloScalars
549
535
  }
550
536
  SCHEMA
551
537
 
@@ -603,74 +589,6 @@ type Query {
603
589
 
604
590
  build_schema_and_compare_output(schema.chop)
605
591
  end
606
-
607
- it 'supports empty types' do
608
- schema = <<-SCHEMA
609
- type Query {
610
- }
611
- SCHEMA
612
-
613
- build_schema_and_compare_output(schema.chop)
614
- end
615
-
616
- it "tracks original AST node" do
617
- schema_definition = <<-GRAPHQL
618
- schema {
619
- query: Query
620
- }
621
-
622
- enum Enum {
623
- VALUE
624
- }
625
-
626
- type Query {
627
- field(argument: String): String
628
- deprecatedField(argument: String): String @deprecated(reason: "Test")
629
- }
630
-
631
- interface Interface {
632
- field(argument: String): String
633
- }
634
-
635
- union Union = Query
636
-
637
- scalar Scalar
638
-
639
- input Input {
640
- argument: String
641
- }
642
-
643
- directive @Directive (
644
- # Argument
645
- argument: String
646
- ) on SCHEMA
647
-
648
- type Type implements Interface {
649
- field(argument: String): String
650
- }
651
- GRAPHQL
652
-
653
- schema = GraphQL::Schema.from_definition(schema_definition)
654
-
655
- assert_equal [1, 1], schema.ast_node.position
656
- assert_equal [5, 1], schema.types["Enum"].ast_node.position
657
- assert_equal [6, 3], schema.types["Enum"].values["VALUE"].ast_node.position
658
- assert_equal [9, 1], schema.types["Query"].ast_node.position
659
- assert_equal [10, 3], schema.types["Query"].fields["field"].ast_node.position
660
- assert_equal [10, 9], schema.types["Query"].fields["field"].arguments["argument"].ast_node.position
661
- assert_equal [11, 45], schema.types["Query"].fields["deprecatedField"].ast_node.directives[0].position
662
- assert_equal [11, 57], schema.types["Query"].fields["deprecatedField"].ast_node.directives[0].arguments[0].position
663
- assert_equal [14, 1], schema.types["Interface"].ast_node.position
664
- assert_equal [15, 3], schema.types["Interface"].fields["field"].ast_node.position
665
- assert_equal [15, 9], schema.types["Interface"].fields["field"].arguments["argument"].ast_node.position
666
- assert_equal [18, 1], schema.types["Union"].ast_node.position
667
- assert_equal [20, 1], schema.types["Scalar"].ast_node.position
668
- assert_equal [22, 1], schema.types["Input"].ast_node.position
669
- assert_equal [22, 1], schema.types["Input"].arguments["argument"].ast_node.position
670
- assert_equal [26, 1], schema.directives["Directive"].ast_node.position
671
- assert_equal [28, 3], schema.directives["Directive"].arguments["argument"].ast_node.position
672
- assert_equal [31, 22], schema.types["Type"].ast_node.interfaces[0].position
673
- end
674
592
  end
675
593
 
676
594
  describe 'Failures' do
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+ require "spec_helper"
3
+
4
+ describe GraphQL::Schema::Enum do
5
+ let(:enum) { Jazz::Family }
6
+ describe "type info" do
7
+ it "tells about the definition" do
8
+ assert_equal "Family", enum.graphql_name
9
+ assert_equal 29, enum.description.length
10
+ assert_equal 6, enum.values.size
11
+ end
12
+
13
+ it "inherits values and description" do
14
+ new_enum = Class.new(enum) do
15
+ value :Nonsense
16
+ value :PERCUSSION, "new description"
17
+ end
18
+
19
+ # Description was inherited
20
+ assert_equal 29, new_enum.description.length
21
+ # values were inherited without modifying the parent
22
+ assert_equal 6, enum.values.size
23
+ assert_equal 7, new_enum.values.size
24
+ perc_value = new_enum.values.find { |v| v.name == "PERCUSSION" }
25
+ assert_equal "new description", perc_value.description
26
+ end
27
+ end
28
+
29
+
30
+ describe ".to_graphql" do
31
+ it "creates an EnumType" do
32
+ enum_type = enum.to_graphql
33
+ assert_equal "Family", enum_type.name
34
+ assert_equal "Groups of musical instruments", enum_type.description
35
+
36
+ string_val = enum_type.values["STRING"]
37
+ didg_val = enum_type.values["DIDGERIDOO"]
38
+ assert_equal "STRING", string_val.name
39
+ assert_equal :str, string_val.value
40
+ assert_equal "DIDGERIDOO", didg_val.name
41
+ assert_equal "Merged into BRASS", didg_val.deprecation_reason
42
+ end
43
+ end
44
+
45
+ describe "in queries" do
46
+ it "works as return values" do
47
+ query_str = "{ instruments { family } }"
48
+ expected_families = ["STRING", "WOODWIND", "BRASS", "KEYS", "KEYS", "PERCUSSION"]
49
+ result = Jazz::Schema.execute(query_str)
50
+ assert_equal expected_families, result["data"]["instruments"].map { |i| i["family"] }
51
+ end
52
+
53
+ it "works as input" do
54
+ query_str = "query($family: Family!) { instruments(family: $family) { name } }"
55
+ expected_names = ["Piano", "Organ"]
56
+ result = Jazz::Schema.execute(query_str, variables: { "family" => "KEYS" })
57
+ assert_equal expected_names, result["data"]["instruments"].map { |i| i["name"] }
58
+ end
59
+ end
60
+ end