Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Way to conditionally use fragment cache? #55

Closed
tjk opened this issue Mar 10, 2021 · 4 comments · Fixed by #61
Closed

Way to conditionally use fragment cache? #55

tjk opened this issue Mar 10, 2021 · 4 comments · Fixed by #61

Comments

@tjk
Copy link

tjk commented Mar 10, 2021

Basically topic, but would like a way to only go through the cache under certain conditions (couldn't tell from code or docs if already supported). For example, if a particular query will explode the cardinality but I want the base case to be cached. Contrived example:

field :search_movies, [MovieResultType], null: false do
  argument :query, String, required: false
end
def search_movies(query: nil)
  cache_fragment(disabled: !query.blank?) do
    Movie.do_expensive_search(query) # and expensive resolving
  end
end
@tjk tjk changed the title Way to conditionally cache? Way to conditionally use fragment cache? Mar 10, 2021
@DmitryTsepelev
Copy link
Owner

Hi @tjk! Currently it's not possible via API, but we could to do a similar thing with plain Ruby:

field :search_movies, [MovieResultType], null: false do
  argument :query, String, required: false
end

def search_movies(query: nil)
  if query.blank?
    cache_fragment(disabled: !query.blank?) do
      Movie.do_expensive_search(query) # and expensive resolving
    end
  else
    Movie.do_expensive_search(query) # and expensive resolving
  end
end

@tjk
Copy link
Author

tjk commented Mar 10, 2021

Yea considered this, just thought it was a little gross -- but I'll go with it for now! Thanks.

Would love to help with a patch if it's something you want added but I figure it's probably trivial enough that you communicating the API you'd like would take more effort than just doing it yourself... but let me know! :)

@DmitryTsepelev
Copy link
Owner

Yeah, I'm definitely open to PRs!

@tjk
Copy link
Author

tjk commented Mar 10, 2021

Pushed my first pass attempt here: #56

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants