graphql 1.8.0.pre7 → 1.8.0.pre8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/lib/graphql/argument.rb +24 -19
  3. data/lib/graphql/backtrace/tracer.rb +16 -22
  4. data/lib/graphql/base_type.rb +6 -1
  5. data/lib/graphql/execution/execute.rb +15 -13
  6. data/lib/graphql/execution/lazy/resolve.rb +1 -3
  7. data/lib/graphql/interface_type.rb +5 -3
  8. data/lib/graphql/language/document_from_schema_definition.rb +1 -1
  9. data/lib/graphql/language/lexer.rb +65 -51
  10. data/lib/graphql/language/lexer.rl +2 -0
  11. data/lib/graphql/language/nodes.rb +118 -71
  12. data/lib/graphql/language/parser.rb +699 -652
  13. data/lib/graphql/language/parser.y +11 -5
  14. data/lib/graphql/language/printer.rb +2 -2
  15. data/lib/graphql/object_type.rb +0 -5
  16. data/lib/graphql/relay/relation_connection.rb +1 -1
  17. data/lib/graphql/schema.rb +15 -3
  18. data/lib/graphql/schema/argument.rb +18 -1
  19. data/lib/graphql/schema/enum.rb +5 -2
  20. data/lib/graphql/schema/enum_value.rb +8 -1
  21. data/lib/graphql/schema/field.rb +10 -3
  22. data/lib/graphql/schema/input_object.rb +4 -2
  23. data/lib/graphql/schema/interface.rb +15 -0
  24. data/lib/graphql/schema/member.rb +2 -1
  25. data/lib/graphql/schema/member/accepts_definition.rb +118 -0
  26. data/lib/graphql/schema/member/build_type.rb +2 -2
  27. data/lib/graphql/schema/member/has_fields.rb +3 -2
  28. data/lib/graphql/schema/object.rb +4 -2
  29. data/lib/graphql/schema/scalar.rb +2 -0
  30. data/lib/graphql/schema/traversal.rb +3 -0
  31. data/lib/graphql/schema/union.rb +6 -11
  32. data/lib/graphql/version.rb +1 -1
  33. data/spec/graphql/argument_spec.rb +21 -0
  34. data/spec/graphql/base_type_spec.rb +22 -0
  35. data/spec/graphql/enum_type_spec.rb +18 -5
  36. data/spec/graphql/execution/execute_spec.rb +3 -3
  37. data/spec/graphql/input_object_type_spec.rb +13 -0
  38. data/spec/graphql/interface_type_spec.rb +12 -0
  39. data/spec/graphql/language/nodes_spec.rb +0 -12
  40. data/spec/graphql/language/parser_spec.rb +74 -0
  41. data/spec/graphql/language/printer_spec.rb +1 -1
  42. data/spec/graphql/object_type_spec.rb +21 -0
  43. data/spec/graphql/relay/range_add_spec.rb +5 -1
  44. data/spec/graphql/relay/relation_connection_spec.rb +7 -1
  45. data/spec/graphql/schema/argument_spec.rb +31 -0
  46. data/spec/graphql/schema/enum_spec.rb +5 -0
  47. data/spec/graphql/schema/field_spec.rb +11 -1
  48. data/spec/graphql/schema/input_object_spec.rb +5 -0
  49. data/spec/graphql/schema/interface_spec.rb +29 -0
  50. data/spec/graphql/schema/member/accepts_definition_spec.rb +62 -0
  51. data/spec/graphql/schema/printer_spec.rb +34 -0
  52. data/spec/graphql/schema/traversal_spec.rb +31 -0
  53. data/spec/graphql/schema/union_spec.rb +30 -0
  54. data/spec/graphql/schema_spec.rb +6 -0
  55. data/spec/graphql/static_validation/rules/fields_will_merge_spec.rb +2 -2
  56. data/spec/graphql/tracing/active_support_notifications_tracing_spec.rb +1 -1
  57. data/spec/graphql/union_type_spec.rb +1 -1
  58. data/spec/spec_helper.rb +1 -0
  59. data/spec/support/dummy/schema.rb +1 -4
  60. metadata +7 -2
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+ require "spec_helper"
3
+
4
+ describe GraphQL::Schema::Argument do
5
+ class SchemaArgumentTest < GraphQL::Schema::Object
6
+ field :field, String, null: false do
7
+ argument :arg, String, description: "test", required: false
8
+
9
+ argument :argWithBlock, String, required: false do
10
+ description "test"
11
+ end
12
+ end
13
+ end
14
+
15
+ describe "graphql definition" do
16
+ it "calls block" do
17
+ assert_equal "test", SchemaArgumentTest.fields["field"].arguments["argWithBlock"].description
18
+ end
19
+ end
20
+
21
+ describe "#description" do
22
+ it "sets description" do
23
+ SchemaArgumentTest.fields["field"].arguments["arg"].description "new description"
24
+ assert_equal "new description", SchemaArgumentTest.fields["field"].arguments["arg"].description
25
+ end
26
+
27
+ it "returns description" do
28
+ assert_equal "test", SchemaArgumentTest.fields["field"].arguments["argWithBlock"].description
29
+ end
30
+ end
31
+ end
@@ -28,6 +28,11 @@ describe GraphQL::Schema::Enum do
28
28
  it "accepts a block" do
