Skip to content

Commit

Permalink
Merge pull request #4648 from gmcgibbon/cache
Browse files Browse the repository at this point in the history
Set GraphQL cache regardless of bootsnap
  • Loading branch information
rmosolgo authored Nov 1, 2023
2 parents 4fa1b8c + dfe9dd1 commit f2387fb
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 `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.
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 = 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 f2387fb

Please sign in to comment.