Commit 4550c443 authored by Imre Farkas's avatar Imre Farkas

Merge branch 'ajk-gql-resolvers-required-argument' into 'master'

Annotate resolvers that require an argument

See merge request gitlab-org/gitlab!48494
parents a6eb1ea3 95410d45
......@@ -8,6 +8,14 @@ module Resolvers
argument_class ::Types::BaseArgument
def self.requires_argument!
@requires_argument = true
end
def self.field_options
super.merge(requires_argument: @requires_argument)
end
def self.singular_type
return unless type
......
......@@ -5,6 +5,8 @@ module Resolvers
class DesignResolver < BaseResolver
type ::Types::DesignManagement::DesignType, null: true
requires_argument!
argument :id, ::Types::GlobalIDType[::DesignManagement::Design],
required: false,
description: 'Find a design by its ID'
......
......@@ -12,6 +12,8 @@ module Resolvers
type Types::DesignManagement::DesignAtVersionType, null: true
requires_argument!
authorize :read_design
argument :id, DesignAtVersionID,
......
......@@ -7,6 +7,8 @@ module Resolvers
type Types::DesignManagement::VersionType, null: true
requires_argument!
authorize :read_design
alias_method :collection, :object
......
......@@ -12,6 +12,7 @@ module Types
def initialize(*args, **kwargs, &block)
@calls_gitaly = !!kwargs.delete(:calls_gitaly)
@constant_complexity = !!kwargs[:complexity]
@requires_argument = !!kwargs.delete(:requires_argument)
kwargs[:complexity] = field_complexity(kwargs[:resolver_class], kwargs[:complexity])
@feature_flag = kwargs[:feature_flag]
kwargs = check_feature_flag(kwargs)
......@@ -20,6 +21,10 @@ module Types
super(*args, **kwargs, &block)
end
def requires_argument?
@requires_argument || arguments.values.any? { |argument| argument.type.non_null? }
end
# Based on https://github.com/rmosolgo/graphql-ruby/blob/v1.11.4/lib/graphql/schema/field.rb#L538-L563
# Modified to fix https://github.com/rmosolgo/graphql-ruby/issues/3113
def resolve_field(obj, args, ctx)
......
......@@ -3,5 +3,7 @@
module Types
module BaseInterface
include GraphQL::Schema::Interface
field_class ::Types::BaseField
end
end
......@@ -2,7 +2,7 @@
module Types
module DesignManagement
class DesignCollectionType < BaseObject
class DesignCollectionType < ::Types::BaseObject
graphql_name 'DesignCollection'
description 'A collection of designs'
......
......@@ -14,7 +14,7 @@ RSpec.describe LooksAhead do
# Simplified schema to test lookahead
let_it_be(:schema) do
issues_resolver = Class.new(Resolvers::BaseResolver) do
issues_resolver = Class.new(GraphQL::Schema::Resolver) do
include LooksAhead
def resolve_with_lookahead(**args)
......@@ -41,7 +41,6 @@ RSpec.describe LooksAhead do
field :issues, issue.connection_type,
null: true
field :issues_with_lookahead, issue.connection_type,
extras: [:lookahead],
resolver: issues_resolver,
null: true
end
......
......@@ -458,8 +458,18 @@ module GraphqlHelpers
field_type(field).kind.enum?
end
# There are a few non BaseField fields in our schema (pageInfo for one).
# None of them require arguments.
def required_arguments?(field)
field.arguments.values.any? { |argument| argument.type.non_null? }
return field.requires_argument? if field.is_a?(::Types::BaseField)
if (meta = field.try(:metadata)) && meta[:type_class]
required_arguments?(meta[:type_class])
elsif args = field.try(:arguments)
args.values.any? { |argument| argument.type.non_null? }
else
false
end
end
def io_value?(value)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment