Giter Club home page Giter Club logo

Comments (6)

DmitryTsepelev avatar DmitryTsepelev commented on June 24, 2024

Hey! The difference is that when you pass only argument (second snippet) it will be used both for building the cache key and as a value for caching, and when you pass an argument and a block — it will build the key using object but return the value.

For instance, imagine that post somehow depends on the user. In this case you might want to cache the post for this particular user, not for everyone:

def post(id:)
  post = Post.find(id)
  cache_fragment([post, current_user]) { post }
end

In case of cache_fragment(Post.find(id)) you do not have this granularity of control: it will grab post, build a key from it and return it back.

from graphql-ruby-fragment_cache.

gsdean avatar gsdean commented on June 24, 2024

Sorry for being obtuse here, but its still not clear from the documentation what the difference between :value and :object is in terms of cache_key:.

from graphql-ruby-fragment_cache.

DmitryTsepelev avatar DmitryTsepelev commented on June 24, 2024

@gsdean but the explanation above is more clear, right?

from graphql-ruby-fragment_cache.

gsdean avatar gsdean commented on June 24, 2024

Sadly no, I think what would be helpful is to clarify the difference in terms of just the attribute without overriding the getter. In other words, what is the difference between:

field :post, PostType, null: true, cache_fragment: {cache_key: :value}

and

field :post, PostType, null: true, cache_fragment: {cache_key: :object}

?

from graphql-ruby-fragment_cache.

gsdean avatar gsdean commented on June 24, 2024

Also not to hijack the thread, but have you seen this #72 (comment)

There is a pretty serious race condition that we should probably address.

from graphql-ruby-fragment_cache.

DmitryTsepelev avatar DmitryTsepelev commented on June 24, 2024

When cache_key is equals :value then the value from the resolver method will be used as a key, when you use object — current object will be used as a key.

For instance impagine a post that has the author, and for some reason we want author cache to be refreshed only when post is updated. In this case we can do this:

class Post
  field :cached_author, Types::User, null: true, cache_fragment: {cache_key: :object}

  def cached_author
    object.author
  end
end

class Query < Base
  field :post, Post, null: true do
    argument :id, GraphQL::Types::ID, required: true
  end

  def post(id:)
    ::Post.find(id)
  end
end

If you keep this code as is than Post instance will be a key for this cached author, otherwise — author itself.

The same thing but in code https://github.com/DmitryTsepelev/graphql-ruby-fragment_cache/blob/master/spec/graphql/fragment_cache/field_extensions_spec.rb#L171.

from graphql-ruby-fragment_cache.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.