File: loader_spec.rb

package info (click to toggle)
ruby-graphql 2.2.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,584 kB
  • sloc: ruby: 67,505; ansic: 1,753; yacc: 831; javascript: 331; makefile: 6
file content (550 lines) | stat: -rw-r--r-- 20,771 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Schema::Loader do
  Boolean = "Boolean"
  ID = "ID"
  Int = "Int"

  let(:schema) {
    node_type = Module.new do
      include GraphQL::Schema::Interface
      graphql_name "Node"

      field :id, ID, null: false
    end

    choice_type = Class.new(GraphQL::Schema::Enum) do
      graphql_name "Choice"

      value "FOO", value: :foo
      value "BAR", deprecation_reason: "Don't use BAR"
    end

    sub_input_type = Class.new(GraphQL::Schema::InputObject) do
      graphql_name "Sub"
      argument :string, String, required: false
    end

    big_int_type = Class.new(GraphQL::Schema::Scalar) do
      graphql_name "BigInt"
      specified_by_url "https://bigint.com"
      def self.coerce_input(value, _ctx)
        value =~ /\d+/ ? Integer(value) : nil
      end

      def self.coerce_result(value, _ctx)
        value.to_s
      end
    end

    variant_input_type = Class.new(GraphQL::Schema::InputObject) do
      graphql_name "Varied"
      argument :id, ID, required: false
      argument :int, Int, required: false
      argument :bigint, big_int_type, required: false, default_value: 2**54
      argument :float, Float, required: false
      argument :bool, Boolean, required: false
      argument :enum, choice_type, required: false
      argument :sub, [sub_input_type], required: false
      argument :deprecated_arg, String, required: false, deprecation_reason: "Don't use Varied.deprecatedArg"
    end

    variant_input_type_with_nulls = Class.new(GraphQL::Schema::InputObject) do
      graphql_name "VariedWithNulls"
      argument :id, ID, required: false, default_value: nil
      argument :int, Int, required: false, default_value: nil
      argument :bigint, big_int_type, required: false, default_value: nil
      argument :float, Float, required: false, default_value: nil
      argument :bool, Boolean, required: false, default_value: nil
      argument :enum, choice_type, required: false, default_value: nil
      argument :sub, [sub_input_type], required: false, default_value: nil
    end

    comment_type = Class.new(GraphQL::Schema::Object) do
      graphql_name "Comment"
      description "A blog comment"
      implements node_type

      field :body, String, null: false

      field :field_with_arg, Int do
        argument :bigint, big_int_type, default_value: 2**54, required: false
      end
    end

    media_type = Module.new do
      include GraphQL::Schema::Interface

      graphql_name "Media"
      description "!!!"
      field :type, String, null: false
    end

    video_type = Class.new(GraphQL::Schema::Object) do
      graphql_name "Video"
      implements media_type
    end

    audio_type = Class.new(GraphQL::Schema::Object) do
      graphql_name "Audio"
      implements media_type
    end

    post_type = Class.new(GraphQL::Schema::Object) do
      graphql_name "Post"
      description "A blog post"

      field :id, ID, null: false
      field :title, String, null: false
      field :summary, String, deprecation_reason: "Don't use Post.summary"
      field :body, String, null: false
      field :comments, [comment_type]
      field :attachment, media_type
    end

    content_type = Class.new(GraphQL::Schema::Union) do
      graphql_name "Content"
      description "A post or comment"
      possible_types post_type, comment_type
    end

    query_root = Class.new(GraphQL::Schema::Object) do
      graphql_name "Query"
      description "The query root of this schema"

      field :post, post_type do
        argument :id, ID
        argument :varied, variant_input_type, required: false, default_value: { id: "123", int: 234, float: 2.3, enum: :foo, sub: [{ string: "str" }] }
        argument :variedWithNull, variant_input_type_with_nulls, required: false, default_value: { id: nil, int: nil, float: nil, enum: nil, sub: nil, bigint: nil, bool: nil }
        argument :variedArray, [variant_input_type], required: false, default_value: [{ id: "123", int: 234, float: 2.3, enum: :foo, sub: [{ string: "str" }] }]
        argument :enum, choice_type, required: false, default_value: :foo
        argument :array, [String], required: false, default_value: ["foo", "bar"]
        argument :deprecated_arg, String, required: false, deprecation_reason: "Don't use Varied.deprecatedArg"
      end

      field :content, content_type
    end

    ping_mutation = Class.new(GraphQL::Schema::RelayClassicMutation) do
      graphql_name "Ping"
    end

    mutation_root = Class.new(GraphQL::Schema::Object) do
      graphql_name "Mutation"
      field :ping, mutation: ping_mutation
    end

    repeatable_transform = Class.new(GraphQL::Schema::Directive::Transform) do
      graphql_name "repeatableTransform"
      repeatable(true)
    end

    Class.new(GraphQL::Schema) do
      query query_root
      mutation mutation_root
      orphan_types audio_type, video_type
      description "A schema for loader_spec.rb"
      directives repeatable_transform
    end
  }

  let(:schema_json) {
    schema.execute(GraphQL::Introspection.query(include_deprecated_args: true, include_schema_description: true, include_specified_by_url: true, include_is_repeatable: true))
  }

  describe "load" do
    def assert_deep_equal(expected_type, actual_type)
      if actual_type.is_a?(Array)
        actual_type.each_with_index do |obj, index|
          assert_deep_equal expected_type[index], obj
        end
      elsif actual_type.is_a?(GraphQL::Schema::Field)
        assert_equal expected_type.graphql_name, actual_type.graphql_name
        assert_equal expected_type.description, actual_type.description
        assert_equal expected_type.deprecation_reason, actual_type.deprecation_reason
        assert_equal expected_type.arguments.keys.sort, actual_type.arguments.keys.sort
        assert_deep_equal expected_type.arguments.values.sort_by(&:graphql_name), actual_type.arguments.values.sort_by(&:graphql_name)
      elsif actual_type.is_a?(GraphQL::Schema::EnumValue)
        assert_equal expected_type.graphql_name, actual_type.graphql_name
        assert_equal expected_type.description, actual_type.description
        assert_equal expected_type.deprecation_reason, actual_type.deprecation_reason
      elsif actual_type.is_a?(GraphQL::Schema::Argument)
        assert_equal expected_type.graphql_name, actual_type.graphql_name
        assert_equal expected_type.description, actual_type.description
        assert_equal expected_type.deprecation_reason, actual_type.deprecation_reason
        assert_deep_equal expected_type.type, actual_type.type
      elsif actual_type.is_a?(GraphQL::Schema::NonNull) || actual_type.is_a?(GraphQL::Schema::List)
        assert_equal expected_type.class, actual_type.class
        assert_deep_equal expected_type.of_type, actual_type.of_type
      elsif actual_type < GraphQL::Schema
        assert_equal expected_type.query.graphql_name, actual_type.query.graphql_name
        assert_equal expected_type.mutation.graphql_name, actual_type.mutation.graphql_name
        assert_equal expected_type.directives.keys.sort, actual_type.directives.keys.sort
        assert_deep_equal expected_type.directives.values.sort_by(&:graphql_name), actual_type.directives.values.sort_by(&:graphql_name)
        assert_equal expected_type.types.keys.sort, actual_type.types.keys.sort
        assert_deep_equal expected_type.types.values.sort_by(&:graphql_name), actual_type.types.values.sort_by(&:graphql_name)
        assert_equal expected_type.description, actual_type.description
      elsif actual_type < GraphQL::Schema::Object
        assert_equal expected_type.graphql_name, actual_type.graphql_name
        assert_equal expected_type.description, actual_type.description
        assert_equal expected_type.interfaces.map(&:graphql_name).sort, actual_type.interfaces.map(&:graphql_name).sort
        assert_deep_equal expected_type.interfaces.sort_by(&:graphql_name), actual_type.interfaces.sort_by(&:graphql_name)
        assert_equal expected_type.fields.keys.sort, actual_type.fields.keys.sort
        assert_deep_equal expected_type.fields.values.sort_by(&:graphql_name), actual_type.fields.values.sort_by(&:graphql_name)
      elsif actual_type < GraphQL::Schema::Interface
        assert_equal expected_type.graphql_name, actual_type.graphql_name
        assert_equal expected_type.description, actual_type.description
        assert_equal expected_type.fields.keys.sort, actual_type.fields.keys.sort
        assert_deep_equal expected_type.fields.values.sort_by(&:graphql_name), actual_type.fields.values.sort_by(&:graphql_name)
      elsif actual_type < GraphQL::Schema::Union
        assert_equal expected_type.graphql_name, actual_type.graphql_name
        assert_equal expected_type.description, actual_type.description
        assert_equal expected_type.possible_types.map(&:graphql_name).sort, actual_type.possible_types.map(&:graphql_name).sort
        assert_deep_equal expected_type.possible_types.sort_by(&:graphql_name), actual_type.possible_types.sort_by(&:graphql_name)
      elsif actual_type < GraphQL::Schema::Scalar
        assert_equal expected_type.graphql_name, actual_type.graphql_name
        assert_equal expected_type.specified_by_url, actual_type.specified_by_url
      elsif actual_type < GraphQL::Schema::Enum
        assert_equal expected_type.graphql_name, actual_type.graphql_name
        assert_equal expected_type.description, actual_type.description
        assert_deep_equal expected_type.values.values.sort_by(&:graphql_name), actual_type.values.values.sort_by(&:graphql_name)
      elsif actual_type < GraphQL::Schema::InputObject
        assert_equal expected_type.graphql_name, actual_type.graphql_name
        assert_equal expected_type.arguments.keys.sort, actual_type.arguments.keys.sort
        assert_deep_equal expected_type.arguments.values.sort_by(&:graphql_name), actual_type.arguments.values.sort_by(&:graphql_name)
      elsif actual_type < GraphQL::Schema::Directive
        assert_equal expected_type.graphql_name, actual_type.graphql_name
        assert_equal expected_type.description, actual_type.description
        assert_equal expected_type.repeatable?, actual_type.repeatable?
        assert_equal expected_type.locations.sort, actual_type.locations.sort
        assert_equal expected_type.arguments.keys.sort, actual_type.arguments.keys.sort
        assert_deep_equal expected_type.arguments.values.sort_by(&:graphql_name), actual_type.arguments.values.sort_by(&:graphql_name)
      else
        assert_equal expected_type, actual_type
      end
    end

    let(:loaded_schema) { GraphQL::Schema.from_introspection(schema_json) }

    it "returns the schema" do
      assert_deep_equal(schema, loaded_schema)
    end

    it "can export the loaded schema" do
      assert loaded_schema.execute(GraphQL::Introspection::INTROSPECTION_QUERY)
    end

    it "has no-op coerce functions" do
      custom_scalar = loaded_schema.types["BigInt"]
      assert_equal true, custom_scalar.valid_isolated_input?("anything")
      assert_equal true, custom_scalar.valid_isolated_input?(12345)
    end

    it "sets correct default values on custom scalar arguments" do
      type = loaded_schema.types["Comment"]
      field = type.fields['fieldWithArg']
      arg = field.arguments['bigint']

      assert_equal((2**54).to_s, arg.default_value)
    end

    it "sets correct default values on custom scalar input fields" do
      type = loaded_schema.types["Varied"]
      field = type.arguments['bigint']

      assert_equal((2**54).to_s, field.default_value)
    end

    it "sets correct default values for complex field arguments" do
      type = loaded_schema.types['Query']
      field = type.fields['post']

      varied = field.arguments['varied']
      assert_equal varied.default_value, { 'id' => "123", 'int' => 234, 'float' => 2.3, 'enum' => "FOO", 'sub' => [{ 'string' => "str" }] }
      assert !varied.default_value.key?('bool'), 'Omits default value for unspecified arguments'

      variedArray = field.arguments['variedArray']
      assert_equal variedArray.default_value, [{ 'id' => "123", 'int' => 234, 'float' => 2.3, 'enum' => "FOO", 'sub' => [{ 'string' => "str" }] }]
      assert !variedArray.default_value.first.key?('bool'), 'Omits default value for unspecified arguments'

      array = field.arguments['array']
      assert_equal array.default_value, ["foo", "bar"]
    end

    it "does not set default value when there are none on input fields" do
      type = loaded_schema.types['Varied']

      assert !type.arguments['id'].default_value?
      assert !type.arguments['int'].default_value?
      assert type.arguments['bigint'].default_value?
      assert !type.arguments['float'].default_value?
      assert !type.arguments['bool'].default_value?
      assert !type.arguments['enum'].default_value?
      assert !type.arguments['sub'].default_value?
    end

    it "sets correct default values `null` on input fields" do
      type = loaded_schema.types['VariedWithNulls']

      assert type.arguments['id'].default_value?
      assert type.arguments['id'].default_value.nil?

      assert type.arguments['int'].default_value?
      assert type.arguments['int'].default_value.nil?

      assert type.arguments['bigint'].default_value?
      assert type.arguments['bigint'].default_value.nil?

      assert type.arguments['float'].default_value?
      assert type.arguments['float'].default_value.nil?

      assert type.arguments['bool'].default_value?
      assert type.arguments['bool'].default_value.nil?

      assert type.arguments['enum'].default_value?
      assert type.arguments['enum'].default_value.nil?

      assert type.arguments['sub'].default_value?
      assert type.arguments['sub'].default_value.nil?
    end

    it "works with underscored names" do
      schema_sdl = <<-GRAPHQL
