Module: GraphQL::Compatibility::QueryParserSpecification

Defined in:
lib/graphql/compatibility/query_parser_specification.rb,
lib/graphql/compatibility/query_parser_specification/query_assertions.rb,
lib/graphql/compatibility/query_parser_specification/parse_error_specification.rb

Overview

This asserts that a given parse function turns a string into the proper tree of {GraphQL{GraphQL::Language{GraphQL::Language::Nodes}.

Defined Under Namespace

Modules: ParseErrorSpecification, QueryAssertions

Constant Summary collapse

QUERY_STRING =
%|
      query getStuff($someVar: Int = 1, $anotherVar: [String!] ) @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: true)
        }

        ... on OtherType @include(unless: false){
          field(arg: [{key: "value", anotherKey: 0.9, anotherAnotherKey: WHATEVER}])
          anotherField
        }

        ... {
          id
        }
      }

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

Class Method Summary collapse

Class Method Details

.build_suite {|query_string| ... } ⇒ Class<Minitest::Test>

Returns A test suite for this parse function

Yield Parameters:

  • query_string (String)

    A query string to parse

Yield Returns:

Returns:

  • (Class<Minitest::Test>)

    A test suite for this parse function


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

def self.build_suite(&block)
  Class.new(Minitest::Test) do
    include QueryAssertions
    include ParseErrorSpecification

    @@parse_fn = block

    def parse(query_string)
      @@parse_fn.call(query_string)
    end

    def test_it_parses_queries
      document = parse(QUERY_STRING)
      query = document.definitions.first
      assert_valid_query(query)
      assert_valid_fragment(document.definitions.last)
      assert_valid_variable(query.variables.first)
      field = query.selections.first
      assert_valid_field(field)
      assert_valid_variable_argument(field.arguments.first)
      assert_valid_literal_argument(field.arguments.last)
      assert_valid_directive(field.directives.first)
      fragment_spread = query.selections[1].selections.last
      assert_valid_fragment_spread(fragment_spread)
      assert_valid_typed_inline_fragment(query.selections[2])
      assert_valid_typeless_inline_fragment(query.selections[3])
    end

    def test_it_parses_unnamed_queries
      document = parse("{ name, age, height }")
      operation =  document.definitions.first
      assert_equal 1, document.definitions.length
      assert_equal "query", operation.operation_type
      assert_equal nil, operation.name
      assert_equal 3, operation.selections.length
    end

    def test_it_parses_the_introspection_query
      parse(GraphQL::Introspection::INTROSPECTION_QUERY)
    end

    def test_it_parses_inputs
      query_string = %|
        {
          field(
            int: 3,
            float: 4.7e-24,
            bool: false,
            string: "☀︎🏆 \\b \\f \\n \\r \\t \\" \u00b6 \\u00b6 / \\/",
            enum: ENUM_NAME,
            array: [7, 8, 9]
            object: {a: [1,2,3], b: {c: "4"}}
            unicode_bom: "\xef\xbb\xbfquery"
            keywordEnum: on
            nullValue: null
            nullValueInObject: {a: null, b: "b"}
            nullValueInArray: ["a", null, "b"]
          )
        }
      |
      document = parse(query_string)
      inputs = document.definitions.first.selections.first.arguments
      assert_equal 3, inputs[0].value, "Integers"
      assert_equal 0.47e-23, inputs[1].value, "Floats"
      assert_equal false, inputs[2].value, "Booleans"
      assert_equal %|☀︎🏆 \b \f \n \r \t " ¶ ¶ / /|, inputs[3].value, "Strings"
      assert_instance_of GraphQL::Language::Nodes::Enum, inputs[4].value
      assert_equal "ENUM_NAME", inputs[4].value.name, "Enums"
      assert_equal [7,8,9], inputs[5].value, "Lists"

      obj = inputs[6].value
      assert_equal "a", obj.arguments[0].name
      assert_equal [1,2,3], obj.arguments[0].value
      assert_equal "b", obj.arguments[1].name
      assert_equal "c", obj.arguments[1].value.arguments[0].name
      assert_equal "4", obj.arguments[1].value.arguments[0].value

      assert_equal %|\xef\xbb\xbfquery|, inputs[7].value, "Unicode BOM"
      assert_equal "on", inputs[8].value.name, "Enum value 'on'"

      assert_instance_of GraphQL::Language::Nodes::NullValue, inputs[9].value

      args = inputs[10].value.arguments
      assert_instance_of GraphQL::Language::Nodes::NullValue, args.find{ |arg| arg.name == 'a' }.value
      assert_equal 'b', args.find{ |arg| arg.name == 'b' }.value

      values = inputs[11].value
      assert_equal 'a', values[0]
      assert_instance_of GraphQL::Language::Nodes::NullValue, values[1]
      assert_equal 'b', values[2]
    end
  end
end