-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduces require time from ~160ms to ~5ms. Autoloads all constants under `GraphQL` to optimize boot time. Also autoloads `GraphQL::Types` and `GraphQL::Query` because they can't be required in sequence. Autoloads are automatically managed with `GraphQL::Autoload` similar to `ActiveSupport::Autoload`.
- Loading branch information
Showing
15 changed files
with
198 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
layout: guide | ||
doc_stub: false | ||
search: true | ||
title: Code Loading | ||
section: Other | ||
desc: Read this before deploying GraphQL to production. | ||
--- | ||
|
||
## Autoloading and Eager Loading | ||
|
||
GraphQL Ruby is autoloaded, which means most code won't be loaded until it is referenced. This is optimal for development and test environments where you want to boot your application as fast as possible. However, this is not optimal for production enviromnets. | ||
|
||
Production environments typically include multiple workers, and need to load an application upfront as much as possible. This ensures requests are as fast as possible at the cost of increased boot time, and forked processes don't need to load additional code. Unfortunately, there is no approach to eager code loading that is accepted by all web application frameworks. | ||
|
||
- For Rails applications, a Railtie is included that automatically eager-loads the GraphQL Ruby library for you. No action is required by the developer to opt into this behaviour. | ||
|
||
- For Sinatra applications, please put `configure(:production) { GraphQL.eager_load! }` in your application file. | ||
|
||
- For Hanami applications, please put `environment(:production) { GraphQL.eager_load! }` in your application file. | ||
|
||
- Other frameworks need to manually call `GraphQL.eager_load!` when their application is booting in production mode. If this is not done properly, GraphQL Ruby will log an warning. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module GraphQL | ||
module Autoload | ||
def autoload(const_name, path) | ||
@_eagerloaded_constants ||= [] | ||
@_eagerloaded_constants << const_name | ||
|
||
super const_name, path | ||
end | ||
|
||
def eager_load! | ||
@_eager_loading = true | ||
if @_eagerloaded_constants | ||
@_eagerloaded_constants.each { |const_name| const_get(const_name) } | ||
@_eagerloaded_constants = nil | ||
end | ||
ensure | ||
@_eager_loading = false | ||
end | ||
|
||
private | ||
|
||
def eager_loading? | ||
@_eager_loading ||= false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
# 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 | ||
extend Autoload | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module EagerModule | ||
module EagerClass | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module EagerModule | ||
module OtherEagerClass | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module LazyModule | ||
module LazyClass | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
describe GraphQL::Autoload do | ||
module LazyModule | ||
extend GraphQL::Autoload | ||
autoload(:LazyClass, "fixtures/lazy_module/lazy_class") | ||
end | ||
|
||
module EagerModule | ||
extend GraphQL::Autoload | ||
autoload(:EagerClass, "fixtures/eager_module/eager_class") | ||
autoload(:OtherEagerClass, "fixtures/eager_module/other_eager_class") | ||
end | ||
|
||
describe "#autoload" do | ||
it "sets autoload" do | ||
assert_equal("fixtures/lazy_module/lazy_class", LazyModule.autoload?(:LazyClass)) | ||
LazyModule::LazyClass | ||
assert_nil(LazyModule.autoload?(:LazyClass)) | ||
end | ||
end | ||
|
||
describe "#eager_load!" do | ||
it "eagerly loads autoload entries" do | ||
EagerModule.eager_load! | ||
|
||
assert_nil(EagerModule.autoload?(:EagerClass)) | ||
assert_nil(EagerModule.autoload?(:OtherEagerClass)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters