Module: GraphQL::Compatibility::LazyExecutionSpecification

Defined in:
lib/graphql/compatibility/lazy_execution_specification.rb,
lib/graphql/compatibility/lazy_execution_specification/lazy_schema.rb

Defined Under Namespace

Modules: LazySchema

Class Method Summary collapse

Class Method Details

.build_suite(execution_strategy) ⇒ Class<Minitest::Test>

Returns A test suite for this execution strategy

Parameters:

  • execution_strategy (<#new, #execute>)

    An execution strategy class

Returns:

  • (Class<Minitest::Test>)

    A test suite for this execution strategy


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
# File 'lib/graphql/compatibility/lazy_execution_specification.rb', line 9

def self.build_suite(execution_strategy)
  Class.new(Minitest::Test) do
    class << self
      attr_accessor :lazy_schema
    end

    self.lazy_schema = LazySchema.build(execution_strategy)

    def test_it_resolves_lazy_values
      pushes = []
      query_str = %|
      {
        p1: push(value: 1) {
          value
        }
        p2: push(value: 2) {
          push(value: 3) {
            value
            push(value: 21) {
              value
            }
          }
        }
        p3: push(value: 4) {
          push(value: 5) {
            value
            push(value: 22) {
              value
            }
          }
        }
      }
      |
      res = self.class.lazy_schema.execute(query_str, context: {pushes: pushes})

      expected_data = {
        "p1"=>{"value"=>1},
        "p2"=>{"push"=>{"value"=>3, "push"=>{"value"=>21}}},
        "p3"=>{"push"=>{"value"=>5, "push"=>{"value"=>22}}},
      }
      assert_equal expected_data, res["data"]

      expected_pushes = [
        [1,2,4], # first level
        [3,5], # second level
        [21, 22],
      ]
      assert_equal expected_pushes, pushes
    end

    def test_it_maintains_path
      query_str = %|
      {
        push(value: 2) {
          push(value: 3) {
            fail1: push(value: 14) {
              value
            }
            fail2: push(value: 14) {
              value
            }
          }
        }
      }
      |
      res = self.class.lazy_schema.execute(query_str, context: {pushes: []})
      assert_equal nil, res["data"]
      # The first fail causes the second field to never resolve
      assert_equal 1, res["errors"].length
      assert_equal ["push", "push", "fail1", "value"], res["errors"][0]["path"]
    end

    def test_it_resolves_mutation_values_eagerly
      pushes = []
      query_str = %|
      mutation {
        p1: push(value: 1) {
          value
        }
        p2: push(value: 2) {
          push(value: 3) {
            value
          }
        }
        p3: push(value: 4) {
          p5: push(value: 5) {
            value
          }
          p6: push(value: 6) {
            value
          }
        }
      }
      |
      res = self.class.lazy_schema.execute(query_str, context: {pushes: pushes})

      expected_data = {
        "p1"=>{"value"=>1},
        "p2"=>{"push"=>{"value"=>3}},
        "p3"=>{"p5"=>{"value"=>5},"p6"=>{"value"=>6}},
      }
      assert_equal expected_data, res["data"]

      expected_pushes = [
        [1],        # first operation
        [2], [3],   # second operation
        [4], [5, 6], # third operation
      ]
      assert_equal expected_pushes, pushes
    end

    def test_it_resolves_lazy_connections
      pushes = []
      query_str = %|
      {
        pushes(values: [1,2,3]) {
          edges {
            node {
              value
              push(value: 4) {
                value
              }
            }
          }
        }
      }
      |
      res = self.class.lazy_schema.execute(query_str, context: {pushes: pushes})

      expected_edges = [
        {"node"=>{"value"=>1, "push"=>{"value"=>4}}},
        {"node"=>{"value"=>2, "push"=>{"value"=>4}}},
        {"node"=>{"value"=>3, "push"=>{"value"=>4}}},
      ]
      assert_equal expected_edges, res["data"]["pushes"]["edges"]
      assert_equal [[1, 2, 3], [4, 4, 4]], pushes
    end

    def test_it_calls_lazy_resolve_instrumentation
      query_str = %|
      {
        p1: push(value: 1) {
          value
        }
        p2: push(value: 2) {
          push(value: 3) {
            value
          }
        }
        pushes(values: [1,2,3]) {
          edges {
            node {
              value
              push(value: 4) {
                value
              }
            }
          }
        }
      }
      |

      log = []
      self.class.lazy_schema.execute(query_str, context: {lazy_instrumentation: log, pushes: []})
      expected_log = [
        "PUSH",
        "Query.push: 1",
        "Query.push: 2",
        "Query.pushes: [1, 2, 3]",
        "PUSH",
        "LazyPush.push: 3",
        "LazyPushEdge.node: 1",
        "LazyPushEdge.node: 2",
        "LazyPushEdge.node: 3",
        "PUSH",
        "LazyPush.push: 4",
        "LazyPush.push: 4",
        "LazyPush.push: 4",
      ]
      assert_equal expected_log, log
    end

    def test_it_skips_ctx_skip
      query_string = <<-GRAPHQL
      {
        p0: push(value: 15) { value }
        p1: push(value: 1) { value }
        p2: push(value: 2) {
          value
          p3: push(value: 15) {
            value
          }
        }
      }
      GRAPHQL
      pushes = []
      res = self.class.lazy_schema.execute(query_string, context: {pushes: pushes})
      assert_equal [[1,2]], pushes
      assert_equal({"data"=>{"p1"=>{"value"=>1}, "p2"=>{"value"=>2}}}, res)
    end
  end
end