Module: GraphQL::Compatibility::ExecutionSpecification

Defined in:
lib/graphql/compatibility/execution_specification.rb,
lib/graphql/compatibility/execution_specification/counter_schema.rb,
lib/graphql/compatibility/execution_specification/specification_schema.rb

Overview

Test an execution strategy. This spec is not meant as a development aid. Rather, when the strategy works, run it here to see if it has any differences from the built-in strategy.

  • Custom scalar input / output
  • Null propagation
  • Query-level masking
  • Directive support
  • Typecasting
  • Error handling (raise / return GraphQL::ExecutionError)
  • Provides Irep & AST node to resolve fn
  • Skipping fields

Some things are explicitly not tested here, because they're handled by other parts of the system:

  • Schema definition (including types and fields)
  • Parsing & parse errors
  • AST -> IRep transformation (eg, fragment merging)
  • Query validation and analysis
  • Relay features

Defined Under Namespace

Modules: CounterSchema, SpecificationSchema

Class Method Summary collapse

Class Method Details

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

Make a minitest suite for this execution strategy, making sure it fulfills all the requirements of this library.

Parameters:

  • execution_strategy (<#new, #execute>)

    An execution strategy class

Returns:

  • (Class<Minitest::Test>)

    A test suite for this execution strategy



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

def self.build_suite(execution_strategy)
  GraphQL::Deprecation.warn "#{self} will be removed from GraphQL-Ruby 2.0. There is no replacement, please open an issue on GitHub if you need support."
  Class.new(Minitest::Test) do
    class << self
      attr_accessor :counter_schema, :specification_schema
    end

    self.specification_schema = SpecificationSchema.build(execution_strategy)
    self.counter_schema = CounterSchema.build(execution_strategy)

    def execute_query(query_string, **kwargs)
      kwargs[:root_value] = SpecificationSchema::DATA
      self.class.specification_schema.execute(query_string, **kwargs)
    end

    def test_it_fetches_data
      query_string = %|
      query getData($nodeId: ID = "1001") {
        flh: node(id: $nodeId) {
          __typename
          ... on Person {
            name @include(if: true)
            skippedName: name @skip(if: true)
            birthdate
            age(on: 1477660133)
          }

          ... on NamedEntity {
            ne_tn: __typename
            ne_n: name
          }

          ... on Organization {
            org_n: name
          }
        }
      }
      |
      res = execute_query(query_string)

      assert_equal nil, res["errors"], "It doesn't have an errors key"

      flh = res["data"]["flh"]
      assert_equal "Fannie Lou Hamer", flh["name"], "It returns values"
      assert_equal Time.new(1917, 10, 6).to_i, flh["birthdate"], "It returns custom scalars"
      assert_equal 99, flh["age"], "It runs resolve functions"
      assert_equal "Person", flh["__typename"], "It serves __typename"
      assert_equal "Person", flh["ne_tn"], "It serves __typename on interfaces"
      assert_equal "Fannie Lou Hamer", flh["ne_n"], "It serves interface fields"
      assert_equal false, flh.key?("skippedName"), "It obeys @skip"
      assert_equal false, flh.key?("org_n"), "It doesn't apply other type fields"
    end

    def test_it_iterates_over_each
      query_string = %|
        query getData($nodeId: ID = "1002") {
          node(id: $nodeId) {
            ... on Person {
              organizations { name }
            }
          }
        }
      |

      res = execute_query(query_string)
      assert_equal ["SNCC"], res["data"]["node"]["organizations"].map { |o| o["name"] }
    end

    def test_it_skips_skipped_fields
      query_str = "      {\n        o3001: organization(id: \"3001\")  { name }\n        o2001: organization(id: \"2001\")  { name }\n      }\n      GRAPHQL\n\n      res = execute_query(query_str)\n      assert_equal [\"o2001\"], res[\"data\"].keys\n      assert_equal false, res.key?(\"errors\")\n    end\n\n    def test_it_propagates_nulls_to_field\n      query_string = %|\n      query getOrg($id: ID = \"2001\"){\n        failure: node(id: $id) {\n          ... on Organization {\n            name\n            leader { name }\n          }\n        }\n        success: node(id: $id) {\n          ... on Organization {\n            name\n          }\n        }\n      }\n      |\n      res = execute_query(query_string)\n\n      failure = res[\"data\"][\"failure\"]\n      success = res[\"data\"][\"success\"]\n\n      assert_equal nil, failure, \"It propagates nulls to the next nullable field\"\n      assert_equal({\"name\" => \"SNCC\"}, success, \"It serves the same object if no invalid null is encountered\")\n      assert_equal 1, res[\"errors\"].length , \"It returns an error for the invalid null\"\n    end\n\n    def test_it_propages_nulls_to_operation\n      query_string = %|\n        {\n          foundOrg: organization(id: \"2001\") {\n            name\n          }\n          organization(id: \"2999\") {\n            name\n          }\n        }\n      |\n\n      res = execute_query(query_string)\n      assert_equal nil, res[\"data\"]\n      assert_equal 1, res[\"errors\"].length\n    end\n\n    def test_it_exposes_raised_and_returned_user_execution_errors\n      query_string = %|\n        {\n          organization(id: \"2001\") {\n            name\n            returnedError\n            raisedError\n          }\n          organizations {\n            returnedError\n            raisedError\n          }\n        }\n      |\n\n      res = execute_query(query_string)\n\n      assert_equal \"SNCC\", res[\"data\"][\"organization\"][\"name\"], \"It runs the rest of the query\"\n\n      expected_errors = [\n        {\n          \"message\"=>\"This error was returned\",\n          \"locations\"=>[{\"line\"=>5, \"column\"=>19}],\n          \"path\"=>[\"organization\", \"returnedError\"]\n        },\n        {\n          \"message\"=>\"This error was raised\",\n          \"locations\"=>[{\"line\"=>6, \"column\"=>19}],\n          \"path\"=>[\"organization\", \"raisedError\"]\n        },\n        {\n          \"message\"=>\"This error was raised\",\n          \"locations\"=>[{\"line\"=>10, \"column\"=>19}],\n          \"path\"=>[\"organizations\", 0, \"raisedError\"]\n        },\n        {\n          \"message\"=>\"This error was raised\",\n          \"locations\"=>[{\"line\"=>10, \"column\"=>19}],\n          \"path\"=>[\"organizations\", 1, \"raisedError\"]\n        },\n        {\n          \"message\"=>\"This error was returned\",\n          \"locations\"=>[{\"line\"=>9, \"column\"=>19}],\n          \"path\"=>[\"organizations\", 0, \"returnedError\"]\n        },\n        {\n          \"message\"=>\"This error was returned\",\n          \"locations\"=>[{\"line\"=>9, \"column\"=>19}],\n          \"path\"=>[\"organizations\", 1, \"returnedError\"]\n        },\n      ]\n\n      expected_errors.each do |expected_err|\n        assert_includes res[\"errors\"], expected_err\n      end\n    end\n\n    def test_it_applies_masking\n      no_org = ->(member, ctx) { member.name == \"Organization\" }\n      query_string = %|\n      {\n        node(id: \"2001\") {\n          __typename\n        }\n      }|\n\n      err = assert_raises(GraphQL::UnresolvedTypeError) {\n        execute_query(query_string, except: no_org)\n      }\n\n      query_string = %|\n      {\n        organization(id: \"2001\") { name }\n      }|\n\n      res = execute_query(query_string, except: no_org)\n\n      assert_equal nil, res[\"data\"]\n      assert_equal 1, res[\"errors\"].length\n      assert_equal \"SNCC\", err.value.name\n      assert_equal GraphQL::Relay::Node.interface, err.field.type\n      assert_equal 1, err.possible_types.length\n      assert_equal \"Organization\", err.resolved_type.name\n      assert_equal \"Query\", err.parent_type.name\n\n      query_string = %|\n      {\n        __type(name: \"Organization\") { name }\n      }|\n\n      res = execute_query(query_string, except: no_org)\n\n      assert_equal nil, res[\"data\"][\"__type\"]\n      assert_equal nil, res[\"errors\"]\n    end\n\n    def test_it_provides_nodes_to_resolve\n      query_string = %|\n      {\n        organization(id: \"2001\") {\n          name\n          nodePresence\n        }\n      }|\n\n      res = execute_query(query_string)\n      assert_equal \"SNCC\", res[\"data\"][\"organization\"][\"name\"]\n      assert_equal [true, true, false], res[\"data\"][\"organization\"][\"nodePresence\"]\n    end\n\n    def test_it_runs_the_introspection_query\n      execute_query(GraphQL::Introspection::INTROSPECTION_QUERY)\n    end\n\n    def test_it_propagates_deeply_nested_nulls\n      query_string = %|\n      {\n        node(id: \"1001\") {\n          ... on Person {\n            name\n            first_organization {\n              leader {\n                name\n              }\n            }\n          }\n        }\n      }\n      |\n      res = execute_query(query_string)\n      assert_equal nil, res[\"data\"][\"node\"]\n      assert_equal 1, res[\"errors\"].length\n    end\n\n    def test_it_doesnt_add_errors_for_invalid_nulls_from_execution_errors\n      query_string = %|\n      query getOrg($id: ID = \"2001\"){\n        failure: node(id: $id) {\n          ... on Organization {\n            name\n            leader { name }\n          }\n        }\n      }\n      |\n      res = execute_query(query_string, context: {return_error: true})\n      error_messages = res[\"errors\"].map { |e| e[\"message\"] }\n      assert_equal [\"Error on Nullable\"], error_messages\n    end\n\n    def test_it_only_resolves_fields_once_on_typed_fragments\n      res = self.class.counter_schema.execute(\"\n      {\n        counter { count }\n        ... on HasCounter {\n          counter { count }\n        }\n      }\n      \")\n\n      expected_data = {\n        \"counter\" => { \"count\" => 1 }\n      }\n      assert_equal expected_data, res[\"data\"]\n      assert_equal 1, self.class.counter_schema.metadata[:count]\n\n      # Deep typed children are correctly distinguished:\n      res = self.class.counter_schema.execute(\"\n      {\n        counter {\n          ... on Counter {\n            counter { count }\n          }\n          ... on AltCounter {\n            counter { count, t: __typename }\n          }\n        }\n      }\n      \")\n\n      expected_data = {\n        \"counter\" => { \"counter\" => { \"count\" => 2 } }\n      }\n      assert_equal expected_data, res[\"data\"]\n    end\n\n    def test_it_runs_middleware\n      log = []\n      query_string = %|\n      {\n        node(id: \"2001\") {\n          __typename\n        }\n      }|\n      execute_query(query_string, context: {middleware_log: log})\n      assert_equal [\"node\", \"__typename\"], log\n    end\n\n    def test_it_uses_type_error_hooks_for_invalid_nulls\n      log = []\n      query_string = %|\n      {\n        node(id: \"1001\") {\n          ... on Person {\n            name\n            first_organization {\n              leader {\n                name\n              }\n            }\n          }\n        }\n      }|\n\n      res = execute_query(query_string, context: { type_errors: log })\n      assert_equal nil, res[\"data\"][\"node\"]\n      assert_equal [nil], log\n    end\n\n    def test_it_uses_type_error_hooks_for_failed_type_resolution\n      log = []\n      query_string = %|\n      {\n        node(id: \"2003\") {\n          __typename\n        }\n      }|\n\n      assert_raises(GraphQL::UnresolvedTypeError) {\n        execute_query(query_string, context: { type_errors: log })\n      }\n\n      assert_equal [SpecificationSchema::BOGUS_NODE], log\n    end\n\n    def test_it_treats_failed_type_resolution_like_nil\n      log = []\n      ctx = { type_errors: log, gobble: true }\n      query_string = %|\n      {\n        node(id: \"2003\") {\n          __typename\n        }\n      }|\n\n      res = execute_query(query_string, context: ctx)\n\n      assert_equal nil, res[\"data\"][\"node\"]\n      assert_equal false, res.key?(\"errors\")\n      assert_equal [SpecificationSchema::BOGUS_NODE], log\n\n      query_string_2 = %|\n      {\n        requiredNode(id: \"2003\") {\n          __typename\n        }\n      }|\n\n      res = execute_query(query_string_2, context: ctx)\n\n      assert_equal nil, res[\"data\"]\n      assert_equal false, res.key?(\"errors\")\n      assert_equal [SpecificationSchema::BOGUS_NODE, SpecificationSchema::BOGUS_NODE], log\n    end\n\n    def test_it_skips_connections\n      query_type = GraphQL::ObjectType.define do\n        name \"Query\"\n        connection :skipped, types[query_type], resolve: ->(o,a,c) { c.skip }\n      end\n      schema = GraphQL::Schema.define(query: query_type)\n      res = schema.execute(\"{ skipped { __typename } }\")\n      assert_equal({\"data\" => nil}, res)\n    end\n  end\nend\n"