29
29
  assert_equal "Neither here nor there, really", enum.values["KEYS"].description
30
30
  end
31
+
32
+ it "is the #owner of its values" do
33
+ value = enum.values["STRING"]
34
+ assert_equal enum, value.owner
35
+ end
31
36
  end
32
37
 
33
38
  it "uses a custom enum value class" do
@@ -14,7 +14,7 @@ describe GraphQL::Schema::Field do
14
14
  it "camelizes the field name, unless camelize: false" do
15
15
  assert_equal 'inspectInput', field.graphql_definition.name
16
16
 
17
- underscored_field = GraphQL::Schema::Field.new(:underscored_field, String, null: false, camelize: false) do
17
+ underscored_field = GraphQL::Schema::Field.new(:underscored_field, String, null: false, camelize: false, owner: nil) do
18
18
  argument :underscored_arg, String, required: true, camelize: false
19
19
  end
20
20
 
@@ -66,6 +66,16 @@ describe GraphQL::Schema::Field do
66
66
  end
67
67
  end
68
68
 
69
+ it "is the #owner of its arguments" do
70
+ field = Jazz::Query.fields["find"]
71
+ argument = field.arguments["id"]
72
+ assert_equal field, argument.owner
73
+ end
74
+
75
+ it "has a reference to the object that owns it with #owner" do
76
+ assert_equal Jazz::Query, field.owner
77
+ end
78
+
69
79
  describe "complexity" do
70
80
  it "accepts a keyword argument" do
71
81
  object = Class.new(Jazz::BaseObject) do
@@ -9,6 +9,11 @@ describe GraphQL::Schema::InputObject do
9
9
  assert_equal nil, input_object.description
10
10
  assert_equal 1, input_object.arguments.size
11
11
  end
12
+
13
+ it "is the #owner of its arguments" do
14
+ argument = input_object.arguments["name"]
15
+ assert_equal input_object, argument.owner
16
+ end
12
17
  end
13
18
 
14
19
  describe ".to_graphql" do
@@ -49,6 +49,35 @@ describe GraphQL::Schema::Interface do
49
49
  assert_equal "id", field.name
50
50
  assert_equal GraphQL::ID_TYPE.to_non_null_type, field.type
51
51
  assert_equal "A unique identifier for this object", field.description
52
+ assert_nil interface_type.resolve_type_proc
53
+ assert_empty interface_type.orphan_types
54
+ end
55
+
56
+ it "can specify a resolve_type method" do
57
+ interface = Class.new(GraphQL::Schema::Interface) do
58
+ def self.resolve_type(_object, _context)
59
+ "MyType"
60
+ end
61
+
62
+ def self.name
63
+ "MyInterface"
64
+ end
65
+ end
66
+ interface_type = interface.to_graphql
67
+ assert_equal "MyType", interface_type.resolve_type_proc.call(nil, nil)
68
+ end
69
+
70
+ it "can specify orphan types" do
71
+ interface = Class.new(GraphQL::Schema::Interface) do
72
+ def self.name
73
+ "MyInterface"
74
+ end
75
+
76
+ orphan_types Dummy::CheeseType, Dummy::HoneyType
77
+ end
78
+
79
+ interface_type = interface.to_graphql
80
+ assert_equal [Dummy::CheeseType, Dummy::HoneyType], interface_type.orphan_types
52
81
  end
53
82
  end
