Last active
April 28, 2020 12:40
-
-
Save theorygeek/8853ca9c8d8d566b5f60ab8747db0306 to your computer and use it in GitHub Desktop.
GraphQL Caching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define an instrumentation that performs the caching | |
class CachingInstrumentation | |
def instrument(_type, field) | |
return field unless field.metadata.include?(:cache_proc) | |
old_resolver = field.resolve_proc | |
new_resolver = -> (obj, args, ctx) do | |
# Get the caching key | |
cache_key = field.metadata[:cache_proc].call(obj, args, ctx) | |
# Attempt to retrieve the value from the cache | |
value = get_from_cache(cache_key) | |
return value if value | |
# On a miss, fallback to the old value + store in cache | |
value = old_resolver.call(obj, args, ctx) | |
insert_into_cache(cache_key, value) | |
value | |
end | |
# Return the redefined field with the new resolver | |
field.redefine do | |
resolve(new_resolver) | |
end | |
end | |
def get_from_cache(key) | |
# TODO | |
end | |
def insert_into_cache(key, value) | |
# TODO | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set up GraphQL Field so that it accepts a proc that will determine the caching value | |
GraphQL::Field.accepts_definitions( | |
cache: GraphQL::Define.assign_metadata_key(:cache_proc) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SomeType = GraphQL::ObjectType.define do | |
name "Something" | |
field :myField, !types.String do | |
# Flag a field as cacheable | |
cache -> (obj, args, ctx) { "Something.myField(#{obj.id})" } | |
resolve -> (obj, args, ctx) { obj.expensive_computation } | |
end | |
end | |
# Instrument your schema | |
Schema.define do | |
# ... other stuff here | |
instrument(:field, CachingInstrumentation.new) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment