graphql-constraint-directive 0.1.0 → 0.1.2
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 +23 -6
- 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: 46bdc345b3060ac6b9c965350a9c5f9928246bdeb1805975228f9f42f69f87e4
|
4
|
+
data.tar.gz: af01415fd405937df061bd4dcc47a2f8a97c34f7d9b50de4e6446426940dc52b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6955147fb378004d1887725a8872b66b8a6fbbb94e7d45c0c4b948c2a3e58763299690b1367572b05f189b6a35f3dc6971e58b831337a2d65324ce91b0ecf690
|
7
|
+
data.tar.gz: 453cf6b76b66e6b4fa2830f39cade3186737177b09102fa61a3ec235af8be098af4dfdd5a3baf6e25637afa7e84ae39999f2fb3cf3a43d2ff97ce9d19088c0ec
|
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
|
|
@@ -10,21 +10,38 @@ module GraphQL
|
|
10
10
|
LENGTH = { min_length: :minimum, max_length: :maximum }.freeze
|
11
11
|
|
12
12
|
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"
|
13
|
+
argument :max_length, Int, required: false, description: "validate max length for String"
|
14
|
+
argument :min_length, Int, required: false, description: "validate min length for String"
|
15
15
|
locations INPUT_FIELD_DEFINITION
|
16
16
|
|
17
17
|
def initialize(owner, **arguments)
|
18
18
|
super
|
19
|
-
validates
|
20
|
-
owner.validates(validates)
|
19
|
+
owner.validates(validation_config)
|
21
20
|
end
|
22
21
|
|
23
|
-
|
22
|
+
private
|
23
|
+
|
24
|
+
def validation_config
|
24
25
|
{
|
25
|
-
length:
|
26
|
+
length: length_config
|
26
27
|
}
|
27
28
|
end
|
29
|
+
|
30
|
+
def length_config
|
31
|
+
filtered_args = arguments.keyword_arguments.filter { |key, _| LENGTH.key?(key) }
|
32
|
+
|
33
|
+
if owner_type != GraphQL::Types::String
|
34
|
+
raise ArgumentError, <<~MD
|
35
|
+
#{filtered_args.keys.join(", ")} in @constraint can't be attached to #{owner.graphql_name} because it has to be a String type.
|
36
|
+
MD
|
37
|
+
end
|
38
|
+
|
39
|
+
filtered_args.transform_keys { |key| LENGTH[key] }
|
40
|
+
end
|
41
|
+
|
42
|
+
def owner_type
|
43
|
+
owner.type.non_null? ? owner.type.of_type : owner.type
|
44
|
+
end
|
28
45
|
end
|
29
46
|
end
|
30
47
|
end
|