54
83
 
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+ require "spec_helper"
3
+
4
+ describe GraphQL::Schema::Member::AcceptsDefinition do
5
+ class AcceptsDefinitionSchema < GraphQL::Schema
6
+ accepts_definition :set_metadata
7
+ set_metadata :a, 999
8
+
9
+ class Option < GraphQL::Schema::Enum
10
+ class EnumValue < GraphQL::Schema::EnumValue
11
+ accepts_definition :metadata
12
+ end
13
+ enum_value_class EnumValue
14
+ accepts_definition :metadata
15
+ metadata :a, 123
16
+ value "A", metadata: [:a, 456]
17
+ value "B"
18
+ end
19
+
20
+ class Query < GraphQL::Schema::Object
21
+ class Field < GraphQL::Schema::Field
22
+ class Argument < GraphQL::Schema::Argument
23
+ accepts_definition :metadata
24
+ end
25
+ argument_class Argument
26
+ accepts_definition :metadata
27
+ end
28
+ field_class Field
29
+ accepts_definition :metadata
30
+ metadata :a, :abc
31
+
32
+ field :option, Option, null: false do
33
+ metadata :a, :def
34
+ argument :value, Integer, required: true, metadata: [:a, :ghi]
35
+ end
36
+ end
37
+
38
+ query(Query)
39
+ end
40
+
41
+
42
+ it "passes along configs for types" do
43
+ assert_equal [:a, 123], AcceptsDefinitionSchema::Option.metadata
44
+ assert_equal 123, AcceptsDefinitionSchema::Option.graphql_definition.metadata[:a]
45
+ assert_equal [:a, :abc], AcceptsDefinitionSchema::Query.metadata
46
+ assert_equal :abc, AcceptsDefinitionSchema::Query.graphql_definition.metadata[:a]
47
+ end
48
+
49
+ it "passes along configs for fields and arguments" do
50
+ assert_equal :def, AcceptsDefinitionSchema.find("Query.option").metadata[:a]
51
+ assert_equal :ghi, AcceptsDefinitionSchema.find("Query.option.value").metadata[:a]
52
+ end
53
+
54
+ it "passes along configs for enum values" do
55
+ assert_equal 456, AcceptsDefinitionSchema.find("Option.A").metadata[:a]
56
+ assert_nil AcceptsDefinitionSchema.find("Option.B").metadata[:a]
57
+ end
58
+
59
+ it "passes along configs for schemas" do
60
+ assert_equal 999, AcceptsDefinitionSchema.graphql_definition.metadata[:a]
61
+ end
62
+ end
@@ -629,6 +629,40 @@ type Post {
629
629
  SCHEMA
630
630
  assert_equal expected.chomp, GraphQL::Schema::Printer.new(schema).print_type(schema.types['Post'])
631
631
  end
632
+
633
+ it "can print arguments that use non-standard Ruby objects as default values" do
634
+ backing_object = Struct.new(:value)
635
+
636
+ scalar_type = GraphQL::ScalarType.define do
637
+ name "SomeType"
638
+ coerce_input ->(value, ctx) { backing_object.new(value) }
639
+ coerce_result ->(obj, ctx) { obj.value }
640
+ end
641
+
642
+ query_root = GraphQL::ObjectType.define do
643
+ name "Query"
644
+ description "The query root of this schema"
645
+
646
+ field :example do
647
+ type scalar_type
648
+ argument :input, scalar_type, default_value: backing_object.new("Howdy")
649
+ resolve ->(obj, args, ctx) { args[:input] }
650
+ end
651
+ end
652
+
653
+ schema = GraphQL::Schema.define do
654
+ query query_root
655
+ end
656
+
657
+ expected = <<SCHEMA
658
+ # The query root of this schema
659
+ type Query {
660
+ example(input: SomeType = "Howdy"): SomeType
661
+ }
662
+ SCHEMA
663
+
664
+ assert_equal expected.chomp, GraphQL::Schema::Printer.new(schema).print_type(query_root)
665
+ end
632
666
  end
633
667
 
634
668
  describe "#print_directive" do
@@ -188,4 +188,35 @@ describe GraphQL::Schema::Traversal do
188
188
  }
189
189
  assert_equal expected, result
190
190
  end
