Analyzer APIを使ってconnectionフィールドのfirstまたはlast argumentを必須パラメータにする (graphql-ruby 1.9)
code:app/graphql/analyzers/relay_connection_first_last_requried.rb
# frozen_string_literal: true
module Analyzers
class RelayConnectionFirstLastRequired < GraphQL::Analysis::AST::Analyzer
def result
errors
end
def on_leave_field(node, _parent, visitor)
return if visitor.skipping?
field = visitor.field_definition
if connection?(field)
args = visitor.arguments_for(node, field)
unless args.key?("first") || args.key?("last")
errors << GraphQL::AnalysisError.new(
ast_node: node,
)
end
end
end
def connection?(field)
# Kibela's SimpleConnection has no last,
# so just check first existence.
field.type.to_type_signature =~ /Connection!?/ && field.arguments.key?("first")
end
private def errors
@errors ||= []
end
end
end