Skip to content

Allow root operation resolvers #3917

@vwkd

Description

@vwkd

Currently, we can't set root operation resolvers. We can only set resolvers for the fields of the root operations. This is in contrast to the object fields of a non-root object type, where we can set the resolver for the field and additionally for the sub-fields. It would be useful to treat the root operation as any other object type for which a resolver can be set.

One use case for a root operation resolver is atomic transactions. For two mutations, either both should succeed or both should fail. Because resolvers are run sequentially, we can't coordinate the database calls through shared state, since one needs to return before the other even started. With root resolvers, we could handle both mutations in a single resolver and ensure an atomic transaction. One limitation would be, that both mutations can only return the same type, since the single return value from the root resolver would be used for both.

This would probably work well together with middleware #1516 too.

A workaround currently is to bundle the mutations into a single mutation such that you can create a single resolver for them. There's a nice example here https://web.archive.org/web/20220113210551/https://graphqlme.com/2018/05/13/transactions-mutations-and-graphql/

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