type A_Type {
  f(argument_1: Int, argument_two: Int): Int
}

type Query {
  some_field: A_Type
}
      GRAPHQL

      introspection_res = GraphQL::Schema.from_definition(schema_sdl).as_json
      rebuilt_schema = GraphQL::Schema.from_introspection(introspection_res)

      assert_equal schema_sdl, rebuilt_schema.to_definition
    end

    it "doesnt warn about method conflicts (because it doesn't make method accesses)" do
      assert_output "", "" do
        GraphQL::Schema.from_introspection({
          "data" => {
            "__schema" => {
              "queryType" => {
                "name" => "Query"
              },
              "mutationType" => nil,
              "subscriptionType" => nil,
              "types" => [
                {
                  "kind" => "OBJECT",
                  "name" => "Query",
                  "description" => nil,
                  "fields" => [
                    {
                      "name" => "int",
                      "description" => nil,
                      "args" => [
                        {
                          "name" => "method",
                          "description" => nil,
                          "type" => {
                            "kind" => "SCALAR",
                            "name" => "Int",
                            "ofType" => nil
                          },
                          "defaultValue" => nil
                        }
                      ],
                      "type" => {
                        "kind" => "SCALAR",
                        "name" => "Int",
                        "ofType" => nil
                      },
                      "isDeprecated" => false,
                      "deprecationReason" => nil
                    }
                  ],
                  "inputFields" => nil,
                  "interfaces" => [

                  ],
                  "enumValues" => nil,
                  "possibleTypes" => nil
                },
                {
                  "kind" => "SCALAR",
                  "name" => "Int",
                  "description" => "Represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.",
                  "fields" => nil,
                  "inputFields" => nil,
                  "interfaces" => nil,
                  "enumValues" => nil,
                  "possibleTypes" => nil
                },
              ]
            }
          }
        })
      end
    end

    it "sets correct default values `nil` on complex field arguments" do
      type = loaded_schema.types['Query']
      field = type.fields['post']
      arg = field.arguments['variedWithNull']

      assert_equal arg.default_value, { 'id' => nil, 'int' => nil, 'float' => nil, 'enum' => nil, 'sub' => nil, 'bool' => nil, 'bigint' => nil }
    end
  end

  it "validates field argument names" do
    json = {
      "data" => {
        "__schema" => {
          "queryType" => {
            "name" => "Query"
          },
          "mutationType" => nil,
          "subscriptionType" => nil,
          "types" => [
            {
              "kind" => "OBJECT",
              "name" => "Query",
              "description" => nil,
              "fields" => [
                {
                  "name" => "int",
                  "description" => nil,
                  "type" => {
                    "kind" => "SCALAR",
                    "name" => "Int",
                    "ofType" => nil,
                  },
                  "args" => [
                    {
                      "name" => "something-wrong",
                      "description" => nil,
                      "type" => {
                        "kind" => "SCALAR",
                        "name" => "Int",
                        "ofType" => nil
                      },
                      "defaultValue" => nil
                    }
                  ],
                }
              ]
            }
          ]
        }
      }
    }
    err = assert_raises GraphQL::InvalidNameError do
      GraphQL::Schema.from_introspection(json)
    end

    assert_includes err.message, "something-wrong"
  end

  it "validates field names" do
    json = {
      "data" => {
        "__schema" => {
          "queryType" => {
            "name" => "Query"
          },
          "mutationType" => nil,
          "subscriptionType" => nil,
          "types" => [
            {
              "kind" => "OBJECT",
              "name" => "Query",
              "description" => nil,
              "fields" => [
                {
                  "name" => "bad.int",
                  "description" => nil,
                  "type" => {
                    "kind" => "SCALAR",
                    "name" => "Int",
                    "ofType" => nil,
                  },
                  "args" => [],
                }
              ]
            }
          ]
        }
      }
    }
    err = assert_raises GraphQL::InvalidNameError do
      GraphQL::Schema.from_introspection(json)
    end

    assert_includes err.message, "bad.int"
  end

  it "validates input object argument names" do
    json = {
      "data" => {
        "__schema" => {
          "queryType" => {
            "name" => "Query"
          },
          "mutationType" => nil,
          "subscriptionType" => nil,
          "types" => [
            {
              "kind" => "OBJECT",
              "name" => "Query",
              "description" => nil,
              "fields" => [
                {
                  "name" => "int",
                  "description" => nil,
                  "type" => {
                    "kind" => "SCALAR",
                    "name" => "Int",
                    "ofType" => nil,
                  },
                  "args" => [
                    {
                      "name" => "inputObject",
                      "description" => nil,
                      "type" => {
                        "kind" => "INPUT_OBJECT",
                        "name" => "SomeInputObject",
                        "ofType" => nil
                      },
                      "defaultValue" => nil
                    }
                  ],
                }
              ]
            },
            {
              "kind" => "INPUT_OBJECT",
              "name" => "SomeInputObject",
              "description" => nil,
              "inputFields" => [
                {
                  "name"=>"bad, input",
                  "type"=> { "kind" => "SCALAR", "name" => "String"},
                  "defaultValue"=> nil,
                  "description" => nil,
                },
              ]
            }
          ]
        }
      }
    }
    err = assert_raises GraphQL::InvalidNameError do
      GraphQL::Schema.from_introspection(json)
    end

    assert_includes err.message, "bad, input"
  end
end