191
+
192
+ it "finds orphan types from interfaces" do
193
+ b_type = GraphQL::ObjectType.define do
194
+ name "B"
195
+ end
196
+
197
+ c_type = GraphQL::ObjectType.define do
198
+ name "C"
199
+ end
200
+
201
+ interface = GraphQL::InterfaceType.define do
202
+ name "AInterface"
203
+ orphan_types [b_type]
204
+ end
205
+
206
+ another_interface = GraphQL::InterfaceType.define do
207
+ name "AnotherIterface"
208
+ orphan_types [b_type, c_type]
209
+ end
210
+
211
+ result = traversal([interface, another_interface]).type_map
212
+ expected = {
213
+ "Boolean" => GraphQL::BOOLEAN_TYPE,
214
+ "String" => GraphQL::STRING_TYPE,
215
+ "AInterface" => interface,
216
+ "AnotherIterface" => another_interface,
217
+ "B" => b_type,
218
+ "C" => c_type
219
+ }
220
+ assert_equal expected, result
221
+ end
191
222
  end
@@ -9,6 +9,36 @@ describe GraphQL::Schema::Union do
9
9
  end
10
10
  end
11
11
 
12
+ describe ".to_graphql" do
13
+ it "creates a UnionType" do
14
+ union = Class.new(GraphQL::Schema::Union) do
15
+ possible_types Jazz::Musician, Jazz::Ensemble
16
+
17
+ def self.name
18
+ "MyUnion"
19
+ end
20
+ end
21
+ union_type = union.to_graphql
22
+ assert_equal "MyUnion", union_type.name
23
+ assert_equal [Jazz::Musician.to_graphql, Jazz::Ensemble.to_graphql], union_type.possible_types
24
+ assert_nil union_type.resolve_type_proc
25
+ end
26
+
27
+ it "can specify a resolve_type method" do
28
+ union = Class.new(GraphQL::Schema::Union) do
29
+ def self.resolve_type(_object, _context)
30
+ "MyType"
31
+ end
32
+
33
+ def self.name
34
+ "MyUnion"
35
+ end
36
+ end
37
+ union_type = union.to_graphql
38
+ assert_equal "MyType", union_type.resolve_type_proc.call(nil, nil)
39
+ end
40
+ end
41
+
12
42
  describe "in queries" do
13
43
  it "works" do
14
44
  query_str = <<-GRAPHQL
@@ -6,6 +6,12 @@ describe GraphQL::Schema do
6
6
  let(:relay_schema) { StarWars::Schema }
7
7
  let(:empty_schema) { GraphQL::Schema.define }
8
8
 
9
+ describe "#graphql_definition" do
10
+ it "returns itself" do
11
+ assert_equal empty_schema, empty_schema.graphql_definition
12
+ end
13
+ end
14
+
9
15
  describe "#rescue_from" do
10
16
  it "adds handlers to the rescue middleware" do
11
17
  schema_defn = schema.graphql_definition
@@ -515,7 +515,7 @@ describe GraphQL::StaticValidation::FieldsWillMerge do
515
515
  scalar: String!
516
516
  }
517
517
 
