Commit 80a6f1bf authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'ajk-rake-graphql-analyse' into 'master'

Add gitlab:graphql:analyze task

See merge request gitlab-org/gitlab!52318
parents e855f37f 0c185707
......@@ -145,6 +145,20 @@ module Gitlab
return redacted if printer.fields_printed > 0
end
def complexity(schema)
# See BaseResolver::resolver_complexity
# we want to see the max possible complexity.
fake_args = Struct
.new(:if, :keyword_arguments)
.new(nil, { sort: true, search: true })
query = GraphQL::Query.new(schema, text)
# We have no arguments, so fake them.
query.define_singleton_method(:arguments_for) { |_x, _y| fake_args }
GraphQL::Analysis::AST.analyze_query(query, [GraphQL::Analysis::AST::QueryComplexity]).first
end
def query
return @query if defined?(@query)
......
......@@ -33,7 +33,31 @@ namespace :gitlab do
)
namespace :graphql do
desc 'Gitlab | GraphQL | Validate queries'
desc 'GitLab | GraphQL | Analyze queries'
task analyze: [:environment, :enable_feature_flags] do |t, args|
queries = if args.to_a.present?
args.to_a.flat_map { |path| Gitlab::Graphql::Queries.find(path) }
else
Gitlab::Graphql::Queries.all
end
queries.each do |defn|
$stdout.puts defn.file
summary, errs = defn.validate(GitlabSchema)
if summary == :client_query
$stdout.puts " - client query"
elsif errs.present?
$stdout.puts " - invalid query"
else
$stdout.puts " - complexity: #{defn.complexity(GitlabSchema)}"
end
$stdout.puts ""
end
end
desc 'GitLab | GraphQL | Validate queries'
task validate: [:environment, :enable_feature_flags] do |t, args|
queries = if args.to_a.present?
args.to_a.flat_map { |path| Gitlab::Graphql::Queries.find(path) }
......
......@@ -151,6 +151,10 @@ RSpec.describe Gitlab::Graphql::Queries do
let(:path) { 'post_by_slug.graphql' }
it_behaves_like 'a valid GraphQL query for the blog schema'
it 'has a complexity' do
expect(subject.complexity(schema)).to be < 10
end
end
context 'a query with an import' do
......
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