-
We're setting up ObjectCache in our GraphQL API. One thing I'm running into is that we have a few GraphQL types that are virtual (not backed by an identifiable object) and I'm not sure how to deal with them. If I do nothing GraphQL Ruby will be unhappy because It seems like Caching lists and connections and Object Dependencies are relevant to this problem but I can't wrap my head around how to apply them. See code below, I think I'm looking for a way to call the Here's an excerpt of the code I'm working with: module Types
class FormType < Types::BaseObject
field :fields, [Types::FormFieldType], null: false
end
class FormFieldType < Types::BaseObject
field :name, String, null: false
end
end
At this point when we query a form with its fields it will raise an error because fields aren't 'real' objects with ids: query {
forms {
nodes {
fields {
name
}
}
}
}
I kind of just want to tell FormFieldType to be cached as part of its parent if that makes sense? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hey, thanks for the great question. Yes, I think class Types::FormFieldType < Types::BaseObject
def self.cache_dependencies_for(form_field, context)
form_field.parent
end
# ...
end Can you return the form field's In a pinch, you could use GraphQL-Ruby's "scoped context" field to accomplish this, but I don't recommend it because scoped context breaks if the query doesn't meet the assumptions of the Ruby code. (That is, if the incoming query was a different structure than assumed, weird things can happen since the query tree was different.) In any case, here it is: https://graphql-ruby.org/queries/executing_queries.html#scoped-context Does that help? |
Beta Was this translation helpful? Give feedback.
Hey, thanks for the great question.
Yes, I think
cache_dependencies_for
is the way to go, you could implement it like this:Can you return the form field's
form
object easily? The most fail-safe would be to use an application-defined method on the form field (or add one if necessary, add"form" => form
to the hash, etc). That way, you could be sure that the form field would have access to the right parent, no matter what.In a pinch, you could use GraphQL-Ruby's "scoped context" field to accomplish this, but I don't recommend it because scop…