Skip to content

Commit

Permalink
Minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Feb 8, 2024
1 parent 87434aa commit 33c9936
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 15 deletions.
19 changes: 8 additions & 11 deletions postgraphiql/src/components/PostGraphiQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ function explainOptionRequiresAnalyze(option) {
return ['wal', 'timing'].includes(option);
}

function buildExplainOptionsHeader(explainOptions) {
function buildExplainOptionsString(explainOptions) {
const analyzeEnabled = explainOptions.analyze;
const validOptions = Object.fromEntries(
Object.entries(explainOptions).filter(
([option]) => !explainOptionRequiresAnalyze(option) || analyzeEnabled,
),
);
const validOptions = analyzeEnabled
? explainOptions
: Object.fromEntries(
Object.entries(explainOptions).filter(([option]) => !explainOptionRequiresAnalyze(option)),
);
return querystring.stringify(validOptions, ';', '=');
}

Expand Down Expand Up @@ -407,18 +407,15 @@ class PostGraphiQL extends React.PureComponent {
*/
executeQuery = async graphQLParams => {
const extraHeaders = this.getHeaders();
const explainOpts = buildExplainOptionsString(this.state.explainOptions);
const response = await fetch(POSTGRAPHILE_CONFIG.graphqlUrl, {
method: 'POST',
headers: Object.assign(
{
Accept: 'application/json',
'Content-Type': 'application/json',
...(this.state.explain && POSTGRAPHILE_CONFIG.allowExplain
? {
'X-PostGraphile-Explain': `on;${buildExplainOptionsHeader(
this.state.explainOptions,
)}`,
}
? { 'X-PostGraphile-Explain': explainOpts ? `on;${explainOpts}` : 'on' }
: null),
},
extraHeaders,
Expand Down
2 changes: 1 addition & 1 deletion scripts/dev
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ trap 'trap - SIGINT SIGTERM EXIT; JOBS="$(jobs -p)"; [[ "$JOBS" != "" ]] && kill

# Run `react-scripts` in the GraphiQL directory as well parallel, but pipe the
# output to `/dev/null`.
#(sleep 1 && cd postgraphiql && PORT=5783 node_modules/.bin/react-scripts start) > /dev/null &
(sleep 1 && cd postgraphiql && PORT=5783 node_modules/.bin/react-scripts start) > /dev/null &

wait %1
kill %2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ function withPostGraphileContextFromReqResGenerator(
jwtToken,
pgSettings,
explain: explainHeader && explainHeader.on,
explainOptions: explainHeader?.options ?? {},
explainOptions: (explainHeader && explainHeader.options) || {},
...moreOptions,
},
context => {
Expand Down
6 changes: 4 additions & 2 deletions src/postgraphile/withPostGraphileContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,10 @@ export function debugPgClient(pgClient: PoolClient, allowExplain = false): PoolC
return null;
}
const plan = result
.map((r: any) => r[firstKey])
.map((r: any) => (typeof r === 'string' ? r : JSON.stringify(r, null, 2)))
.map((r: any) => {
const val = r[firstKey];
return typeof val === 'string' ? val : JSON.stringify(val, null, 2);
})
.join('\n');
return {
...rest,
Expand Down

0 comments on commit 33c9936

Please sign in to comment.