Skip to content

Is it possible to pass db transaction in dataloader? #1702

Answered by xamgore
ziimakc asked this question in Q&A
Discussion options

You must be logged in to vote

So you have a scope with selection fields in the form scope { user { ... } friends { ... } }.

You would like the whole tree to be fetched during the same transaction, starting from the scope node and progressing into the inner fields.

I don't think you need a Dataloader, just the shared transaction for inner resolvers:

impl Mutation {
  fn scope(...) -> Scope {
    // do mutation with the first transaction, commit
    let tx = new_read_only_transaction();
    Scope {
        tx,
        dataloader: DbEntityLoader::new(tx),
        user_id,
        friends_ids,
    }
}

#[graphql(complex)]
struct Scope {
  tx: DatabaseTransaction,     // either this scope object keeps the transaction
  dat…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@ziimakc
Comment options

@xamgore
Comment options

Answer selected by ziimakc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants