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 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
|
# frozen_string_literal: true
require "spec_helper"
require "uri"
describe GraphQL::StaticValidation::ArgumentLiteralsAreCompatible do
include StaticValidationHelpers
let(:query_string) {%|
query getCheese {
stringCheese: cheese(id: "aasdlkfj") { ...cheeseFields }
cheese(id: 1) { source @skip(if: "whatever") }
yakSource: searchDairy(product: [{source: COW, fatContent: 1.1}]) { __typename }
badSource: searchDairy(product: {source: 1.1}) { __typename }
missingSource: searchDairy(product: [{fatContent: 1.1}]) { __typename }
listCoerce: cheese(id: 1) { similarCheese(source: YAK) { __typename } }
missingInputField: searchDairy(product: [{source: YAK, wacky: 1}]) { __typename }
}
fragment cheeseFields on Cheese {
similarCheese(source: 4.5) { __typename }
}
|}
it "finds undefined or missing-required arguments to fields and directives" do
# `wacky` above is handled by ArgumentsAreDefined, missingSource is handled by RequiredInputObjectAttributesArePresent
# so only 4 are tested below
assert_equal(6, errors.length)
query_root_error = {
"message"=>"Argument 'id' on Field 'stringCheese' has an invalid value (\"aasdlkfj\"). Expected type 'Int!'.",
"locations"=>[{"line"=>3, "column"=>7}],
"path"=>["query getCheese", "stringCheese", "id"],
"extensions"=>{"code"=>"argumentLiteralsIncompatible", "typeName"=>"Field", "argumentName"=>"id"},
}
assert_includes(errors, query_root_error)
directive_error = {
"message"=>"Argument 'if' on Directive 'skip' has an invalid value (\"whatever\"). Expected type 'Boolean!'.",
"locations"=>[{"line"=>4, "column"=>30}],
"path"=>["query getCheese", "cheese", "source", "if"],
"extensions"=>{"code"=>"argumentLiteralsIncompatible", "typeName"=>"Directive", "argumentName"=>"if"},
}
assert_includes(errors, directive_error)
input_object_field_error = {
"message"=>"Argument 'source' on InputObject 'DairyProductInput' has an invalid value (1.1). Expected type 'DairyAnimal!'.",
"locations"=>[{"line"=>6, "column"=>39}],
"path"=>["query getCheese", "badSource", "product", 0, "source"],
"extensions"=>{"code"=>"argumentLiteralsIncompatible", "typeName"=>"InputObject", "argumentName"=>"source"},
}
assert_includes(errors, input_object_field_error)
fragment_error = {
"message"=>"Argument 'source' on Field 'similarCheese' has an invalid value (4.5). Expected type '[DairyAnimal!]!'.",
"locations"=>[{"line"=>13, "column"=>7}],
"path"=>["fragment cheeseFields", "similarCheese", "source"],
"extensions"=> {"code"=>"argumentLiteralsIncompatible", "typeName"=>"Field", "argumentName"=>"source"}
}
assert_includes(errors, fragment_error)
end
describe "using input objects for enums it adds an error" do
let(:query_string) { <<-GRAPHQL
{
yakSource: searchDairy(product: [{source: {a: 1, b: 2}, fatContent: 1.1}]) { __typename }
}
GRAPHQL
}
it "works" do
assert_equal 1, errors.length
end
end
describe "using enums for scalar arguments it adds an error" do
let(:query_string) { <<-GRAPHQL
{
cheese(id: I_AM_ENUM_VALUE) {
source
}
}
GRAPHQL
}
let(:enum_invalid_for_id_error) do
{
"message" => "Argument 'id' on Field 'cheese' has an invalid value (I_AM_ENUM_VALUE). Expected type 'Int!'.",
"locations" => [{ "line" => 2, "column" => 9 }],
"path"=> ["query", "cheese", "id"],
"extensions"=> { "code" => "argumentLiteralsIncompatible", "typeName" => "Field", "argumentName" => "id" }
}
end
it "works" do
assert_includes(errors, enum_invalid_for_id_error)
assert_equal 1, errors.length
end
end
describe "null value" do
describe "nullable arg" do
let(:schema) {
GraphQL::Schema.from_definition(%|
type Query {
field(arg: Int): Int
}
|)
}
let(:query_string) {%|
query {
field(arg: null)
}
|}
it "finds no errors" do
assert_equal [], errors
end
end
describe "non-nullable arg" do
let(:schema) {
GraphQL::Schema.from_definition(%|
type Query {
field(arg: Int!): Int
}
|)
}
let(:query_string) {%|
query {
field(arg: null)
}
|}
it "finds error" do
assert_equal [{
"message"=>"Argument 'arg' on Field 'field' has an invalid value (null). Expected type 'Int!'.",
"locations"=>[{"line"=>3, "column"=>11}],
"path"=>["query", "field", "arg"],
"extensions"=>{"code"=>"argumentLiteralsIncompatible", "typeName"=>"Field", "argumentName"=>"arg"}
}], errors
end
end
describe "non-nullable array" do
let(:schema) {
GraphQL::Schema.from_definition(%|
type Query {
field(arg: [Int!]): Int
}
|)
}
let(:query_string) {%|
query {
field(arg: [null])
}
|}
it "finds error" do
assert_equal [{
"message"=>"Argument 'arg' on Field 'field' has an invalid value ([null]). Expected type '[Int!]'.",
"locations"=>[{"line"=>3, "column"=>11}],
"path"=>["query", "field", "arg"],
"extensions"=>{"code"=>"argumentLiteralsIncompatible", "typeName"=>"Field", "argumentName"=>"arg"}
}], errors
end
end
describe "array with nullable values" do
let(:schema) {
GraphQL::Schema.from_definition(%|
type Query {
field(arg: [Int]): Int
}
|)
}
let(:query_string) {%|
query {
field(arg: [null])
}
|}
it "finds no errors" do
assert_equal [], errors
end
end
describe "input object" do
let(:schema) {
GraphQL::Schema.from_definition(%|
type Query {
field(arg: Input): Int
}
input Input {
a: Int
b: Int!
}
|)
}
let(:query_string) {%|
query {
field(arg: {a: null, b: null})
}
|}
it "it finds errors" do
assert_equal 1, errors.length
refute_includes errors, {
"message"=>"Argument 'arg' on Field 'field' has an invalid value ({a: null, b: null}). Expected type 'Input'.",
"locations"=>[{"line"=>3, "column"=>11}],
"path"=>["query", "field", "arg"],
"extensions"=>{"code"=>"argumentLiteralsIncompatible", "typeName"=>"Field", "argumentName"=>"arg"}
}
assert_includes errors, {
"message"=>"Argument 'b' on InputObject 'Input' has an invalid value (null). Expected type 'Int!'.",
"locations"=>[{"line"=>3, "column"=>22}],
"path"=>["query", "field", "arg", "b"],
"extensions"=>{"code"=>"argumentLiteralsIncompatible", "typeName"=>"InputObject", "argumentName"=>"b"}
}
end
end
end
describe "dynamic fields" do
let(:query_string) {"
query {
__type(name: 1) { name }
}
"}
it "finds invalid argument types" do
assert_includes(errors, {
"message"=>"Argument 'name' on Field '__type' has an invalid value (1). Expected type 'String!'.",
"locations"=>[{"line"=>3, "column"=>9}],
"path"=>["query", "__type", "name"],
"extensions"=>{"code"=>"argumentLiteralsIncompatible", "typeName"=>"Field", "argumentName"=>"name"}
})
end
end
describe "error references argument" do
let(:validator) { GraphQL::StaticValidation::Validator.new(schema: schema) }
let(:query) { GraphQL::Query.new(schema, query_string) }
let(:errors) { validator.validate(query)[:errors] }
let(:query_string) {"
query {
cheese(id: true) { source }
milk(id: 1) { source @skip(if: TRUE) }
}
"}
it "works with field" do
id_argument = schema.types['Query'].fields['cheese'].get_argument('id')
error = errors.find { |error| error.argument_name == 'id' }
assert_equal id_argument, error.argument
assert_equal true, error.value
end
it "works with directive" do
if_argument = schema.directives['skip'].get_argument('if')
error = errors.find { |error| error.argument_name == 'if' }
assert_equal if_argument, error.argument
assert_instance_of GraphQL::Language::Nodes::Enum, error.value
assert_equal "TRUE", error.value.name
end
end
class CustomErrorMessagesSchema < GraphQL::Schema
class TimeType < GraphQL::Schema::Scalar
description "Time since epoch in seconds"
def self.coerce_input(value, ctx)
Time.at(Float(value))
rescue ArgumentError
raise GraphQL::CoercionError, 'cannot coerce to Float'
end
def self.coerce_result(value, ctx)
value.to_f
end
end
class RangeType < GraphQL::Schema::InputObject
argument :from, TimeType
argument :to, TimeType
end
class EmailType < GraphQL::Schema::Scalar
def self.coerce_input(value, ctx)
if URI::MailTo::EMAIL_REGEXP.match(value)
value
else
raise GraphQL::CoercionError.new("Invalid email address", extensions: { "code" => "invalid_email_address" })
end
end
def self.coerce_result(value, ctx)
value.to_f
end
end
class Query < GraphQL::Schema::Object
description "The query root of this schema"
field :time, TimeType do
argument :value, TimeType, required: false
argument :range, RangeType, required: false
end
def time(value: nil, range: nil)
value
end
field :email, EmailType do
argument :value, EmailType, required: false
end
def email(value:)
value
end
end
query(Query)
end
describe "custom error messages" do
let(:schema) { CustomErrorMessagesSchema }
let(:query_string) {%|
query {
time(value: "a")
}
|}
describe "with a shallow coercion" do
it "sets error message from a CoercionError if raised" do
assert_equal 1, errors.length
assert_includes errors, {
"message"=> "cannot coerce to Float",
"locations"=>[{"line"=>3, "column"=>9}],
"path"=>["query", "time", "value"],
"extensions"=>{"code"=>"argumentLiteralsIncompatible", "typeName"=>"CoercionError"}
}
end
end
describe "with a deep coercion" do
let(:query_string) {%|
query {
time(range: { from: "a", to: "b" })
}
|}
from_error = {
"message"=>"cannot coerce to Float",
"locations"=>[{"line"=>3, "column"=>23}],
"path"=>["query", "time", "range", "from"],
"extensions"=>{"code"=>"argumentLiteralsIncompatible", "typeName"=>"CoercionError"},
}
to_error = {
"message"=>"cannot coerce to Float",
"locations"=>[{"line"=>3, "column"=>23}],
"path"=>["query", "time", "range", "to"],
"extensions"=>{"code"=>"argumentLiteralsIncompatible", "typeName"=>"CoercionError"},
}
bubbling_error = {
"message"=>"cannot coerce to Float",
"locations"=>[{"line"=>3, "column"=>11}],
"path"=>["query", "time", "range"],
"extensions"=>{"code"=>"argumentLiteralsIncompatible", "typeName"=>"CoercionError"},
}
describe "sets deep error message from a CoercionError if raised" do
it "works" do
assert_equal 2, errors.length
assert_includes(errors, from_error)
assert_includes(errors, to_error)
refute_includes(errors, bubbling_error)
end
end
end
end
describe "custom error extensions" do
let(:schema) { CustomErrorMessagesSchema }
let(:query_string) {%|
query {
email(value: "a")
}
|}
describe "with a shallow coercion" do
it "sets error extensions code from a CoercionError if raised" do
assert_equal 1, errors.length
assert_includes errors, {
"message"=> "Invalid email address",
"locations"=>[{"line"=>3, "column"=>9}],
"path"=>["query", "email", "value"],
"extensions"=>{"code"=>"invalid_email_address", "typeName"=>"CoercionError"}
}
end
end
end
end
|