From 48979766297658c901d479f2ad255a976cc1a5ca Mon Sep 17 00:00:00 2001 From: Aditya Kapoor Date: Fri, 30 Aug 2019 14:15:15 +0530 Subject: [PATCH 1/2] Add more verbose data type for `parameter` method instead of using old `types.*` data types. --- CHANGELOG.md | 8 ++++++++ lib/graphql/sugar.rb | 12 ++++++------ lib/graphql/sugar/function.rb | 8 ++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b12fcc7..cba7844 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +## [0.1.8] - 2019-08-30 +### Enhancement +- Move over to the more verbose Rails style of defining data type for `parameter`. Use `:string`, `:float`, `:decimal`, `:boolean` and `:datetime` instead of old style of using `types.String`, `types.Float`, etc. + +## [0.1.7] - 2019-08-27 +### Enhancement +- Add automatic type detection for `GraphQL::Sugar::Object` to calculate the trivial classes. + ## [0.1.6] - 2018-05-18 ### Fixed - Support for MySQL by conditionally checking PostgreSQL-specific `array?` diff --git a/lib/graphql/sugar.rb b/lib/graphql/sugar.rb index 427de04..daf39e0 100644 --- a/lib/graphql/sugar.rb +++ b/lib/graphql/sugar.rb @@ -4,12 +4,12 @@ module GraphQL module Sugar GRAPHQL_TYPE_MAPPING = { - integer: GraphQL::INT_TYPE, - float: GraphQL::FLOAT_TYPE, - decimal: GraphQL::FLOAT_TYPE, - boolean: GraphQL::BOOLEAN_TYPE, - string: GraphQL::STRING_TYPE, - datetime: GraphQL::DATETIME_TYPE + integer: GraphQL::INT_TYPE, + float: GraphQL::FLOAT_TYPE, + decimal: GraphQL::FLOAT_TYPE, + boolean: GraphQL::BOOLEAN_TYPE, + string: GraphQL::STRING_TYPE, + datetime: GraphQL::DATETIME_TYPE, }.freeze def self.get_resolver_graphql_type(field_name) diff --git a/lib/graphql/sugar/function.rb b/lib/graphql/sugar/function.rb index 757d65e..ca039fd 100644 --- a/lib/graphql/sugar/function.rb +++ b/lib/graphql/sugar/function.rb @@ -16,6 +16,14 @@ module ClassMethods # However, extended Field DSL (specified using `GraphQL::Field.accepts_definitions(...)`) # is not available within Functions. Therefore, re-defining it here. def parameter(name, *args, **kwargs, &block) + computed_type = GRAPHQL_TYPE_MAPPING[args.first] + computed_type = GraphQL::ID_TYPE if name == :id + + null_argument = !!!kwargs.delete(:null) # reverting nil to false and then to true + required_argument = !!kwargs.delete(:required) # reverting nil to false + computed_type = computed_type.to_non_null_type if required_argument || null_argument + + args[0] = computed_type kwargs[:as] ||= name.to_s.underscore.to_sym argument(name, *args, **kwargs, &block) end From 60941412b1b746833cbdb61195e53a1283423a85 Mon Sep 17 00:00:00 2001 From: Aditya Kapoor Date: Mon, 9 Sep 2019 12:52:26 +0530 Subject: [PATCH 2/2] Consider class constants too. --- lib/graphql/sugar/function.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/graphql/sugar/function.rb b/lib/graphql/sugar/function.rb index ca039fd..58cc7e2 100644 --- a/lib/graphql/sugar/function.rb +++ b/lib/graphql/sugar/function.rb @@ -16,7 +16,8 @@ module ClassMethods # However, extended Field DSL (specified using `GraphQL::Field.accepts_definitions(...)`) # is not available within Functions. Therefore, re-defining it here. def parameter(name, *args, **kwargs, &block) - computed_type = GRAPHQL_TYPE_MAPPING[args.first] + computed_type = GraphQL.const_get("#{args.first.to_s.upcase}_TYPE") + computed_type = GRAPHQL_TYPE_MAPPING[args.first] if args.first.is_a? Symbol computed_type = GraphQL::ID_TYPE if name == :id null_argument = !!!kwargs.delete(:null) # reverting nil to false and then to true