Sha256: 4202c9cff79905b9dd52b91cb8076cb3448280433aceec57f1e9d4ebfd68247e

Contents?: true

Size: 1.7 KB

Versions: 16

Compression:

Stored size: 1.7 KB

Contents

module GraphQL::Models
  module MutationHelpers
    def self.print_input_fields(field_map, definer, map_name_prefix)
      definer.instance_exec do
        field_map.fields.each do |f|
          field_type = f[:type]

          if f[:required] && !field_map.leave_null_unchanged?
            field_type = field_type.to_non_null_type
          end

          input_field(f[:name], field_type)
        end

        if field_map.leave_null_unchanged?
          field_names = field_map.fields.select { |f| !f[:required] }.map { |f| f[:name] }
          field_names += field_map.nested_maps.reject(&:required).map { |m| m.name }
          field_names = field_names.sort_by { |s| s }

          unless field_names.empty?
            enum = GraphQL::EnumType.define do
              name "#{map_name_prefix}UnsettableFields"
              field_names.each { |n| value(n, n.titleize, value: n) }
            end

            input_field('unsetFields', types[!enum])
          end
        end
      end

      # Build the input types for the nested input maps
      field_map.nested_maps.each do |child_map|
        type = build_input_type(child_map, "#{map_name_prefix}#{child_map.name.to_s.classify}")

        if child_map.has_many
          type = type.to_non_null_type.to_list_type
        end

        if child_map.required && !field_map.leave_null_unchanged?
          type = type.to_non_null_type
        end

        definer.instance_exec do
          input_field(child_map.name, type)
        end
      end
    end

    def self.build_input_type(field_map, name)
      type = GraphQL::InputObjectType.define do
        name(name)
        GraphQL::Models::MutationHelpers.print_input_fields(field_map, self, name)
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
graphql-activerecord-0.10.0 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.10.0.pre.alpha3 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.10.0.pre.alpha2 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.10.0.pre.alpha1 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.9.1 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.9.0 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.8.0 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.7.3 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.7.2 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.8.0.pre.alpha1 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.7.1 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.7.0 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.6.7 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.6.6 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.6.5 lib/graphql/models/mutation_helpers/print_input_fields.rb
graphql-activerecord-0.6.4 lib/graphql/models/mutation_helpers/print_input_fields.rb