Skip to content

Commit

Permalink
Autoload nested constants
Browse files Browse the repository at this point in the history
Reduces require time from ~125ms to ~5ms. Autoloads all constants under
`GraphQL` to optimize boot time. Also autoloads `GraphQL::Types` because
they can't be required in sequence.use they can't be required in sequence.
  • Loading branch information
gmcgibbon committed Nov 29, 2024
1 parent db1dba5 commit f9617b3
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 61 deletions.
131 changes: 84 additions & 47 deletions lib/graphql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,53 +74,90 @@ class << self
end

self.reject_numbers_followed_by_names = false
end

# Order matters for these:

require "graphql/execution_error"
require "graphql/runtime_type_error"
require "graphql/unresolved_type_error"
require "graphql/invalid_null_error"
require "graphql/analysis_error"
require "graphql/coercion_error"
require "graphql/invalid_name_error"
require "graphql/integer_decoding_error"
require "graphql/integer_encoding_error"
require "graphql/string_encoding_error"
require "graphql/date_encoding_error"
require "graphql/duration_encoding_error"
require "graphql/type_kinds"
require "graphql/name_validator"
require "graphql/language"

require_relative "./graphql/railtie" if defined? Rails::Railtie

require "graphql/analysis"
require "graphql/tracing"
require "graphql/dig"
require "graphql/execution"
require "graphql/pagination"
require "graphql/schema"
require "graphql/query"
require "graphql/dataloader"
require "graphql/types"
require "graphql/static_validation"
require "graphql/execution"
require "graphql/schema/built_in_types"
require "graphql/schema/loader"
require "graphql/schema/printer"
require "graphql/introspection"
require "graphql/relay"
def self.eager_load!
[
ExecutionError,
RuntimeTypeError,
UnresolvedTypeError,
InvalidNullError,
AnalysisError,
CoercionError,
InvalidNameError,
IntegerDecodingError,
IntegerEncodingError,
StringEncodingError,
DateEncodingError,
DurationEncodingError,
TypeKinds,
NameValidator,
Language,
Analysis,
Tracing,
Dig,
Execution,
Pagination,
Schema,
Query,
Dataloader,
Types,
*Types.eager_load!,
StaticValidation,
Execution,
Introspection,
Relay,
Subscriptions,
ParseError,
Backtrace,
UnauthorizedError,
UnauthorizedEnumValueError,
UnauthorizedFieldError,
LoadApplicationObjectFailedError,
Testing,
Current,
]
end

autoload :ExecutionError, "graphql/execution_error"
autoload :RuntimeTypeError, "graphql/runtime_type_error"
autoload :UnresolvedTypeError, "graphql/unresolved_type_error"
autoload :InvalidNullError, "graphql/invalid_null_error"
autoload :AnalysisError, "graphql/analysis_error"
autoload :CoercionError, "graphql/coercion_error"
autoload :InvalidNameError, "graphql/invalid_name_error"
autoload :IntegerDecodingError, "graphql/integer_decoding_error"
autoload :IntegerEncodingError, "graphql/integer_encoding_error"
autoload :StringEncodingError, "graphql/string_encoding_error"
autoload :DateEncodingError, "graphql/date_encoding_error"
autoload :DurationEncodingError, "graphql/duration_encoding_error"
autoload :TypeKinds, "graphql/type_kinds"
autoload :NameValidator, "graphql/name_validator"
autoload :Language, "graphql/language"

autoload :Analysis, "graphql/analysis"
autoload :Tracing, "graphql/tracing"
autoload :Dig, "graphql/dig"
autoload :Execution, "graphql/execution"
autoload :Pagination, "graphql/pagination"
autoload :Schema, "graphql/schema"
autoload :Query, "graphql/query"
autoload :Dataloader, "graphql/dataloader"
autoload :Types, "graphql/types"
autoload :StaticValidation, "graphql/static_validation"
autoload :Execution, "graphql/execution"
autoload :Introspection, "graphql/introspection"
autoload :Relay, "graphql/relay"
autoload :Subscriptions, "graphql/subscriptions"
autoload :ParseError, "graphql/parse_error"
autoload :Backtrace, "graphql/backtrace"

