diff --git a/guides/parser_cache.md b/guides/parser_cache.md new file mode 100644 index 0000000000..b0c9a58913 --- /dev/null +++ b/guides/parser_cache.md @@ -0,0 +1,12 @@ +--- +layout: guide +doc_stub: false +search: true +title: GraphQL Parser cache +section: Other +desc: How to make parsing GraphQL faster with caching +--- + +Parser caching may be optionally enabled by setting `config.graphql.parser_cache = true` in your Rails application. The cache may be manually built by assigning `GraphQL::Language::Parser.cache = GraphQL::Language::Cache.new("some_dir")`. This will create a directory (`tmp/cache/graphql` by default) that stores a cache of parsed files. + +Much like [bootsnap](https://github.com/Shopify/bootsnap), the parser cache needs to be cleaned up manually. You will need to clear the cache directory for each new deployment of your application. Also note that the parser cache will grow as your schema is loaded, so the cache directory must be writable. diff --git a/lib/graphql/railtie.rb b/lib/graphql/railtie.rb index 36b11c1d68..9752e4d9dc 100644 --- a/lib/graphql/railtie.rb +++ b/lib/graphql/railtie.rb @@ -1,12 +1,15 @@ # frozen_string_literal: true + module GraphQL class Railtie < Rails::Railtie - config.before_configuration do - # Bootsnap compile cache has similar expiration properties, - # so we assume that if the user has bootsnap setup it's ok - # to piggy back on it. - if ::Object.const_defined?("Bootsnap::CompileCache::ISeq") && Bootsnap::CompileCache::ISeq.cache_dir - Language::Parser.cache ||= Language::Cache.new(Pathname.new(Bootsnap::CompileCache::ISeq.cache_dir).join('graphql')) + config.graphql = ActiveSupport::OrderedOptions.new + config.graphql.parser_cache = false + + initializer("graphql.cache") do |app| + if config.graphql.parser_cache + Language::Parser.cache ||= Language::Cache.new( + app.root.join("tmp/cache/graphql") + ) end end end