Skip to content

Commit

Permalink
Handle arguments with loads in Graphql mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
such committed Jul 7, 2023
1 parent c83b87d commit 5f63dfd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lib/tapioca/dsl/compilers/graphql_mutation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module Compilers
class GraphqlMutation < Compiler
extend T::Sig

ConstantType = type_member { { fixed: T.class_of(GraphQL::Schema::InputObject) } }
ConstantType = type_member { { fixed: T.class_of(GraphQL::Schema::Mutation) } }

sig { override.void }
def decorate
Expand All @@ -59,7 +59,11 @@ def decorate
params = compile_method_parameters_to_rbi(method_def).map do |param|
name = param.param.name
argument = arguments_by_name.fetch(name, nil)
create_typed_param(param.param, argument ? Helpers::GraphqlTypeHelper.type_for(argument.type) : "T.untyped")
type = argument.loads ? GraphQL::Schema::Wrapper.new(argument.loads) : argument.type if argument
create_typed_param(
param.param,
argument ? Helpers::GraphqlTypeHelper.type_for(type) : "T.untyped",
)
end

root.create_path(constant) do |mutation|
Expand Down
11 changes: 8 additions & 3 deletions spec/tapioca/dsl/compilers/graphql_mutation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ def resolve(body:, post_id:)

it "generates correct RBI for all graphql types" do
add_ruby_file("create_comment.rb", <<~RUBY)
class LoadedType < GraphQL::Schema::Object
field "foo", type: String
end
class EnumA < GraphQL::Schema::Enum
value "foo"
end
Expand Down Expand Up @@ -127,8 +131,9 @@ class CreateComment < GraphQL::Schema::Mutation
argument :enum_b, EnumB, required: true
argument :input_object, CreateCommentInput, required: true
argument :custom_scalar, CustomScalar, required: true
argument :loaded_argument, ID, required: true, loads: LoadedType
def resolve(boolean:, float:, id:, int:, date:, datetime:, json:, string:, enum_a:, enum_b:, input_object:, custom_scalar:)
def resolve(boolean:, float:, id:, int:, date:, datetime:, json:, string:, enum_a:, enum_b:, input_object:, custom_scalar:, loaded_argument:)
# ...
end
end
Expand All @@ -138,8 +143,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: T.untyped).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, 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, loaded_argument: T.untyped).returns(T.untyped) }
def resolve(boolean:, float:, id:, int:, date:, datetime:, json:, string:, enum_a:, enum_b:, input_object:, custom_scalar:, loaded_argument:); end
end
RBI

Expand Down

0 comments on commit 5f63dfd

Please sign in to comment.