Skip to content

Examples of field resolution affecting parent loader strategy #39

@bessey

Description

@bessey

[This is not a bug, but I don't know where else to put this]

I am trying to wrap my head around what I think is a pretty common use case, and am hoping there's some prior art from others I can learn from. The high level summary is that, given the following query:

query fetchProperty {
  property(id: 1) { 
    id
    name
  }
  property(id: 2) { 
    id
    name
  }
}

I want my batch loader to generate the SQL SELECT id, name FROM properties WHERE id IN (1, 2). Now I have built a loader to generate SELECT * FROM properties WHERE id IN (1, 2) without issues, but I am struggling to understand if / how its possible to have the sub-fields of the field requested affect the loader's strategy.

Relevant code for query:

module GraphType
  QueryType = GraphQL::ObjectType.define do
    field :property, -> { PropertyType } do
      argument :id, !types.ID
      resolve -> (obj, args, ctx) {
        # this is a simple ActiveRecord batch loader that aggregates IDs and
        # fetches them with `.where(id: ids)`
        GraphTool::FindLoader.for(Property.all).load(args[:id])
      }
    end
  end
end


module GraphType
  PropertyType = GraphQL::ObjectType.define do
    field :id, !types.ID
    field :name, !types.String
    field :address, !types.String
    field :email, !types.String
  end
end

I guess what I'm asking is, is there a context available where the property promise has not yet been resolved, but property.name's resolve function is being executed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions