graphql-constraint-directive 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -1
- data/README.md +3 -3
- data/lib/graphql/constraint/directive/constraint.rb +19 -7
- data/lib/graphql/constraint/directive/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba3f526ee2124471f6dfc59db75677018871fb2166a056def33c4436ac77bf56
|
4
|
+
data.tar.gz: bcf163565e80cbc79b26ca429d540c7b0949281fe7fe2b86b5af3136b0b71932
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 917131726cb5381b676bd09c4c8e30a87a64b6bbc7674304396fd7da14979a7c6ee5243516d2a757f2bbd16809925c73eb083f027e9764f607f8666ab448826d
|
7
|
+
data.tar.gz: 4f91c444e0013e31250d3cb431f745b9fc3e947063c706739ab86915a19009fcd3b459e417bf4ba4df6de3f7a5a3ba439a81b64f58354cfacd4c981530089a39
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# GraphQL::Constraint::Directive
|
2
2
|
|
3
3
|
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/graphql/ruby/constraint/directive`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
4
|
|
@@ -8,11 +8,11 @@ TODO: Delete this and the text above, and describe your gem
|
|
8
8
|
|
9
9
|
Install the gem and add to the application's Gemfile by executing:
|
10
10
|
|
11
|
-
$ bundle add graphql-
|
11
|
+
$ bundle add graphql-constraint-directive
|
12
12
|
|
13
13
|
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
14
|
|
15
|
-
$ gem install graphql-
|
15
|
+
$ gem install graphql-constraint-directive
|
16
16
|
|
17
17
|
## Usage
|
18
18
|
|
@@ -1,7 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "graphql"
|
4
|
-
require "byebug"
|
5
4
|
|
6
5
|
module GraphQL
|
7
6
|
module Constraint
|
@@ -10,21 +9,34 @@ module GraphQL
|
|
10
9
|
LENGTH = { min_length: :minimum, max_length: :maximum }.freeze
|
11
10
|
|
12
11
|
description "validate GraphQL fields"
|
13
|
-
argument :max_length, Int, required: false, description: "validate max length"
|
14
|
-
argument :min_length, Int, required: false, description: "validate min length"
|
12
|
+
argument :max_length, Int, required: false, description: "validate max length for String"
|
13
|
+
argument :min_length, Int, required: false, description: "validate min length for String"
|
15
14
|
locations INPUT_FIELD_DEFINITION
|
16
15
|
|
17
16
|
def initialize(owner, **arguments)
|
18
17
|
super
|
19
|
-
validates
|
20
|
-
owner.validates(validates)
|
18
|
+
owner.validates(validation_config)
|
21
19
|
end
|
22
20
|
|
23
|
-
|
21
|
+
private
|
22
|
+
|
23
|
+
def validation_config
|
24
24
|
{
|
25
|
-
length:
|
25
|
+
length: length_config
|
26
26
|
}
|
27
27
|
end
|
28
|
+
|
29
|
+
def length_config
|
30
|
+
filtered_args = arguments.keyword_arguments.filter { |key, _| LENGTH.key?(key) }
|
31
|
+
|
32
|
+
if owner.type != GraphQL::Types::String
|
33
|
+
raise ArgumentError, <<~MD
|
34
|
+
#{filtered_args.keys.join(", ")} in @constraint can't be attached to #{owner.graphql_name} because it has to be a String type.
|
35
|
+
MD
|
36
|
+
end
|
37
|
+
|
38
|
+
filtered_args.transform_keys { |key| LENGTH[key] }
|
39
|
+
end
|
28
40
|
end
|
29
41
|
end
|
30
42
|
end
|