From a8db461a71fdcadb5ed58712d6c87bb7f63c3d30 Mon Sep 17 00:00:00 2001 From: Jonathan Baker Date: Tue, 8 Jun 2021 14:27:07 -0400 Subject: [PATCH] Move arg helpers to WithArgs module. --- lib/graphql_schema.rb | 16 ++++++++-------- test/graphql_schema_test.rb | 4 +++- test/support/schema.rb | 1 + 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/graphql_schema.rb b/lib/graphql_schema.rb index bb61d07..9532517 100644 --- a/lib/graphql_schema.rb +++ b/lib/graphql_schema.rb @@ -38,6 +38,14 @@ module WithArgs def args @args ||= @hash.fetch('args').map{ |arg_hash| InputValue.new(arg_hash) } end + + def required_args + @required_args ||= args.select{ |arg| arg.type.non_null? } + end + + def optional_args + @optional_args ||= args.reject{ |arg| arg.type.non_null? } + end end module NamedHash @@ -111,14 +119,6 @@ def initialize(field_hash) @hash = field_hash end - def required_args - @required_args ||= args.select{ |arg| arg.type.non_null? } - end - - def optional_args - @optional_args ||= args.reject{ |arg| arg.type.non_null? } - end - def type @type ||= TypeDeclaration.new(@hash.fetch('type')) end diff --git a/test/graphql_schema_test.rb b/test/graphql_schema_test.rb index 67c01fe..46c8b10 100644 --- a/test/graphql_schema_test.rb +++ b/test/graphql_schema_test.rb @@ -164,7 +164,9 @@ def test_description def test_directives example_directive = directive("directiveExample") - assert_equal %w(input), example_directive.args.map(&:name) + assert_equal %w(input enabled), example_directive.args.map(&:name) + assert_equal %w(input), example_directive.required_args.map(&:name) + assert_equal %w(enabled), example_directive.optional_args.map(&:name) assert_equal "A nice runtime customization", example_directive.description assert_equal ["FIELD"], example_directive.locations refute example_directive.builtin? diff --git a/test/support/schema.rb b/test/support/schema.rb index 7acc392..c66bb9c 100644 --- a/test/support/schema.rb +++ b/test/support/schema.rb @@ -102,6 +102,7 @@ class DirectiveExample < GraphQL::Schema::Directive description "A nice runtime customization" locations FIELD argument :input, !SetIntegerInput, required: true + argument :enabled, Boolean, required: false end ExampleSchema = GraphQL::Schema.define do