Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more verbose data type for parameter method instead of using ol… #9

Open
wants to merge 2 commits into
base: support-1.9
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?`
Expand Down
12 changes: 6 additions & 6 deletions lib/graphql/sugar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions lib/graphql/sugar/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ 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.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
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
Expand Down