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
|
# frozen_string_literal: true
module ValidatorHelpers
def self.included(child_class)
child_class.extend(ClassMethods)
end
class BlankString < String
def blank?
true
end
end
class NonBlankString < String
if method_defined?(:blank?)
undef :blank?
end
end
def build_schema(arg_type, validates_config)
schema = Class.new(GraphQL::Schema)
base_argument = Class.new(GraphQL::Schema::Argument) do
def initialize(*args, secret: false, **kwargs, &block)
super(*args, **kwargs, &block)
@secret = secret
end
def visible?(_ctx)
!@secret
end
end
base_field = Class.new(GraphQL::Schema::Field) do
argument_class(base_argument)
end
validated_input = Class.new(GraphQL::Schema::InputObject) do
graphql_name "ValidatedInput"
argument :a, arg_type, required: false
argument :b, arg_type, required: false
argument :c, arg_type, required: false
validates(validates_config)
def prepare
self
end
end
validated_resolver = Class.new(GraphQL::Schema::Resolver) do
graphql_name "ValidatedResolver"
argument :a, arg_type, required: false
argument :b, arg_type, required: false
argument :c, arg_type, required: false
validates(validates_config)
type(arg_type, null: true)
def resolve(a: 0, b: 0, c: 0)
a + b + c
end
end
validated_arg_resolver = Class.new(GraphQL::Schema::Resolver) do
graphql_name "ValidatedArgResolver"
argument :input, arg_type, required: false, validates: validates_config
type(String, null: false)
def resolve(input: :NO_INPUT)
input.to_s.upcase
end
end
query_type = Class.new(GraphQL::Schema::Object) do
graphql_name "Query"
field_class(base_field)
field :validated, arg_type do
argument :value, arg_type, required: false, validates: validates_config
end
def validated(value: nil)
value
end
field :multi_validated, arg_type, validates: validates_config do
argument :a, arg_type, required: false
argument :b, arg_type, required: false
argument :c, arg_type, required: false
argument :secret, arg_type, required: false, secret: true
argument :secret2, arg_type, required: false, secret: true
end
def multi_validated(a: 0, b: 0, c: 0)
a + b + c
end
field :validated_input, arg_type do
argument :input, validated_input
end
def validated_input(input:)
(input[:a] || 0) + (input[:b] || 0) + (input[:c] || 0)
end
field :validated_resolver, resolver: validated_resolver
field :validated_arg_resolver, resolver: validated_arg_resolver
field :list, [self], null: false
def list
[:a, :b, :c]
end
end
schema.query(query_type)
if ADD_WARDEN
schema.use(GraphQL::Schema::Warden)
else
schema.use(GraphQL::Schema::Visibility)
end
schema
end
module ClassMethods
def build_tests(validator_name, field_type, expectations)
expectations.each do |expectation|
name = expectation[:name] ? "#{expectation[:name]}: " : ""
it "#{name}#{validator_name} on #{field_type} works with #{expectation[:config]}" do
schema = build_schema(field_type, { validator_name => expectation[:config] })
expectation[:cases].each do |test_case|
result = schema.execute(test_case[:query], variables: test_case[:variables])
if !result["data"]
pp result
refute result["errors"].map { |e| e["message"] }, test_case[:query]
end
assert_equal test_case[:error_messages], (result["errors"] || []).map { |e| e["message"] }, test_case[:query]
if test_case[:result].nil?
assert_nil result["data"]["validated"], test_case[:query]
else
assert_equal test_case[:result], result["data"]["validated"], test_case[:query]
end
end
end
end
end
end
end
|