Class: RuboCop::Cop::Gusto::Graphql::IdInputArgumentDescription

Inherits:
Base
  • Object
show all
Includes:
IdDescriptionConcerns
Defined in:
lib/rubocop/cop/gusto/graphql/id_input_argument_description.rb

Overview

Ensures ID type arguments on mutations and input objects follow the standardized description template: "The identifier of the (<ObjectName.fieldName>) to " "The identifiers of each (<ObjectName.fieldName>) to " (for arrays)

This cop only applies to arguments on mutations and input objects, NOT on regular GraphQL objects/queries (see IdFieldArgumentDescription for those).

Examples:

Bad

class UpdateEmployeeMutation < BaseMutation
  argument :employee_id, ID, 'Employee ID', required: true
end

Good

class UpdateEmployeeMutation < BaseMutation
  argument :employee_id, ID, 'The identifier of the Employee (`Employee.id`) to update', required: true
end

class DeleteEmployeeInput < BaseInputObject
  argument :employee_ids, [ID], 'The identifiers of each Employee (`Employee.id`) to delete', required: true
end

Constant Summary collapse

MSG =
"ID argument description should match template: " \
"'The identifier of the <ObjectName> (`<ObjectName.fieldName>`) to <action>'."
RESTRICT_ON_SEND =
%i(argument).freeze
TYPE_REF =

Single: "The identifier of the (<ObjectName.fieldName>) to " Also supports multiple types joined by " or ": "The identifier of the Employee (Employee.id) or Contractor (Contractor.id) to " The action can be any verb phrase (update, delete, add items to, etc.)

/\w+ \(`\w+\.\w+`\)/
SINGLE_PATTERN =
/\AThe identifier of the #{TYPE_REF}(?: or #{TYPE_REF})* to \w+/
ARRAY_PATTERN =

Array: "The identifiers of each (<ObjectName.fieldName>) to "

/\AThe identifiers of each #{TYPE_REF}(?: or #{TYPE_REF})* to \w+/

Constants included from IdDescriptionConcerns

RuboCop::Cop::Gusto::Graphql::IdDescriptionConcerns::ARGUMENT_CALL_PATTERN, RuboCop::Cop::Gusto::Graphql::IdDescriptionConcerns::FIELD_CALL_PATTERN, RuboCop::Cop::Gusto::Graphql::IdDescriptionConcerns::MUTATION_OR_INPUT_BASE_SUFFIXES, RuboCop::Cop::Gusto::Graphql::IdDescriptionConcerns::PRIMARY_ID_FIELDS

Instance Method Summary collapse

Methods included from IdDescriptionConcerns

#argument_call?, #field_call?

Instance Method Details

#on_send(node) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubocop/cop/gusto/graphql/id_input_argument_description.rb', line 48

def on_send(node)
  # Only check if we're in a mutation or input object context
  return unless in_mutation_or_input_object?(node)

  argument_call?(node) do |_name, type_node|
    is_array = array_id_type?(type_node)
    return unless id_type?(type_node) || is_array

    desc = extract_description(node)
    return if desc.nil? # No description - let other cops handle missing descriptions
    return if valid_argument_description?(desc, is_array)

    add_offense(node)
  end
end