title: Using pg-promise Tasks with graphql and dataloader
body: <p>First thing is to say that I have seen this question <a href="https://stackoverflow.com/questions/54679043/sharing-a-pg-promise-task-across-parts-of-an-http-request">Sharing a pg-promise task across parts of an http request</a>  and read the answer. I'd like to revisit the question with some added complication.</p>
  <p>I am working on a typescript graphql server on <code>apollo-server-express</code> using a postgres database as the backend and <code>pg-promise</code> as the database interface library. I have query resolvers using dataloaders using the pattern described here: <a href="https://github.com/graphql/dataloader#creating-a-new-dataloader-per-request" rel="nofollow noreferrer">https://github.com/graphql/dataloader#creating-a-new-dataloader-per-request</a>.</p>
  <p>All database access occurs via a single instance of <code>pg-promise</code> as recommended e.g. <a href="https://github.com/vitaly-t/pg-promise-demo/blob/master/TypeScript/db/index.ts" rel="nofollow noreferrer">https://github.com/vitaly-t/pg-promise-demo/blob/master/TypeScript/db/index.ts</a>.</p>
  <p>Like the original question, I am trying to find a good way to create a Task that can be incorporated into the dataloaders (or passed as a param when the dataloader is called) such that query resolvers operate within a single database connection. I don't know which resolvers will be called or in which order they will be called so I can't say that a certain resolver should create the task.</p>
  <p>I've been trying to figure a way to use the graphql <code>Context</code> to share the task as I am currently using the <code>Context</code> to share the dataloaders object but as was noted, all calls in the task happen within a callback function so sharing the task via the <code>Context</code> is out. Here's an example of how the context is setup:</p>
  <pre><code>/*
  createLoaders() returns a new object of DataLoaders e.g.
  {
    users: new DataLoader(ids =&gt; getUsers(ids)),
    tags: new DataLoader(ids =&gt; getTags(ids))
  }
  */
  import { createLoaders } from './dataloaders'
  
  const server = new ApolloServer({
    schema,
    context: (): Promise&lt;Context&gt; =&gt; {
      // create a connection here to inject into createLoaders?
      const loaders = createLoaders()
      return { loaders }
    }
  })
  </code></pre>
  <p>and an example in the Apollo docs of creating a db connection within the context: <a href="https://www.apollographql.com/docs/apollo-server/data/resolvers/#the-context-argument" rel="nofollow noreferrer">https://www.apollographql.com/docs/apollo-server/data/resolvers/#the-context-argument</a> (note that they don't describe closing the connection anywhere).</p>
  <pre><code>context: async () =&gt; ({
    db: await client.connect(),
  })
  
  // Resolver
  (parent, args, context, info) =&gt; {
    return context.db.query('SELECT * FROM table_name');
  }
  </code></pre>
  <p>This may not be possible without using the <code>connect</code> method, but its use for this type of operation is discouraged by the author of the libary. And the same problem arises that the connection needs to be returned to the pool when the query is finally resolved just before sending results back to the client.</p>
  <p>Any help is appreciated! (and thanks to @vitaly-t for such strong work on <code>pg-promise</code>)</p>
  <p><strong>Edit 1</strong>
  @Daniel-Rearden your suggestion and linked gist ended up working. I will likely inspect incoming requests from within the plugin if always opening a task becomes an issue. Marking your answer correct!</p>