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
-
.build_suite {|query_string| ... } ⇒ Class<Minitest::Test>
A test suite for this parse function.
Class Method Details
.build_suite {|query_string| ... } ⇒ Class<Minitest::Test>
Returns A test suite for this parse function
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 |
# File 'lib/graphql/compatibility/query_parser_specification.rb', line 13 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"] blockString: """ Hello, World """ ) } | 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] block_str_value = inputs[12].value assert_equal "Hello,\n World", block_str_value end def test_it_doesnt_parse_nonsense_variables query_string_1 = "query Vars($var1) { cheese(id: $var1) { flavor } }" query_string_2 = "query Vars2($var1: Int = $var1) { cheese(id: $var1) { flavor } }" err_1 = assert_raises(GraphQL::ParseError) do parse(query_string_1) end assert_equal [1,17], [err_1.line, err_1.col] err_2 = assert_raises(GraphQL::ParseError) do parse(query_string_2) end assert_equal [1,26], [err_2.line, err_2.col] end def test_enum_value_definitions_have_a_position document = parse(""" enum Enum { VALUE } """) assert_equal [3, 17], document.definitions[0].values[0].position end def test_field_definitions_have_a_position document = parse(""" type A { field: String } """) assert_equal [3, 17], document.definitions[0].fields[0].position end def test_input_value_definitions_have_a_position document = parse(""" input A { field: String } """) assert_equal [3, 17], document.definitions[0].fields[0].position end def test_parses_when_there_are_no_interfaces schema = " type A { a: String } " document = parse(schema) assert_equal [], document.definitions[0].interfaces.map(&:name) end def test_parses_implements_with_leading_ampersand schema = " type A implements & B { a: String } " document = parse(schema) assert_equal ["B"], document.definitions[0].interfaces.map(&:name) assert_equal [2, 35], document.definitions[0].interfaces[0].position end def test_parses_implements_with_leading_ampersand_and_multiple_interfaces schema = " type A implements & B & C { a: String } " document = parse(schema) assert_equal ["B", "C"], document.definitions[0].interfaces.map(&:name) assert_equal [2, 35], document.definitions[0].interfaces[0].position assert_equal [2, 39], document.definitions[0].interfaces[1].position end def test_parses_implements_without_leading_ampersand schema = " type A implements B { a: String } " document = parse(schema) assert_equal ["B"], document.definitions[0].interfaces.map(&:name) assert_equal [2, 33], document.definitions[0].interfaces[0].position end def test_parses_implements_without_leading_ampersand_and_multiple_interfaces schema = " type A implements B & C { a: String } " document = parse(schema) assert_equal ["B", "C"], document.definitions[0].interfaces.map(&:name) assert_equal [2, 33], document.definitions[0].interfaces[0].position assert_equal [2, 37], document.definitions[0].interfaces[1].position end def test_supports_old_syntax_for_parsing_multiple_interfaces schema = " type A implements B, C { a: String } " document = parse(schema) assert_equal ["B", "C"], document.definitions[0].interfaces.map(&:name) assert_equal [2, 33], document.definitions[0].interfaces[0].position assert_equal [2, 36], document.definitions[0].interfaces[1].position end end end |