Skip to content

How to properly use transactions (relational db) with N+1 queries #684

@stringbeans

Description

@stringbeans

Suppose I have a mutation:

mutation {
  updateStudent(studentId: 5, firstname: "joe") {
    studentId,
    firstname,
    studentTags {
      value
    }
  }
}

Notice that after the mutation I want to get back the Student object and embedded in the Student object is a one-to-many relationship with studentTags.

Now, to properly handle this we'd want to wrap the update operation as well as the following fetch query in a transaction...

however following graphql's principle, the fetch after the insert SHOULD only fetch the corresponding student and not the relations. Eventually, the resolver for the StudentTag object will run which will then fetch the studentTags for the student object. This is problematic however because now its outside of the transaction (assuming i use a transaction closure) OR i simply dont know when to call transaction.commit() because I don't know when the parent Student node has been fully resolved with all its children.

Am I missing something? Is the only option to parse the graphql mutation query after the insert to determine what relations I need to eagerly load? Is there a hook I can use for when all children nodes of a parent node have been fully resolved so I can call a transaction.commit() ?

I suspect anyone who uses a relational db with graphql will eventually run into this issue... I'm currently using Sequelize as my ORM if that helps.

Thanks in advance :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions