File: printer_spec.rb

package info (click to toggle)
ruby-graphql 2.5.19-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,868 kB
  • sloc: ruby: 80,420; ansic: 1,808; yacc: 845; javascript: 480; makefile: 6
file content (458 lines) | stat: -rw-r--r-- 11,628 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
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Language::Printer do
  let(:document) { GraphQL.parse(query_string) }
  let(:query_string) {%|
    query getStuff($someVar: Int = 1, $anotherVar: [String!] @special(very: true), $skipNested: Boolean! = false) @skip(if: false) {
      myField: someField(someArg: $someVar, ok: 1.4) @skip(if: $anotherVar) @thing(or: "Whatever")
      anotherField(someArg: [1, 2, 3]) {
        nestedField
        ...moreNestedFields @skip(if: $skipNested)
      }
      ... on OtherType @include(unless: false) {
        field(arg: [{key: "value", anotherKey: 0.9, anotherAnotherKey: WHATEVER}])
        anotherField
      }
      ... {
        id
      }
    }

    fragment moreNestedFields on NestedType @or(something: "ok") {
      anotherNestedField
    }
  |}

  let(:printer) { GraphQL::Language::Printer.new }

  describe "#print" do
    it "prints the query string" do
      assert_equal query_string.gsub(/^    /, "").strip, printer.print(document)
    end

    it "prints a truncated query string" do
      expected = query_string.gsub(/^    /, "").strip[0, 50 - GraphQL::Language::Printer::OMISSION.size]
      expected = "#{expected}#{GraphQL::Language::Printer::OMISSION}"

      assert_equal(
        expected,
        printer.print(document, truncate_size: 50),
      )
    end

    describe "inputs" do
      let(:query_string) {%|
        query {
          field(null_value: null, null_in_array: [1, null, 3], int: 3, float: 4.7e-24, bool: false, string: "☀︎🏆\\n escaped \\" unicode ¶ /", enum: ENUM_NAME, array: [7, 8, 9], object: {a: [1, 2, 3], b: {c: "4"}}, unicode_bom: "\xef\xbb\xbfquery")
        }
      |}

      it "prints the query string" do
        assert_equal query_string.gsub(/^        /, "").strip, printer.print(document)
      end
    end

    describe "schema" do
      describe "schema with convention names for root types" do
        let(:query_string) {<<-schema
          schema {
            query: Query
            mutation: Mutation
            subscription: Subscription
          }
        schema
        }

        it 'omits schema definition' do
          refute printer.print(document) =~ /schema/
        end
      end

      describe "schema with custom query root name" do
        let(:query_string) {<<-schema
          schema {
            query: MyQuery
            mutation: Mutation
            subscription: Subscription
          }
        schema
        }

        it 'includes schema definition' do
          assert_equal query_string.gsub(/^          /, "").strip, printer.print(document)
        end
      end

      describe "schema with custom mutation root name" do
        let(:query_string) {<<-schema
          schema {
            query: Query
            mutation: MyMutation
            subscription: Subscription
          }
        schema
        }

        it 'includes schema definition' do
          assert_equal query_string.gsub(/^          /, "").strip, printer.print(document)
        end
      end

      describe "schema with custom subscription root name" do
        let(:query_string) {<<-schema
          schema {
            query: Query
            mutation: Mutation
            subscription: MySubscription
          }
        schema
        }

        it 'includes schema definition' do
          assert_equal query_string.gsub(/^          /, "").strip, printer.print(document)
        end
      end

      describe "full featured schema" do
        # Based on: https://github.com/graphql/graphql-js/blob/bc96406ab44453a120da25a0bd6e2b0237119ddf/src/language/__tests__/schema-kitchen-sink.graphql
        let(:query_string) {<<-schema
          schema {
            query: QueryType
            mutation: MutationType
          }

          """
          Union description
          """
          union AnnotatedUnion @onUnion = A | B

          type Foo implements Bar & AnnotatedInterface {
            one: Type
            two(argument: InputType!): Type
            three(argument: InputType, other: String): Int
            four(argument: String = "string"): String
            five(argument: [String] = ["string", "string"]): String
            six(argument: InputType = {key: "value"}): Type
            seven(argument: String = null): Type
          }

          """
          Scalar description
          """
          scalar CustomScalar

          type AnnotatedObject implements Bar @onObject(arg: "value") {
            annotatedField(arg: Type = "default" @onArg): Type @onField
          }

          interface Bar {
            one: Type
            four(argument: String = "string"): String
          }

          """
          Enum description
          """
          enum Site {
            """
            Enum value description
            """
            DESKTOP
            MOBILE
          }

          interface AnnotatedInterface @onInterface {
            annotatedField(arg: Type @onArg): Type @onField
          }

          union Feed = Story | Article | Advert

          """
          Input description
          """
          input InputType {
            key: String!
            answer: Int = 42
          }

          union AnnotatedUnion @onUnion = A | B

          scalar CustomScalar

          """
          Directive description
          """
          directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

          scalar AnnotatedScalar @onScalar

          enum Site {
            DESKTOP
            MOBILE
          }

          enum AnnotatedEnum @onEnum {
            ANNOTATED_VALUE @onEnumValue
            OTHER_VALUE
          }

          input InputType {
            key: String!
            answer: Int = 42
          }

          input AnnotatedInput @onInputObjectType {
            annotatedField: Type @onField
          }

          directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

          directive @include(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT
        schema
        }

        it "generate" do
          assert_equal query_string.gsub(/^          /, "").strip, printer.print(document)
        end

        it "doesn't mutate the document" do
          assert_equal printer.print(document), printer.print(document)
        end
      end

      describe "schema extension" do
        let(:query_string) do
          <<-SCHEMA
          extend schema
            @onSchema
          {
            query: QueryType
            mutation: MutationType
          }

          extend union AnnotatedUnion @onUnion = A | B

          extend type Foo implements Bar @onType {
            one: Type
            two(argument: InputType!): Type
          }

          extend scalar CustomScalar @onScalar

          extend interface Bar @onInterface {
            one: Type
          }

          extend enum Site @onEnum {
            DESKTOP
            MOBILE
          }

          extend input InputType @onInputType {
            key: String!
            answer: Int = 42
          }
          SCHEMA
        end

        it "generates correctly" do
          assert_equal query_string.gsub(/^          /, "").strip, printer.print(document)
        end
      end
    end
  end

  it "handles comments"  do
    module MyInterface
      include GraphQL::Schema::Interface

      comment "Interface comment"
    end

    scalar = Class.new(GraphQL::Schema::Scalar) do
      graphql_name "DateTime"

      comment "Scalar comment"
    end

    query_type = Class.new(GraphQL::Schema::Object) do
      implements MyInterface

      graphql_name "Query"
      field :issue, Integer, comment: "Field comment" do
        argument :number, Integer, comment: "Argument comment"
        argument :date_time, scalar
      end

      def issue(number:)
        number
      end
    end

    enum_type = Class.new(GraphQL::Schema::Enum) do
      graphql_name "UserRole"

      comment "Enum comment"

      value "ADMIN"
      value "VIEWER", comment: "Enum value comment"
    end

    input_object = Class.new(GraphQL::Schema::InputObject) do
      graphql_name "CreateUserInput"

      comment "Input object comment"

      argument :first_name, String, comment: "Argument comment"
      argument :role, enum_type do
        comment "Argument comment"
      end
    end

    union = Class.new(GraphQL::Schema::Union) do
      graphql_name "CreateUserResponse"

      comment "Union comment"

      possible_types(
        Class.new(GraphQL::Schema::Object) do
          graphql_name "CreateUserSuccess"

          field :user, (Class.new(GraphQL::Schema::Object) do
            graphql_name "User"

            field :first_name, String, comment: "Field comment"
          end)
        end,
        Class.new(GraphQL::Schema::Object) do
          graphql_name "CreateUserError"

          comment "Object type comment"

          field :message, String, null: false do
            comment "Field comment"
          end
        end
      )
    end

    mutation = Class.new(GraphQL::Schema::Mutation) do
      graphql_name "CreateUser"
      comment "Mutation comment"

      argument :input, input_object, comment: "Input argument comment"

      field :payload, union, null: false
    end

    mutation_type = Class.new(GraphQL::Schema::Object) do
      graphql_name "Mutation"

      field :create_user, mutation: mutation
    end

    schema = Class.new(GraphQL::Schema) do
      query(query_type)
      mutation(mutation_type)
    end

    expected = <<~SCHEMA.chomp
      # Object type comment
      type CreateUserError {
        # Field comment
        message: String!
      }

      # Input object comment
      input CreateUserInput {
        # Argument comment
        firstName: String!

        # Argument comment
        role: UserRole!
      }

      """
      Autogenerated return type of CreateUser.
      """
      type CreateUserPayload {
        payload: CreateUserResponse!
      }

      # Union comment
      union CreateUserResponse = CreateUserError | CreateUserSuccess

      type CreateUserSuccess {
        user: User
      }

      # Scalar comment
      scalar DateTime

      type Mutation {
        # Mutation comment
        createUser(
          # Input argument comment
          input: CreateUserInput!
        ): CreateUserPayload
      }

      # Interface comment
      interface MyInterface

      type Query implements MyInterface {
        # Field comment
        issue(
          dateTime: DateTime!

          # Argument comment
          number: Int!
        ): Int
      }

      type User {
        # Field comment
        firstName: String
      }

      # Enum comment
      enum UserRole {
        ADMIN

        # Enum value comment
        VIEWER
      }
    SCHEMA

    assert_equal(
      expected,
      printer.print(schema.to_document),
    )
  end

  it "handles large ints" do
    query_type = Class.new(GraphQL::Schema::Object) do
      graphql_name "Query"
      field :issue, Integer do
        argument :number, Integer
      end

      def issue(number:)
        number
      end
    end

    schema = Class.new(GraphQL::Schema) do
      query(query_type)
    end
    query_str = "query {\n  issue(number: 9999.9e999)\n}"
    printed_query_str = "query {\n  issue(number: Infinity)\n}"

    assert_equal printed_query_str, GraphQL.parse(query_str).to_query_string

    result = schema.execute(query_str)
    expected_err = "Argument 'number' on Field 'issue' has an invalid value. Expected type 'Int!'."
    assert_equal [expected_err], result["errors"].map { |e| e["message"] }
  end
end