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 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
12 changes: 11 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,16 @@ 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)
return_type = signature&.return_type

if return_type && !(T::Private::Types::Void === return_type || T::Private::Types::NotTyped === return_type)
return_type.to_s
else
"T.untyped"
end
when GraphQL::Schema::InputObject.singleton_class
type_for_constant(unwrapped_type)
when GraphQL::Schema::NonNull.singleton_class
Expand Down
64 changes: 61 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,63 @@ 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 BrokenScalarType < GraphQL::Schema::Scalar
class << self
extend T::Sig

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

class NoSigScalarType < GraphQL::Schema::Scalar
class << self
def coerce_input(value, context)
end
end
end

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

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

expected = <<~RBI
# typed: strong

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

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