Skip to content

Schema-level root resolve function #353

Closed
@helfer

Description

@helfer

Having worked with GraphQL for a while now, I find that having resolve functions for fields is the really nice abstraction at the core of GraphQL that makes it so powerful. However, there seems to be one point in the query execution where the abstraction breaks down, and I'm not sure why so I thought I'd ask here to be enlightened:

Why is there no resolve function at the root?

Having no resolve function at the root essentially means that there are as many entry points into the graph as there are fields of the query, mutation and subscription types. If you need to do any logic common to all queries (eg. authenticating a user) you either have to create an extra type and make all your queries a field of that, or you have to do it outside of GraphQL and pass the result in as the root value or context. Why is that? Wouldn't it be much nicer, if mutation and subscriptions were considered just like fields of the schema type? By doing that, the resolve tree would be rooted and no longer a forest (it would still be three trees, actually, but if that's an issue we could consider adding a root for that).

With schema-level resolve functions, there would in many cases be no more need for writing custom code that runs outside the resolve functions on the server. Instead, the server (eg. express-graphql) could just pass in the request object (and whatever else it needs) to the root resolve function, which would be the logical place for containing any logic that has to run before any other resolvers run.

I know there will most likely still be a need to run some checks etc. before a request is passed to validation and execution, but I think having a root resolve function would be really nice to have nevertheless because it makes the resolver abstraction more consistent.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions