Skip to content

How to pass a single DataLoader instance to all graphql request handlers? #9

Description

@ryancole

I'm trying to implement DataLoader which batches together underlying database queries within a single request. I'm looking towards react-relay-network-layer to assist with the batching of GraphQL queries into a single HTTP request so that I can use a single DataLoader for all queries within that request.

I notice that graphqlBatchHTTPWrapper calls the graphqlHTTPMiddleware for each query. I need to make accessible to each graphqlHTTPMiddleware call the same instance of my loaders. How do you suggest that I go about this?

Prior to adding react-relay-network-layer, or any batching, just using the default relay network layer, I was simply instantiating my DataLoaders right within the express-graphql middleware function and passing them to the resolvers via rootValue. I can no longer do that there because that'll create new DataLoaders for each query. I need a single DataLoader per request.

Looking for your advice. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions