Commit 0c185707 authored by Alex Kalderimis's avatar Alex Kalderimis Committed by Bob Van Landuyt

Add gitlab:graphql:analyze task

parent 06655b7f
...@@ -145,6 +145,20 @@ module Gitlab ...@@ -145,6 +145,20 @@ module Gitlab
return redacted if printer.fields_printed > 0 return redacted if printer.fields_printed > 0
end 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 def query
return @query if defined?(@query) return @query if defined?(@query)
......
...@@ -33,7 +33,31 @@ namespace :gitlab do ...@@ -33,7 +33,31 @@ namespace :gitlab do
) )
namespace :graphql 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| task validate: [:environment, :enable_feature_flags] do |t, args|
queries = if args.to_a.present? queries = if args.to_a.present?
args.to_a.flat_map { |path| Gitlab::Graphql::Queries.find(path) } args.to_a.flat_map { |path| Gitlab::Graphql::Queries.find(path) }
......
...@@ -151,6 +151,10 @@ RSpec.describe Gitlab::Graphql::Queries do ...@@ -151,6 +151,10 @@ RSpec.describe Gitlab::Graphql::Queries do
let(:path) { 'post_by_slug.graphql' } let(:path) { 'post_by_slug.graphql' }
it_behaves_like 'a valid GraphQL query for the blog schema' 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 end
context 'a query with an import' do 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