question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Accessing `pgClient` in hooks for `explain` data

See original GitHub issue

Summary

We’re using the embeddable Apollo Explorer in place of GraphiQL (for a number of reasons that are off topic) and we’d like to add the explain data onto result data.

Additional context

We already know that we can use the postgraphile:http:end hook to mutate result data. However, we’re hitting a wall trying to find the right hook or right method to access pgClient._explainResults. We’re starting postgraphile with allowExplain: true fwiw.

I’ve tried to trace through contexts to figure out where postgraphile might be exposing that, but I’m coming up empty. (It’s a bit dizzying trying to follow what is sent where and how contexts are constructed, without being intimately familiar with it).

Any pointers to solve this one?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
shellscapecommented, Jun 23, 2022

awesome. got it. here’s the last bit, which must be placed before app.use(postgraphile) :

  // koa
  app.use(async (ctx, next) => {
    // Note: must use lowercase here, as postgraphile looks for the lowercase variant
    ctx.request.headers['x-postgraphile-explain'] = 'on';
    await next();
  });

I haven’t figured out how to hack Apollo Explorer to inject explain into the results pane (it seems to be ignoring anything that’s not "data") but this will allow someone to log the explain at the very least:

  'postgraphile:http:result': (result, opts) => {
    const { explain } = result as any;

    if (explain) {
      explain.forEach(({ plan, query }: { plan: any; query: any }) => {
        log.info('Explain:\n\n  query:', query, '\n\n  plan:', plan);
      });
    }
  }

Thanks @benjie

0reactions
benjiecommented, Jun 22, 2022

Turns out it’s X-PostGraphile-Explain: on; set here in the GraphiQL fetcher: https://github.com/graphile/postgraphile/blob/a33712580ae3304d9f68cedaf4816a0661dfbad1/postgraphiql/src/components/PostGraphiQL.js#L376

Which results in explain being set to true on the withPostGraphileContext options: https://github.com/graphile/postgraphile/blob/ed6ecb4596088120969795a9ec30cb625ef93eae/src/postgraphile/http/createPostGraphileHttpRequestHandler.ts#L152

Which then uses the startExplain / stopExplain methods that we patch onto the Postgres client:

https://github.com/graphile/postgraphile/blob/263ba7477bc2133eebdf89d29acd0460e58501ec/src/postgraphile/withPostGraphileContext.ts#L481-L508

(Note that this will not work with schema-only usage because we don’t mod the postgres client with schema-only usage.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Apache Airflow | Getting Results From PostgreSQL Using Hooks
In this video, we are going to see how can we get data from PostgreSQL using a Hook from our DAG. If you...
Read more >
Using a PostgreSQL client to connect to your DB cluster
You can use the pgAdmin client to access your data in native PostgreSQL dialect. To connect to the cluster with the pgAdmin client....
Read more >
Airflow Hooks Explained 101: A Complete Guide - Learn | Hevo
In summary, this blog presented a complete overview of developing and maintaining Airflow hooks, using one example of a PostgreSQL Airflow hook.
Read more >
Connecting – node-postgres
... methods for connecting to database instances using short-lived authentication tokens. ... const { Client } = require('pg') client = new Client({ host: ......
Read more >
PostGraphile | makeExtendSchemaPlugin (graphile-utils)
When defining a field on an existing table-backed type defined by PostGraphile, it's useful to access data from the underlying table in the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found