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 ...@@ -8,6 +8,14 @@ module Resolvers
argument_class ::Types::BaseArgument 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 def self.singular_type
return unless type return unless type
......
...@@ -5,6 +5,8 @@ module Resolvers ...@@ -5,6 +5,8 @@ module Resolvers
class DesignResolver < BaseResolver class DesignResolver < BaseResolver
type ::Types::DesignManagement::DesignType, null: true type ::Types::DesignManagement::DesignType, null: true
requires_argument!
argument :id, ::Types::GlobalIDType[::DesignManagement::Design], argument :id, ::Types::GlobalIDType[::DesignManagement::Design],
required: false, required: false,
description: 'Find a design by its ID' description: 'Find a design by its ID'
......
...@@ -12,6 +12,8 @@ module Resolvers ...@@ -12,6 +12,8 @@ module Resolvers
type Types::DesignManagement::DesignAtVersionType, null: true type Types::DesignManagement::DesignAtVersionType, null: true
requires_argument!
authorize :read_design authorize :read_design
argument :id, DesignAtVersionID, argument :id, DesignAtVersionID,
......
...@@ -7,6 +7,8 @@ module Resolvers ...@@ -7,6 +7,8 @@ module Resolvers
type Types::DesignManagement::VersionType, null: true type Types::DesignManagement::VersionType, null: true
requires_argument!
authorize :read_design authorize :read_design
alias_method :collection, :object alias_method :collection, :object
......
...@@ -12,6 +12,7 @@ module Types ...@@ -12,6 +12,7 @@ module Types
def initialize(*args, **kwargs, &block) def initialize(*args, **kwargs, &block)
@calls_gitaly = !!kwargs.delete(:calls_gitaly) @calls_gitaly = !!kwargs.delete(:calls_gitaly)
@constant_complexity = !!kwargs[:complexity] @constant_complexity = !!kwargs[:complexity]
@requires_argument = !!kwargs.delete(:requires_argument)
kwargs[:complexity] = field_complexity(kwargs[:resolver_class], kwargs[:complexity]) kwargs[:complexity] = field_complexity(kwargs[:resolver_class], kwargs[:complexity])
@feature_flag = kwargs[:feature_flag] @feature_flag = kwargs[:feature_flag]
kwargs = check_feature_flag(kwargs) kwargs = check_feature_flag(kwargs)
...@@ -20,6 +21,10 @@ module Types ...@@ -20,6 +21,10 @@ module Types
super(*args, **kwargs, &block) super(*args, **kwargs, &block)
end 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 # 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 # Modified to fix https://github.com/rmosolgo/graphql-ruby/issues/3113
def resolve_field(obj, args, ctx) def resolve_field(obj, args, ctx)
......
...@@ -3,5 +3,7 @@ ...@@ -3,5 +3,7 @@
module Types module Types
module BaseInterface module BaseInterface
include GraphQL::Schema::Interface include GraphQL::Schema::Interface
field_class ::Types::BaseField
end end
end end
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
module Types module Types
module DesignManagement module DesignManagement
class DesignCollectionType < BaseObject class DesignCollectionType < ::Types::BaseObject
graphql_name 'DesignCollection' graphql_name 'DesignCollection'
description 'A collection of designs' description 'A collection of designs'
......
...@@ -14,7 +14,7 @@ RSpec.describe LooksAhead do ...@@ -14,7 +14,7 @@ RSpec.describe LooksAhead do
# Simplified schema to test lookahead # Simplified schema to test lookahead
let_it_be(:schema) do let_it_be(:schema) do
issues_resolver = Class.new(Resolvers::BaseResolver) do issues_resolver = Class.new(GraphQL::Schema::Resolver) do
include LooksAhead include LooksAhead
def resolve_with_lookahead(**args) def resolve_with_lookahead(**args)
...@@ -41,7 +41,6 @@ RSpec.describe LooksAhead do ...@@ -41,7 +41,6 @@ RSpec.describe LooksAhead do
field :issues, issue.connection_type, field :issues, issue.connection_type,
null: true null: true
field :issues_with_lookahead, issue.connection_type, field :issues_with_lookahead, issue.connection_type,
extras: [:lookahead],
resolver: issues_resolver, resolver: issues_resolver,
null: true null: true
end end
......
...@@ -458,8 +458,18 @@ module GraphqlHelpers ...@@ -458,8 +458,18 @@ module GraphqlHelpers
field_type(field).kind.enum? field_type(field).kind.enum?
end end
# There are a few non BaseField fields in our schema (pageInfo for one).
# None of them require arguments.
def required_arguments?(field) 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 end
def io_value?(value) 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