518
- type NonNullStringBox1Impl implements SomeBox, NonNullStringBox1 {
518
+ type NonNullStringBox1Impl implements SomeBox & NonNullStringBox1 {
519
519
  scalar: String!
520
520
  unrelatedField: String
521
521
  deepBox: SomeBox
@@ -525,7 +525,7 @@ describe GraphQL::StaticValidation::FieldsWillMerge do
525
525
  scalar: String!
526
526
  }
527
527
 
528
- type NonNullStringBox2Impl implements SomeBox, NonNullStringBox2 {
528
+ type NonNullStringBox2Impl implements SomeBox & NonNullStringBox2 {
529
529
  scalar: String!
530
530
  unrelatedField: String
531
531
  deepBox: SomeBox
@@ -41,10 +41,10 @@ describe GraphQL::Tracing::ActiveSupportNotificationsTracing do
41
41
  "graphql.execute_field",
42
42
  "graphql.execute_query",
43
43
  "graphql.lazy_loader",
44
- "graphql.execute_field",
45
44
  "graphql.execute_field_lazy",
46
45
  "graphql.execute_field",
47
46
  "graphql.execute_field_lazy",
47
+ "graphql.execute_field",
48
48
  "graphql.execute_field_lazy",
49
49
  "graphql.execute_field_lazy",
50
50
  "graphql.execute_query_lazy",
@@ -150,7 +150,7 @@ describe GraphQL::UnionType do
150
150
  end
151
151
 
152
152
  describe "#dup" do
153
- it "copies possible types without affecting the orginal" do
153
+ it "copies possible types without affecting the original" do
154
154
  union.possible_types # load the internal cache
155
155
  union_2 = union.dup
156
156
  union_2.possible_types << type_3
@@ -38,6 +38,7 @@ Minitest.backtrace_filter = Minitest::BacktraceFilter.new
38
38
  # This is for convenient access to metadata in test definitions
39
39
  assign_metadata_key = ->(target, key, value) { target.metadata[key] = value }
40
40
  assign_metadata_flag = ->(target, flag) { target.metadata[flag] = true }
41
+ GraphQL::Schema.accepts_definitions(set_metadata: assign_metadata_key)
41
42
  GraphQL::BaseType.accepts_definitions(metadata: assign_metadata_key)
42
43
  GraphQL::Field.accepts_definitions(metadata: assign_metadata_key)
43
44
  GraphQL::Argument.accepts_definitions(metadata: assign_metadata_key)
@@ -223,10 +223,7 @@ module Dummy
223
223
  description "Where it came from"
224
224
  end
225
225
 
226
- input_field :originDairy, types.String, "Dairy which produced it", default_value: "Sugar Hollow Dairy" do
227
- description "Ignored because arg takes precedence"
228
- default_value "Ignored because keyword arg takes precedence"
229
- end
226
+ input_field :originDairy, types.String, "Dairy which produced it", default_value: "Sugar Hollow Dairy"
230
227
 
231
228
  input_field :fatContent, types.Float, "How much fat it has" do
232
229
  # ensure we can define default in block
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.0.pre7
4
+ version: 1.8.0.pre8
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-02-27 00:00:00.000000000 Z
11
+ date: 2018-03-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: benchmark-ips
@@ -523,6 +523,7 @@ files:
523
523
  - lib/graphql/schema/late_bound_type.rb
524
524
  - lib/graphql/schema/loader.rb
525
525
  - lib/graphql/schema/member.rb
526
+ - lib/graphql/schema/member/accepts_definition.rb
526
527
  - lib/graphql/schema/member/build_type.rb
527
528
  - lib/graphql/schema/member/has_fields.rb
528
529
  - lib/graphql/schema/member/instrumentation.rb
@@ -999,6 +1000,7 @@ files:
999
1000
  - spec/graphql/relay/range_add_spec.rb
1000
1001
  - spec/graphql/relay/relation_connection_spec.rb
1001
1002
  - spec/graphql/scalar_type_spec.rb
1003
+ - spec/graphql/schema/argument_spec.rb
1002
1004
  - spec/graphql/schema/build_from_definition_spec.rb
1003
1005
  - spec/graphql/schema/catchall_middleware_spec.rb
1004
1006
  - spec/graphql/schema/enum_spec.rb
@@ -1009,6 +1011,7 @@ files:
1009
1011
  - spec/graphql/schema/interface_spec.rb
1010
1012
  - spec/graphql/schema/introspection_system_spec.rb
1011
1013
  - spec/graphql/schema/loader_spec.rb
1014
+ - spec/graphql/schema/member/accepts_definition_spec.rb
1012
1015
  - spec/graphql/schema/middleware_chain_spec.rb
1013
1016
  - spec/graphql/schema/object_spec.rb
1014
1017
  - spec/graphql/schema/printer_spec.rb
@@ -1500,6 +1503,7 @@ test_files:
1500
1503
  - spec/graphql/relay/range_add_spec.rb
1501
1504
  - spec/graphql/relay/relation_connection_spec.rb
1502
1505
  - spec/graphql/scalar_type_spec.rb
1506
+ - spec/graphql/schema/argument_spec.rb
1503
1507
  - spec/graphql/schema/build_from_definition_spec.rb
1504
1508
  - spec/graphql/schema/catchall_middleware_spec.rb
1505
1509
  - spec/graphql/schema/enum_spec.rb
@@ -1510,6 +1514,7 @@ test_files:
1510
1514
  - spec/graphql/schema/interface_spec.rb
1511
1515
  - spec/graphql/schema/introspection_system_spec.rb
1512
1516
  - spec/graphql/schema/loader_spec.rb
1517
+ - spec/graphql/schema/member/accepts_definition_spec.rb
1513
1518
  - spec/graphql/schema/middleware_chain_spec.rb
1514
1519
  - spec/graphql/schema/object_spec.rb
1515
1520
  - spec/graphql/schema/printer_spec.rb