Skip to content

Commit 9433eff

Browse files
authored
Merge pull request #3959 from rmosolgo/schema-footprint-benchmark
Add 'rake bench:profile_schema_memory_footprint' and work on it
2 parents 80ed8b5 + 943c3ed commit 9433eff

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ namespace :bench do
116116
prepare_benchmark
117117
GraphQLBenchmark.profile_batch_loaders
118118
end
119+
120+
desc "Check the memory footprint of a large schema"
121+
task :profile_schema_memory_footprint do
122+
prepare_benchmark
123+
GraphQLBenchmark.profile_schema_memory_footprint
124+
end
119125
end
120126

121127
namespace :test do

benchmark/run.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,45 @@ def self.profile_batch_loaders
221221

222222
report.pretty_print
223223
end
224+
225+
def self.profile_schema_memory_footprint
226+
schema = nil
227+
report = MemoryProfiler.report do
228+
query_type = Class.new(GraphQL::Schema::Object) do
229+
graphql_name "Query"
230+
100.times do |i|
231+
type = Class.new(GraphQL::Schema::Object) do
232+
graphql_name "Object#{i}"
233+
field :f, Integer
234+
end
235+
field "f#{i}", type
236+
end
237+
end
238+
239+
thing_type = Class.new(GraphQL::Schema::Object) do
240+
graphql_name "Thing"
241+
field :name, String
242+
end
243+
244+
mutation_type = Class.new(GraphQL::Schema::Object) do
245+
graphql_name "Mutation"
246+
100.times do |i|
247+
mutation_class = Class.new(GraphQL::Schema::RelayClassicMutation) do
248+
graphql_name "Do#{i}"
249+
argument :id, "ID"
250+
field :thing, thing_type
251+
field :things, thing_type.connection_type
252+
end
253+
field "f#{i}", mutation: mutation_class
254+
end
255+
end
256+
257+
schema = Class.new(GraphQL::Schema) do
258+
query(query_type)
259+
mutation(mutation_type)
260+
end
261+
end
262+
263+
report.pretty_print
264+
end
224265
end

lib/graphql/schema/argument.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ def initialize(arg_name = nil, type_expr = nil, desc = nil, required: true, type
7575
end
7676
end
7777

78-
self.validates(validates)
78+
if validates && !validates.empty?
79+
self.validates(validates)
80+
end
81+
7982
if required == :nullable
8083
self.owner.validates(required: { argument: arg_name })
8184
end

lib/graphql/schema/field.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ def initialize(type: nil, name: nil, owner: nil, null: true, description: nil, d
303303
end
304304
end
305305

306-
self.validates(validates)
306+
if !validates.empty?
307+
self.validates(validates)
308+
end
307309

308310
if definition_block
309311
if definition_block.arity == 1

lib/graphql/schema/object.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,14 @@ def initialize(object, context)
9898
class << self
9999
# Set up a type-specific invalid null error to use when this object's non-null fields wrongly return `nil`.
100100
# It should help with debugging and bug tracker integrations.
101-
def inherited(child_class)
102-
child_class.const_set(:InvalidNullError, GraphQL::InvalidNullError.subclass_for(child_class))
103-
super
101+
def const_missing(name)
102+
if name == :InvalidNullError
103+
custom_err_class = GraphQL::InvalidNullError.subclass_for(self)
104+
const_set(:InvalidNullError, custom_err_class)
105+
custom_err_class
106+
else
107+
super
108+
end
104109
end
105110

106111
def kind

0 commit comments

Comments
 (0)