-
-
Notifications
You must be signed in to change notification settings - Fork 582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for EXPLAIN options to GraphiQL #1618
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
960090b
Add options to Postgres client's explain
angelyan 5dbbd23
Support explain options via postgraphile http header
angelyan 03daa6e
Support explain options in postgraphiql
angelyan 0016e0a
Add button to copy SQL query and plan to postgraphiql
angelyan daee88d
Run linter
angelyan 999cf6f
Roll back EXPLAIN query for mutations
angelyan b68688e
Run prettier
angelyan 059a3a9
Issue explain queries synchronously to keep other concurrent queries …
angelyan 22cdbc3
Remove unnecessary promise chaining
angelyan 390872c
Merge remote-tracking branch 'origin/main'
angelyan 3333a62
Merge explain and explain options headers
angelyan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ import { ExecutionResult, OperationDefinitionNode, Kind } from 'graphql'; | |
import * as sql from 'pg-sql2'; | ||
import { $$pgClient } from '../postgres/inventory/pgClientFromContext'; | ||
import { pluginHookFromOptions } from './pluginHook'; | ||
import { mixed, WithPostGraphileContextOptions } from '../interfaces'; | ||
import { ExplainOptions, mixed, WithPostGraphileContextOptions } from '../interfaces'; | ||
import { formatSQLForDebugging } from 'postgraphile-core'; | ||
|
||
const undefinedIfEmpty = ( | ||
|
@@ -78,6 +78,7 @@ const withDefaultPostGraphileContext: WithPostGraphileContextFn = async ( | |
pgDefaultRole, | ||
pgSettings, | ||
explain, | ||
explainOptions, | ||
queryDocumentAst, | ||
operationName, | ||
pgForceTransaction, | ||
|
@@ -214,7 +215,7 @@ const withDefaultPostGraphileContext: WithPostGraphileContextFn = async ( | |
return withAuthenticatedPgClient(async pgClient => { | ||
let results: Promise<Array<ExplainResult>> | null = null; | ||
if (explain) { | ||
pgClient.startExplain(); | ||
pgClient.startExplain(explainOptions); | ||
} | ||
try { | ||
return await callback({ | ||
|
@@ -454,7 +455,8 @@ type ExplainResult = Omit<RawExplainResult, 'result'> & { | |
declare module 'pg' { | ||
interface ClientBase { | ||
_explainResults: Array<RawExplainResult> | null; | ||
startExplain: () => void; | ||
_explainOptions: ExplainOptions | null; | ||
startExplain: (options: ExplainOptions) => void; | ||
stopExplain: () => Promise<Array<ExplainResult>>; | ||
} | ||
} | ||
|
@@ -471,13 +473,15 @@ export function debugPgClient(pgClient: PoolClient, allowExplain = false): PoolC | |
// already set, use that. | ||
pgClient[$$pgClientOrigQuery] = pgClient.query; | ||
|
||
pgClient.startExplain = () => { | ||
pgClient.startExplain = options => { | ||
pgClient._explainResults = []; | ||
pgClient._explainOptions = options; | ||
}; | ||
|
||
pgClient.stopExplain = async () => { | ||
const results = pgClient._explainResults; | ||
pgClient._explainResults = null; | ||
pgClient._explainOptions = null; | ||
if (!results) { | ||
return Promise.resolve([]); | ||
} | ||
|
@@ -490,7 +494,10 @@ export function debugPgClient(pgClient: PoolClient, allowExplain = false): PoolC | |
if (!firstKey) { | ||
return null; | ||
} | ||
const plan = result.map((r: any) => r[firstKey]).join('\n'); | ||
const plan = result | ||
.map((r: any) => r[firstKey]) | ||
.map((r: any) => (typeof r === 'string' ? r : JSON.stringify(r, null, 2))) | ||
.join('\n'); | ||
return { | ||
...rest, | ||
plan, | ||
|
@@ -532,15 +539,38 @@ export function debugPgClient(pgClient: PoolClient, allowExplain = false): PoolC | |
const query = a && a.text ? a.text : a; | ||
const values = a && a.text ? a.values : b; | ||
if (query.match(/^\s*(select|insert|update|delete|with)\s/i) && !query.includes(';')) { | ||
// Build the EXPLAIN command | ||
let explainCommand = 'explain'; | ||
const explainOptionClauses = Object.entries(pgClient._explainOptions ?? {}).map( | ||
([option, value]) => `${option} ${value}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
); | ||
if (explainOptionClauses.length > 0) { | ||
explainCommand = `${explainCommand} (${explainOptionClauses.join(', ')})`; | ||
} | ||
// Explain it | ||
const explain = `explain ${query}`; | ||
const explain = `${explainCommand} ${query}`; | ||
|
||
// IMPORTANT: the following 3 queries MUST BE ISSUED SYNCHRONOUSLY to ensure other concurrent queries | ||
// aren't interleaved. | ||
|
||
// Create a savepoint before running the EXPLAIN, so we can roll back to it to avoid running mutations | ||
// twice when ANALYZE is enabled. | ||
// This query will fail if there isn't an active transaction. That's fine because that means this is a | ||
// query, so we don't need to roll it back anyway. | ||
pgClient[$$pgClientOrigQuery] | ||
.call(this, 'savepoint postgraphile_explain') | ||
.catch(() => {}); | ||
const explainPromise = pgClient[$$pgClientOrigQuery] | ||
.call(this, explain, values) | ||
.then((data: any) => data.rows) | ||
// swallow errors during explain | ||
.catch(() => null); | ||
pgClient[$$pgClientOrigQuery] | ||
.call(this, 'rollback to savepoint postgraphile_explain') | ||
.catch(() => {}); | ||
pgClient._explainResults.push({ | ||
query, | ||
result: pgClient[$$pgClientOrigQuery] | ||
.call(this, explain, values) | ||
.then((data: any) => data.rows) | ||
// swallow errors during explain | ||
.catch(() => null), | ||
result: explainPromise, | ||
}); | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not understand why prettier is not formatting this file in the way I'd expect. It doesn't seem to be in the
prettierignore
file, and it doesn't seem to be excluded from the prettier globhttps://github.com/graphile/postgraphile/blob/55bff41460b113481c8161ef8f178f5af0a17df3/package.json#L48
And yet, CI passes. Very strange. I will have to investigate at some point.