Replies: 3 comments
-
One possibility would be to use class AssociationExtension < GraphQL::Schema::FieldExtension
def resolve(object:, arguments:, **rest)
context.current_path #=> ["counties", 0, "cbsa"]
field_name = context.current_path.last.to_sym #=> :cbsa
dataloader.with(Sources::Association, field_name).load(object)
end
end Going to try this later on and report back, but also waiting for someone to talk me out of this idea entirely. |
Beta Was this translation helpful? Give feedback.
-
FieldExtensions have a reference to the graphql-ruby/lib/graphql/schema/field_extension.rb Lines 12 to 13 in 930468f But I think if it was me, I'd use a resolver, something like this: class Types::BaseField < GraphQL::Schema::Field
class DataloadRelationResolver < GraphQL::Schema::Resolver
def resolve
dataloader.with(Sources::Association, @field.original_name).load(object)
end
end
def initialize(*args, dataload_relation: false, **kwargs, &block)
if dataload_relation
# TODO add a check to see if there's already a resolver, raise an ArgumentError if so
kwargs[:resolver] = DataloadRelationResolver
end
super(*args, **kwargs, &block)
end
end Then use it like: field :cbsa, Types::CbsaType, dataload_relation: true I wouldn't recommend using {
county(id: $id) {
blah: cbsa { ... }
# ^^ in this case, context[:current_path] would have "blah" at the end
}
} |
Beta Was this translation helpful? Give feedback.
-
Very cool. I will give this a try.
…On Thu, Jan 25, 2024 at 3:22 PM Robert Mosolgo ***@***.***> wrote:
FieldExtensions have a reference to the field they're attached to, that
might help:
https://github.com/rmosolgo/graphql-ruby/blob/930468fb4d39a62a21baf341f0c98932a15c3ba9/lib/graphql/schema/field_extension.rb#L12-L13
But I think if it was me, I'd use a resolver, something like this:
class Types::BaseField < GraphQL::Schema::Field
class DataloadRelationResolver < GraphQL::Schema::Resolver
def resolve
dataloader.with(Sources::Association, @field.original_name).load(object)
end
end
def initialize(*args, dataload_relation: false, **kwargs, &block)
if dataload_relation
# TODO add a check to see if there's already a resolver, raise an ArgumentError if so
kwargs[:resolver] = DataloadRelationResolver
end
super(*args, **kwargs, &block)
end end
Then use it like:
field :cbsa, Types::CbsaType, dataload_relation: true
I wouldn't recommend using context[:current_path] because it will include
any field aliases in the query, for example:
{
county(id: $id) {
blah: cbsa { ... } # ^^ in this case, context[:current_path] would have "blah" at the end
}
}
—
Reply to this email directly, view it on GitHub
<#4806 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABIZMP37P53GZINUN6XVI3YQLEJRAVCNFSM6AAAAABCH5WFX6VHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4DENJQGEYDE>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Hello. I'm trying to figure out a way to write a custom field extension or resolver to use for basic cases when I want to batch load. In fact I'd love to automatically use this resolver/extension when the field is an ActiveRecord association, but one step at a time.
I've implemented
Sources::Association
with data loader and can use it like this:But what I'd like is
I'm trying to wrap my head around how to do this but it seemed difficult enough to convince me it might not be a great idea. In my head it feels like implementing resolve in an Extension would be simple enough but I'm not exactly sure how to get the field name to symbolize to pass to data loader. Something like
I suppose I could initialize it with the field name but it's redundant and verbose.
Not exactly sure how the custom resolver would work as I don't want to declare args/type/etc... in the Resolver. I just want the resolve method to use my dataloader source.
Anyways what are people's thoughts. Talk me out of this because it's spinning my wheels 🙃
Beta Was this translation helpful? Give feedback.
All reactions