Sha256: 73d2299320e99764f07cab02c364a8bf3c3478ed5fb8e6763a6613752a53c463

Contents?: true

Size: 2 KB

Versions: 1

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true
module GraphQL
  class Schema
    class InputObject < GraphQL::Schema::Member
      extend GraphQL::Delegate

      def initialize(values, context:, defaults_used:)
        @arguments = self.class.arguments_class.new(values, context: context, defaults_used: defaults_used)
        @context = context
      end

      # A lot of methods work just like GraphQL::Arguments
      def_delegators :@arguments, :[], :key?, :to_h
      def_delegators :to_h, :keys, :values, :each, :any?

      class << self
        # @return [Class<GraphQL::Arguments>]
        attr_accessor :arguments_class

        def argument(*args)
          argument = GraphQL::Schema::Argument.new(*args)
          own_arguments << argument
          arg_name = argument.graphql_definition.name
          # Add a method access
          define_method(Member::BuildType.underscore(arg_name)) do
            @arguments.public_send(arg_name)
          end
        end

        def arguments
          all_arguments = own_arguments
          inherited_arguments = (superclass <= GraphQL::Schema::InputObject ? superclass.arguments : [])
          inherited_arguments.each do |inherited_a|
            if all_arguments.none? { |a| a.name == inherited_a.name }
              all_arguments << inherited_a
            end
          end
          all_arguments
        end

        def own_arguments
          @own_arguments ||= []
        end

        def to_graphql
          type_defn = GraphQL::InputObjectType.new
          type_defn.name = graphql_name
          type_defn.description = description
          arguments.each do |arg|
            type_defn.arguments[arg.graphql_definition.name] = arg.graphql_definition
          end
          # Make a reference to a classic-style Arguments class
          self.arguments_class = GraphQL::Query::Arguments.construct_arguments_class(type_defn)
          # But use this InputObject class at runtime
          type_defn.arguments_class = self
          type_defn
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-1.8.0.pre3 lib/graphql/schema/input_object.rb