Skip to content

Commit

Permalink
Make GraphQL parser cache opt-in
Browse files Browse the repository at this point in the history
The cache has nothing to do with bootsnap, so we don't need to piggyback
off of it at all. This will prevent bootsnap cache to be polluted by the
graphql gem, and allow more apps to leverage GQL caching. The parser
cache is also now opt-in, which will make this behaviour more expected.
  • Loading branch information
gmcgibbon committed Sep 26, 2023
1 parent f6da30f commit 1b283e2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
12 changes: 12 additions & 0 deletions guides/parser_cache.md
Original file line number Diff line number Diff line change
@@ -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 `GrpahQL::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.
15 changes: 9 additions & 6 deletions lib/graphql/railtie.rb
Original file line number Diff line number Diff line change
@@ -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 = config.active_storage = 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
Expand Down

0 comments on commit 1b283e2

Please sign in to comment.