File: visitor_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 (398 lines) | stat: -rw-r--r-- 10,671 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
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Language::Visitor do
  let(:document) { GraphQL.parse("
    query cheese {
      cheese(id: 1) {
        flavor,
        source,
        producers(first: 3) {
          name
        }
        ... cheeseFields
      }
    }

    fragment cheeseFields on Cheese { flavor }
    ")}

  module CountWhileVisiting
    attr_reader :counts
    def initialize(document)
      @counts = {fields_entered: 0, arguments_entered: 0, arguments_left: 0, argument_names: []}
      super
    end

    def on_field(node, parent)
      counts[:fields_entered] += 1
      super(node, parent)
    end

    def on_argument(node, parent)
      counts[:argument_names] << node.name
      counts[:arguments_entered] += 1
      super
    ensure
      counts[:arguments_left] += 1
    end

    def on_document(node, parent)
      counts[:finished] = true
      super
    end
  end

  class VisitorSpecVisitor < GraphQL::Language::Visitor
    include CountWhileVisiting
  end

  class VisitorSpecStaticVisitor < GraphQL::Language::StaticVisitor
    include CountWhileVisiting
  end

  let(:visitor) { VisitorSpecVisitor.new(document) }
  let(:counts) { visitor.counts }

  it "visitor calls hooks during a depth-first tree traversal" do
    visitor.visit
    assert_equal(6, counts[:fields_entered])
    assert_equal(2, counts[:arguments_entered])
    assert_equal(2, counts[:arguments_left])
    assert_equal(["id", "first"], counts[:argument_names])
    assert(counts[:finished])

    static_visitor = VisitorSpecStaticVisitor.new(document)
    static_visitor.visit
    static_counts = static_visitor.counts

    assert_equal(6, static_counts[:fields_entered])
    assert_equal(2, static_counts[:arguments_entered])
    assert_equal(2, static_counts[:arguments_left])
    assert_equal(["id", "first"], static_counts[:argument_names])
    assert(static_counts[:finished])
  end

  class SkippingVisitor < VisitorSpecVisitor
    def on_document(_n, _p); end
  end

  describe "Visitor::SKIP" do
    let(:visitor) { SkippingVisitor.new(document) }

    it "visitor skips the rest of the node" do
      visitor.visit
      assert_equal(0, counts[:fields_entered])
    end
  end

  describe "AST modification" do
    class ModificationTestVisitor < GraphQL::Language::Visitor
      def on_field(node, parent)
        if node.name == "c"
          new_node = node.merge(name: "renamedC")
          super(new_node, parent)
        elsif node.name == "addFields"
          new_node = node.merge_selection(name: "addedChild")
          super(new_node, parent)
        elsif node.name == "anotherAddition"
          new_node = node
            .merge_argument(name: "addedArgument", value: 1)
            .merge_directive(name: "doStuff")
          super(new_node, parent)
        else
          super
        end
      end

      def on_argument(node, parent)
        # https://github.com/rmosolgo/graphql-ruby/issues/2148
        # Parent could become a random value, double check that it's a node
        # to actually fail the test
        raise RuntimeError, "Parent isn't a Node!" unless parent.class < GraphQL::Language::Nodes::AbstractNode

        if node.name == "deleteMe"
          super(DELETE_NODE, parent)
        elsif node.name.include?("nope")
          [1]
        else
          super
        end
      end

      def on_variable_identifier(node, parent)
        if node.name == "firstName"
          node = node.merge(name: "lastName")
        end
        super(node, parent)
      end

      def on_input_object(node, parent)
        if node.arguments.map(&:name).sort == ["delete", "me"]
          super(DELETE_NODE, parent)
        else
          super
        end
      end

      def on_directive(node, parent)
        if node.name == "doStuff"
          new_node = node.merge_argument(name: "addedArgument2", value: 2)
          super(new_node, parent)
        else
          super
        end
      end

      def on_inline_fragment(node, parent)
        if node.selections.map(&:name) == ["renameFragmentField", "spread"]
          _field, spread = node.selections
          new_node = node.merge(selections: [GraphQL::Language::Nodes::Field.new(name: "renamed"), spread])
          super(new_node, parent)
        else
          super(node, parent)
        end
      end

      def on_fragment_spread(node, parent)
        if node.name == "spread"
          new_node = node.merge(name: "renamedSpread")
          super(new_node, parent)
        else
          super(node, parent)
        end
      end

      def on_object_type_definition(node, parent)
        if node.name == "Rename"
          new_node = node.merge(name: "WasRenamed")
          super(new_node, parent)
        else
          super(node, parent)
        end
      end

      def on_field_definition(node, parent)
        if node.name == "renameThis"
          new_node = node.merge(name: "wasRenamed")
          super(new_node, parent)
        else
          super
        end
      end

      def on_input_value_definition(node, parent)
        if node.name == "renameThisArg"
          new_node = node.merge(name: "argWasRenamed")
          super(new_node, parent)
        else
          super
        end
      end

      def on_variable_definition(node, parent)
        if node.type.name == 'A'
          new_type = GraphQL::Language::Nodes::TypeName.new(name: 'RenamedA')
          super(node.merge(type: new_type), parent)
        elsif node.name == "firstName"
          super(node.merge(name: "lastName"), parent)
        else
          super
        end
      end
    end

    def get_result(query_str)
      document = GraphQL.parse(query_str)
      visitor = ModificationTestVisitor.new(document)
      visitor.visit
      return document, visitor.result
    end

    it "can modify variable names" do
      query = <<-GRAPHQL.chop
query($firstName: String) {
  a(b: $firstName)
}
      GRAPHQL
      expected_result = <<-GRAPHQL.chop
query($lastName: String) {
  a(b: $lastName)
}
      GRAPHQL
      document, new_document = get_result(query)
      assert_equal expected_result, new_document.to_query_string, "the result has changes"
      assert_equal query, document.to_query_string, "the original is unchanged"
    end

    it "returns a new AST with modifications applied" do
      query = <<-GRAPHQL.chop
query($a: A, $b: B) {
  a(a1: 1) {
    b(b2: 2) {
      c(c3: 3)
    }
  }
  d(d4: 4)
}
      GRAPHQL
      document, new_document = get_result(query)
      refute_equal document, new_document
      expected_result = <<-GRAPHQL.chop
query($a: RenamedA, $b: B) {
  a(a1: 1) {
    b(b2: 2) {
      renamedC(c3: 3)
    }
  }
  d(d4: 4)
}
GRAPHQL
      assert_equal expected_result, new_document.to_query_string, "the result has changes"
      assert_equal query, document.to_query_string, "the original is unchanged"

      # This is testing the implementation: nodes which aren't affected by modification
      # should be shared between the two trees
      orig_c3_argument =     document.definitions.first.selections.first.selections.first.selections.first.arguments.first
      copy_c3_argument = new_document.definitions.first.selections.first.selections.first.selections.first.arguments.first
      assert_equal "c3", orig_c3_argument.name
      assert orig_c3_argument.equal?(copy_c3_argument), "Child nodes are persisted"

      orig_d_field =     document.definitions.first.selections[1]
      copy_d_field = new_document.definitions.first.selections[1]
      assert_equal "d", orig_d_field.name
      assert orig_d_field.equal?(copy_d_field), "Sibling nodes are persisted"

      orig_b_field =     document.definitions.first.selections.first.selections.first
      copy_b_field = new_document.definitions.first.selections.first.selections.first
      assert_equal "b", orig_b_field.name
      refute orig_b_field.equal?(copy_b_field), "Parents with modified children are copied"
    end

    it "deletes nodes with DELETE_NODE" do
      before_query = <<-GRAPHQL.chop
query {
  f1 {
    f2(deleteMe: 1) {
      f3(c1: {deleteMe: {c2: 2}})
      f4(c2: [{keepMe: 1}, {deleteMe: 2}, {keepMe: 3}])
    }
  }
}
GRAPHQL

      after_query = <<-GRAPHQL.chop
query {
  f1 {
    f2 {
      f3(c1: {})
      f4(c2: [{keepMe: 1}, {}, {keepMe: 3}])
    }
  }
}
GRAPHQL

      document, new_document = get_result(before_query)
      assert_equal before_query, document.to_query_string
      assert_equal after_query, new_document.to_query_string
    end

    it "Deletes from lists" do
      before_query = <<-GRAPHQL.chop
query {
  f1(arg1: [{a: 1}, {delete: 1, me: 2}, {b: 2}])
}
GRAPHQL

      after_query = <<-GRAPHQL.chop
query {
  f1(arg1: [{a: 1}, {b: 2}])
}
GRAPHQL

      document, new_document = get_result(before_query)
      assert_equal before_query, document.to_query_string
      assert_equal after_query, new_document.to_query_string
    end

    it "can add children" do
      before_query = <<-GRAPHQL.chop
query {
  addFields
  anotherAddition
}
GRAPHQL

      after_query = <<-GRAPHQL.chop
query {
  addFields {
    addedChild
  }
  anotherAddition(addedArgument: 1) @doStuff(addedArgument2: 2)
}
GRAPHQL

      document, new_document = get_result(before_query)
      assert_equal before_query, document.to_query_string
      assert_equal after_query, new_document.to_query_string
    end

    it "ignore non-Nodes::AbstractNode return values" do
      query = <<-GRAPHQL.chop
query {
  doesntDoAnything(stillNothing: {nope: 1, alsoNope: 2, stillNope: 3})
}
GRAPHQL

      document, new_document = get_result(query)
      assert_equal query, document.to_query_string
      assert_equal query, new_document.to_query_string
    end

    it "can modify inline fragments" do
      before_query = <<-GRAPHQL.chop
query {
  ... on Query {
    renameFragmentField
    ...spread
  }
}
GRAPHQL

      after_query = <<-GRAPHQL.chop
query {
  ... on Query {
    renamed
    ...renamedSpread
  }
}
GRAPHQL

      document, new_document = get_result(before_query)
      assert_equal before_query, document.to_query_string
      assert_equal after_query, new_document.to_query_string
    end

    it "works with SDL" do
      before_query = <<-GRAPHQL.chop
type Rename @doStuff {
  f: Int
  renameThis: String
  f2(renameThisArg: Boolean): Boolean
}
GRAPHQL

      after_query = <<-GRAPHQL.chop
type WasRenamed @doStuff(addedArgument2: 2) {
  f: Int
  wasRenamed: String
  f2(argWasRenamed: Boolean): Boolean
}
GRAPHQL

      document, new_document = get_result(before_query)
      assert_equal before_query, document.to_query_string
      assert_equal after_query, new_document.to_query_string
    end
  end
end