Sha256: bd1f23e79a15932f85944111b77714ea8425072f6d2b50651e47db25f73679df

Contents?: true

Size: 1.36 KB

Versions: 75

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true
module GraphQL
  module StaticValidation
    class RequiredArgumentsArePresent
      include GraphQL::StaticValidation::Message::MessageHelper

      def validate(context)
        v = context.visitor
        v[GraphQL::Language::Nodes::Field] << ->(node, parent) { validate_field(node, context) }
        v[GraphQL::Language::Nodes::Directive] << ->(node, parent) { validate_directive(node, context) }
      end

      private

      def validate_directive(ast_directive, context)
        directive_defn = context.schema.directives[ast_directive.name]
        assert_required_args(ast_directive, directive_defn, context)
      end

      def validate_field(ast_field, context)
        defn = context.field_definition
        assert_required_args(ast_field, defn, context)
      end

      def assert_required_args(ast_node, defn, context)
        present_argument_names = ast_node.arguments.map(&:name)
        required_argument_names = defn.arguments.values
          .select { |a| a.type.kind.non_null? }
          .map(&:name)

        missing_names = required_argument_names - present_argument_names
        if missing_names.any?
          context.errors << message("#{ast_node.class.name.split("::").last} '#{ast_node.name}' is missing required arguments: #{missing_names.join(", ")}", ast_node, context: context)
        end
      end
    end
  end
end

Version data entries

75 entries across 75 versions & 1 rubygems

Version Path
graphql-1.8.18 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.17 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.16 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.15 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.14 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.13 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.12 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.11 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.10 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.9 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.8 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.7 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.6 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.5 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.4 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.3 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.2 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.1 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.0 lib/graphql/static_validation/rules/required_arguments_are_present.rb
graphql-1.8.0.pre11 lib/graphql/static_validation/rules/required_arguments_are_present.rb