File: required_arguments_are_present_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 (98 lines) | stat: -rw-r--r-- 2,775 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
# frozen_string_literal: true
require "spec_helper"

describe GraphQL::StaticValidation::RequiredArgumentsArePresent do
  include StaticValidationHelpers
  let(:query_string) {"
    query getCheese {
      okCheese: cheese(id: 1) { ...cheeseFields }
      cheese { source }
    }

    fragment cheeseFields on Cheese {
      similarCheese { __typename }
      flavor @include(if: true)
      id @skip
    }
  "}

  it "finds undefined arguments to fields and directives" do
    assert_equal(3, errors.length)

    query_root_error = {
      "message"=>"Field 'cheese' is missing required arguments: id",
      "locations"=>[{"line"=>4, "column"=>7}],
      "path"=>["query getCheese", "cheese"],
      "extensions"=>{"code"=>"missingRequiredArguments", "className"=>"Field", "name"=>"cheese", "arguments"=>"id"}
    }
    assert_includes(errors, query_root_error)

    fragment_error = {
      "message"=>"Field 'similarCheese' is missing required arguments: source",
      "locations"=>[{"line"=>8, "column"=>7}],
      "path"=>["fragment cheeseFields", "similarCheese"],
      "extensions"=>{"code"=>"missingRequiredArguments", "className"=>"Field", "name"=>"similarCheese", "arguments"=>"source"}
    }
    assert_includes(errors, fragment_error)

    directive_error = {
      "message"=>"Directive 'skip' is missing required arguments: if",
      "locations"=>[{"line"=>10, "column"=>10}],
      "path"=>["fragment cheeseFields", "id"],
      "extensions"=>{"code"=>"missingRequiredArguments", "className"=>"Directive", "name"=>"skip", "arguments"=>"if"}
    }
    assert_includes(errors, directive_error)
  end

  describe "dynamic fields" do
    let(:query_string) {"
      query {
        __type { name }
      }
    "}

    it "finds undefined required arguments" do
      expected_errors = [
        {
          "message"=>"Field '__type' is missing required arguments: name",
          "locations"=>[
            {"line"=>3, "column"=>9}
          ],
          "path"=>["query", "__type"],
          "extensions"=>{
            "code"=>"missingRequiredArguments",
            "className"=>"Field",
            "name"=>"__type",
            "arguments"=>"name"
          }
        }
      ]
      assert_equal(expected_errors, errors)
    end
  end

  describe "when a required arg is hidden" do
    class Query < GraphQL::Schema::Object
      field :int, Integer do
        argument :input, Integer do
          def visible?(*)
            false
          end
        end
      end

      def int(input: -1)
        input
      end
    end

    class HiddenArgSchema < GraphQL::Schema
      query(Query)
    end

    it "Doesn't require a hidden input" do
      res = HiddenArgSchema.execute("{ int }")
      assert_equal(-1, res["data"]["int"])
    end
  end
end