autoload :UnauthorizedError, "graphql/unauthorized_error"
autoload :UnauthorizedEnumValueError, "graphql/unauthorized_enum_value_error"
autoload :UnauthorizedFieldError, "graphql/unauthorized_field_error"
autoload :LoadApplicationObjectFailedError, "graphql/load_application_object_failed_error"
autoload :Testing, "graphql/testing"
autoload :Current, "graphql/current"
end

require "graphql/version"
require "graphql/subscriptions"
require "graphql/parse_error"
require "graphql/backtrace"

require "graphql/unauthorized_error"
require "graphql/unauthorized_enum_value_error"
require "graphql/unauthorized_field_error"
require "graphql/load_application_object_failed_error"
require "graphql/testing"
require "graphql/current"
require "graphql/railtie" if defined? Rails::Railtie
1 change: 1 addition & 0 deletions lib/graphql/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module GraphQL
class Railtie < Rails::Railtie
config.graphql = ActiveSupport::OrderedOptions.new
config.graphql.parser_cache = false
config.eager_load_namespaces << GraphQL

initializer("graphql.cache") do |app|
if config.graphql.parser_cache
Expand Down
4 changes: 4 additions & 0 deletions lib/graphql/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1802,3 +1802,7 @@ module DefaultTraceClass
end
end
end

require "graphql/schema/built_in_types"
require "graphql/schema/loader"
require "graphql/schema/printer"
1 change: 0 additions & 1 deletion lib/graphql/schema/relay_classic_mutation.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# frozen_string_literal: true
require "graphql/types/string"

module GraphQL
class Schema
Expand Down
43 changes: 32 additions & 11 deletions lib/graphql/types.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
# frozen_string_literal: true
require "graphql/types/boolean"
require "graphql/types/big_int"
require "graphql/types/float"
require "graphql/types/id"
require "graphql/types/int"
require "graphql/types/iso_8601_date"
require "graphql/types/iso_8601_date_time"
require "graphql/types/iso_8601_duration"
require "graphql/types/json"
require "graphql/types/string"
require "graphql/types/relay"

module GraphQL
module Types
def self.eager_load!
[
Boolean,
BigInt,
Float,
ID,
Int,
JSON,
String,
ISO8601Date,
ISO8601DateTime,
ISO8601Duration,
Relay,
]
end

autoload :Boolean, "graphql/types/boolean"
autoload :BigInt, "graphql/types/big_int"
autoload :Float, "graphql/types/float"
autoload :ID, "graphql/types/id"
autoload :Int, "graphql/types/int"
autoload :JSON, "graphql/types/json"
autoload :String, "graphql/types/string"
autoload :ISO8601Date, "graphql/types/iso_8601_date"
autoload :ISO8601DateTime, "graphql/types/iso_8601_date_time"
autoload :ISO8601Duration, "graphql/types/iso_8601_duration"
autoload :Relay, "graphql/types/relay"
end
end
2 changes: 1 addition & 1 deletion spec/graphql/types/iso_8601_date_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true
require "spec_helper"
require "graphql/types/iso_8601_date"

describe GraphQL::Types::ISO8601Date do
module DateTest
class DateObject < GraphQL::Schema::Object
Expand Down
2 changes: 1 addition & 1 deletion spec/graphql/types/iso_8601_date_time_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true
require "spec_helper"
require "graphql/types/iso_8601_date_time"

describe GraphQL::Types::ISO8601DateTime do
module DateTimeTest
class DateTimeObject < GraphQL::Schema::Object
Expand Down

0 comments on commit f9617b3

Please sign in to comment.