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 support for custom GraphQL scalar coercion #1659

Merged
merged 3 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 5 additions & 1 deletion lib/tapioca/dsl/helpers/graphql_type_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def type_for(argument)
type_for_constant(Float)
when GraphQL::Types::ID.singleton_class, GraphQL::Types::String.singleton_class
type_for_constant(String)
when GraphQL::Types::Int.singleton_class
when GraphQL::Types::Int.singleton_class, GraphQL::Types::BigInt.singleton_class
type_for_constant(Integer)
when GraphQL::Types::ISO8601Date.singleton_class
type_for_constant(Date)
Expand All @@ -45,6 +45,10 @@ def type_for(argument)
else
"T.any(#{value_types.join(", ")})"
end
when GraphQL::Schema::Scalar.singleton_class
method = Runtime::Reflection.method_of(unwrapped_type, :coerce_input)
signature = Runtime::Reflection.signature_of(method)
signature&.return_type&.to_s || "T.untyped"
Copy link
Contributor Author

@nicoco007 nicoco007 Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default behaviour of Scalar is to just return the raw value so I think it makes more sense to return T.untyped by default here rather than the unwrapped_type. Happy to discuss!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this makes sense to me. However, this needs more finesse, since return_type can be Void or NotTyped, neither of which make sense as a parameter type.

We need to do something similar to

return_type = signature.return_type
return if return_type == T::Private::Types::Void || return_type == T::Private::Types::NotTyped

Copy link
Member

@paracycle paracycle Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I added this check and associated test cases in a commit, and will merge when the CI is green.

when GraphQL::Schema::InputObject.singleton_class
type_for_constant(unwrapped_type)
when GraphQL::Schema::NonNull.singleton_class
Expand Down
45 changes: 42 additions & 3 deletions spec/tapioca/dsl/compilers/graphql_mutation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class CreateComment < GraphQL::Schema::Mutation
argument :float, Float, required: true
argument :id, ID, required: true
argument :int, Int, required: true
argument :big_int, GraphQL::Types::BigInt, required: true
argument :date, GraphQL::Types::ISO8601Date, required: true
argument :datetime, GraphQL::Types::ISO8601DateTime, required: true
argument :json, GraphQL::Types::JSON, required: true
Expand All @@ -153,7 +154,7 @@ class CreateComment < GraphQL::Schema::Mutation
argument :input_object, CreateCommentInput, required: true
argument :custom_scalar, CustomScalar, required: true

def resolve(boolean:, float:, id:, int:, date:, datetime:, json:, string:, enum_a:, enum_b:, input_object:, custom_scalar:)
def resolve(boolean:, float:, id:, int:, big_int:, date:, datetime:, json:, string:, enum_a:, enum_b:, input_object:, custom_scalar:)
# ...
end
end
Expand All @@ -163,8 +164,8 @@ def resolve(boolean:, float:, id:, int:, date:, datetime:, json:, string:, enum_
# typed: strong

class CreateComment
sig { params(boolean: T::Boolean, float: ::Float, id: ::String, int: ::Integer, date: ::Date, datetime: ::Time, json: T::Hash[::String, T.untyped], string: ::String, enum_a: ::String, enum_b: T.any(::String, ::Symbol), input_object: ::CreateCommentInput, custom_scalar: ::CustomScalar).returns(T.untyped) }
def resolve(boolean:, float:, id:, int:, date:, datetime:, json:, string:, enum_a:, enum_b:, input_object:, custom_scalar:); end
sig { params(boolean: T::Boolean, float: ::Float, id: ::String, int: ::Integer, big_int: ::Integer, date: ::Date, datetime: ::Time, json: T::Hash[::String, T.untyped], string: ::String, enum_a: ::String, enum_b: T.any(::String, ::Symbol), input_object: ::CreateCommentInput, custom_scalar: T.untyped).returns(T.untyped) }
def resolve(boolean:, float:, id:, int:, big_int:, date:, datetime:, json:, string:, enum_a:, enum_b:, input_object:, custom_scalar:); end
end
RBI

Expand Down Expand Up @@ -201,6 +202,44 @@ def resolve(loaded_argument:, loaded_arguments:, custom_name:, optional_loaded_a

assert_equal(expected, rbi_for(:CreateComment))
end

it "generates correct RBI for custom scalars with return types" do
add_ruby_file("create_comment.rb", <<~RUBY)
class CustomScalar; end

class CustomScalarType < GraphQL::Schema::Scalar
class << self
extend T::Sig

sig { params(value: T.untyped, context: GraphQL::Query::Context).returns(CustomScalar) }
def coerce_input(value, context)
CustomScalar.new
end
end
end

class CreateComment < GraphQL::Schema::Mutation
argument :custom_scalar, CustomScalarType, required: true
argument :custom_scalar_array, [CustomScalarType], required: true
argument :optional_custom_scalar, CustomScalarType, required: false

def resolve(custom_scalar:, custom_scalar_array:, optional_custom_scalar: nil)
# ...
end
end
RUBY

expected = <<~RBI
# typed: strong

class CreateComment
sig { params(custom_scalar: ::CustomScalar, custom_scalar_array: T::Array[::CustomScalar], optional_custom_scalar: T.nilable(::CustomScalar)).returns(T.untyped) }
def resolve(custom_scalar:, custom_scalar_array:, optional_custom_scalar: T.unsafe(nil)); end
end
RBI

assert_equal(expected, rbi_for(:CreateComment))
end
end
end
end
Expand Down