Sha256: e18c06aa8b5806ba536c8fa90b4d6c0d489bd172aa8c6aeee6e095b8cda9a6f2

Contents?: true

Size: 1.93 KB

Versions: 1

Compression:

Stored size: 1.93 KB

Contents

# typed: strict
# frozen_string_literal: true

begin
  require "graphql"
rescue LoadError
  return
end

require "tapioca/dsl/helpers/graphql_type_helper"

module Tapioca
  module Dsl
    module Compilers
      # `Tapioca::Dsl::Compilers::GraphqlInputObject` generates RBI files for subclasses of
      # [`GraphQL::Schema::InputObject`](https://graphql-ruby.org/api-doc/2.0.11/GraphQL/Schema/InputObject).
      #
      # For example, with the following `GraphQL::Schema::InputObject` subclass:
      #
      # ~~~rb
      # class CreateCommentInput < GraphQL::Schema::InputObject
      #   argument :body, String, required: true
      #   argument :post_id, ID, required: true
      # end
      # ~~~
      #
      # this compiler will produce the RBI file `notify_user_job.rbi` with the following content:
      #
      # ~~~rbi
      # # create_comment.rbi
      # # typed: true
      # class CreateCommentInput
      #   sig { returns(String) }
      #   def body; end
      #
      #   sig { returns(String) }
      #   def post_id; end
      # end
      # ~~~
      class GraphqlInputObject < Compiler
        extend T::Sig

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

        sig { override.void }
        def decorate
          arguments = constant.all_argument_definitions
          return if arguments.empty?

          graphql_type_helper = Helpers::GraphqlTypeHelper.new

          root.create_path(constant) do |input_object|
            arguments.each do |argument|
              name = argument.keyword.to_s
              input_object.create_method(name, return_type: graphql_type_helper.type_for(argument.type))
            end
          end
        end

        class << self
          extend T::Sig

          sig { override.returns(T::Enumerable[Module]) }
          def gather_constants
            all_classes.select { |c| c < GraphQL::Schema::InputObject }
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tapioca-0.10.0 lib/tapioca/dsl/compilers/graphql_input_object.rb