Skip to content

Commit ea7b49b

Browse files
authored
feat(deps): upgrade graphiql-explorer (#1132)
1 parent 881fa45 commit ea7b49b

File tree

4 files changed

+158
-71
lines changed

4 files changed

+158
-71
lines changed

postgraphiql/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"version": "1.0.0",
55
"homepage": "https://graphile.org/postgraphile/",
66
"dependencies": {
7-
"graphiql": "npm:@benjie/graphiql@0.13.1-benjie.0",
8-
"graphiql-explorer": "^0.3.7",
7+
"graphiql": "^0.13.2",
8+
"graphiql-explorer": "^0.4.3",
99
"graphql": "^14.2.1",
1010
"react": "^16.8.6",
1111
"react-dom": "^16.8.6",

postgraphiql/src/components/PostGraphiQL.js

Lines changed: 114 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,39 @@ import './postgraphiql.css';
77
import { buildClientSchema, introspectionQuery, isType, GraphQLObjectType } from 'graphql';
88
import { SubscriptionClient } from 'subscriptions-transport-ws';
99

10+
const defaultQuery = `\
11+
# Welcome to PostGraphile's built-in GraphiQL IDE
12+
#
13+
# GraphiQL is an in-browser tool for writing, validating, and
14+
# testing GraphQL queries.
15+
#
16+
# Type queries into this side of the screen, and you will see intelligent
17+
# typeaheads aware of the current GraphQL type schema and live syntax and
18+
# validation errors highlighted within the text.
19+
#
20+
# GraphQL queries typically start with a "{" character. Lines that starts
21+
# with a # are ignored.
22+
#
23+
# An example GraphQL query might look like:
24+
#
25+
# {
26+
# field(arg: "value") {
27+
# subField
28+
# }
29+
# }
30+
#
31+
# Keyboard shortcuts:
32+
#
33+
# Prettify Query: Shift-Ctrl-P (or press the prettify button above)
34+
#
35+
# Merge Query: Shift-Ctrl-M (or press the merge button above)
36+
#
37+
# Run Query: Ctrl-Enter (or press the play button above)
38+
#
39+
# Auto Complete: Ctrl-Space (or just start typing)
40+
#
41+
`;
42+
1043
const isSubscription = ({ query }) =>
1144
parse(query).definitions.some(
1245
definition =>
@@ -31,49 +64,6 @@ const isValidJSON = json => {
3164
}
3265
};
3366

34-
/**
35-
* The GraphiQL Explorer sidebar.
36-
*/
37-
class ExplorerWrapper extends React.PureComponent {
38-
state = {
39-
query: '',
40-
};
41-
componentDidMount() {
42-
const graphiql = this.props.graphiql;
43-
// Extract query from the graphiql ref
44-
if (graphiql) {
45-
this.setState({ query: graphiql.state.query });
46-
}
47-
// Set onEditQuery in the parent so that we can be notified of query changes
48-
this.props.setOnEditQuery(query => this.setState({ query }));
49-
}
50-
51-
componentDidUpdate(prevProps) {
52-
if (!prevProps.graphiql && this.props.graphiql) {
53-
// Extract query from the graphiql ref
54-
this.setState({ query: this.props.graphiql.state.query });
55-
}
56-
}
57-
58-
_onEditQuery = query => {
59-
const graphiql = this.props.graphiql;
60-
if (graphiql) {
61-
graphiql.handleEditQuery(query);
62-
}
63-
};
64-
render() {
65-
return (
66-
<GraphiQLExplorer
67-
schema={this.props.schema}
68-
query={this.state.query}
69-
onEdit={this._onEditQuery}
70-
explorerIsOpen={this.props.explorerIsOpen}
71-
onToggleExplorer={this.props.onToggleExplorer}
72-
/>
73-
);
74-
}
75-
}
76-
7767
const l = window.location;
7868
const websocketUrl = POSTGRAPHILE_CONFIG.graphqlUrl.match(/^https?:/)
7969
? POSTGRAPHILE_CONFIG.graphqlUrl.replace(/^http/, 'ws')
@@ -93,6 +83,7 @@ class PostGraphiQL extends React.PureComponent {
9383
// Our GraphQL schema which GraphiQL will use to do its intelligence
9484
// stuffs.
9585
schema: null,
86+
query: '',
9687
showHeaderEditor: false,
9788
headersText: '{\n"Authorization": null\n}\n',
9889
headersTextValid: true,
@@ -102,6 +93,10 @@ class PostGraphiQL extends React.PureComponent {
10293
POSTGRAPHILE_CONFIG.enhanceGraphiql && POSTGRAPHILE_CONFIG.subscriptions ? 'pending' : null,
10394
};
10495

96+
_onEditQuery = query => {
97+
this.setState({ query });
98+
};
99+
105100
subscriptionsClient =
106101
POSTGRAPHILE_CONFIG.enhanceGraphiql && POSTGRAPHILE_CONFIG.subscriptions
107102
? new SubscriptionClient(websocketUrl, {
@@ -191,6 +186,13 @@ class PostGraphiQL extends React.PureComponent {
191186
// Store our event source so we can unsubscribe later.
192187
this._eventSource = eventSource;
193188
}
189+
const graphiql = this.graphiql;
190+
this.setState({ query: graphiql._storage.get('query') || defaultQuery });
191+
const editor = graphiql.getQueryEditor();
192+
editor.setOption('extraKeys', {
193+
...(editor.options.extraKeys || {}),
194+
'Shift-Alt-LeftClick': this._handleInspectOperation,
195+
});
194196
}
195197

196198
componentWillUnmount() {
@@ -200,6 +202,59 @@ class PostGraphiQL extends React.PureComponent {
200202
this._eventSource = null;
201203
}
202204

205+
_handleInspectOperation = (cm, mousePos) => {
206+
const parsedQuery = parse(this.state.query || '');
207+
208+
if (!parsedQuery) {
209+
console.error("Couldn't parse query document");
210+
return null;
211+
}
212+
213+
var token = cm.getTokenAt(mousePos);
214+
var start = { line: mousePos.line, ch: token.start };
215+
var end = { line: mousePos.line, ch: token.end };
216+
var relevantMousePos = {
217+
start: cm.indexFromPos(start),
218+
end: cm.indexFromPos(end),
219+
};
220+
221+
var position = relevantMousePos;
222+
223+
var def = parsedQuery.definitions.find(definition => {
224+
if (!definition.loc) {
225+
console.log('Missing location information for definition');
226+
return false;
227+
}
228+
229+
const { start, end } = definition.loc;
230+
return start <= position.start && end >= position.end;
231+
});
232+
233+
if (!def) {
234+
console.error('Unable to find definition corresponding to mouse position');
235+
return null;
236+
}
237+
238+
var operationKind =
239+
def.kind === 'OperationDefinition'
240+
? def.operation
241+
: def.kind === 'FragmentDefinition'
242+
? 'fragment'
243+
: 'unknown';
244+
245+
var operationName =
246+
def.kind === 'OperationDefinition' && !!def.name
247+
? def.name.value
248+
: def.kind === 'FragmentDefinition' && !!def.name
249+
? def.name.value
250+
: 'unknown';
251+
252+
var selector = `.graphiql-explorer-root #${operationKind}-${operationName}`;
253+
254+
var el = document.querySelector(selector);
255+
el && el.scrollIntoView();
256+
};
257+
203258
cancelSubscription = () => {
204259
if (this.activeSubscription !== null) {
205260
this.activeSubscription.unsubscribe();
@@ -522,13 +577,18 @@ class PostGraphiQL extends React.PureComponent {
522577
);
523578
}
524579

580+
setGraphiqlRef = ref => {
581+
if (!ref) {
582+
return;
583+
}
584+
this.graphiql = ref;
585+
this.setState({ mounted: true });
586+
};
587+
525588
render() {
526589
const { schema } = this.state;
527590
const sharedProps = {
528-
ref: ref => {
529-
this.setState({ graphiql: ref });
530-
this.graphiql = ref;
531-
},
591+
ref: this.setGraphiqlRef,
532592
schema: schema,
533593
fetcher: this.fetcher,
534594
};
@@ -537,14 +597,17 @@ class PostGraphiQL extends React.PureComponent {
537597
} else {
538598
return (
539599
<div className="postgraphiql-container graphiql-container">
540-
<ExplorerWrapper
600+
<GraphiQLExplorer
541601
schema={schema}
542-
graphiql={this.state.graphiql}
543-
setOnEditQuery={onEditQuery => this.setState({ onEditQuery })}
602+
query={this.state.query}
603+
onEdit={this._onEditQuery}
604+
onRunOperation={operationName => this.graphiql.handleRunQuery(operationName)}
544605
explorerIsOpen={this.state.explorerIsOpen}
545606
onToggleExplorer={this.handleToggleExplorer}
607+
//getDefaultScalarArgValue={getDefaultScalarArgValue}
608+
//makeDefaultArg={makeDefaultArg}
546609
/>
547-
<GraphiQL onEditQuery={this.state.onEditQuery} {...sharedProps}>
610+
<GraphiQL onEditQuery={this._onEditQuery} query={this.state.query} {...sharedProps}>
548611
<GraphiQL.Logo>
549612
<div style={{ display: 'flex', alignItems: 'center' }}>
550613
<div>

postgraphiql/src/components/postgraphiql.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,14 @@
5858
.postgraphiql-container .toolbar {
5959
align-items: center;
6060
}
61+
62+
.postgraphiql-container > .historyPaneWrap {
63+
min-width: 380px;
64+
max-width: 50%;
65+
/* Allow for longer width */
66+
width: auto !important;
67+
}
68+
69+
.graphiql-explorer-root {
70+
overflow: auto !important;
71+
}

postgraphiql/yarn.lock

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,18 +2344,18 @@ code-point-at@^1.0.0:
23442344
resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77"
23452345
integrity sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=
23462346

2347-
codemirror-graphql@^0.7.1:
2348-
version "0.7.1"
2349-
resolved "https://registry.yarnpkg.com/codemirror-graphql/-/codemirror-graphql-0.7.1.tgz#64b995643d511b9aa8f85eeeb2feac7aeb4b94d4"
2350-
integrity sha512-HtHXMJAn6iGJYpijkzi3IlqWIdGrB6V0RqJ607yffJTCKk/OgaNtdLOb8hZJyEtHfkw7PZDaKybMAVCi6ScWSQ==
2347+
codemirror-graphql@^0.8.3:
2348+
version "0.8.3"
2349+
resolved "https://registry.yarnpkg.com/codemirror-graphql/-/codemirror-graphql-0.8.3.tgz#8de806d418f72121ccfd9820594aa306ac0d3366"
2350+
integrity sha512-ZipSnPXFKDMThfvfTKTAt1dQmuGctVNann8hTZg6017+vwOcGpIqCuQIZLRDw/Y3zZfCyydRARHgbSydSCXpow==
23512351
dependencies:
23522352
graphql-language-service-interface "^1.3.2"
23532353
graphql-language-service-parser "^1.2.2"
23542354

2355-
codemirror@^5.26.0:
2356-
version "5.46.0"
2357-
resolved "https://registry.yarnpkg.com/codemirror/-/codemirror-5.46.0.tgz#be3591572f88911e0105a007c324856a9ece0fb7"
2358-
integrity sha512-3QpMge0vg4QEhHW3hBAtCipJEWjTJrqLLXdIaWptJOblf1vHFeXLNtFhPai/uX2lnFCehWNk4yOdaMR853Z02w==
2355+
codemirror@^5.47.0:
2356+
version "5.48.2"
2357+
resolved "https://registry.yarnpkg.com/codemirror/-/codemirror-5.48.2.tgz#a9dd3d426dea4cd59efd59cd98e20a9152a30922"
2358+
integrity sha512-i9VsmC8AfA5ji6EDIZ+aoSe4vt9FcwPLdHB1k1ItFbVyuOFRrcfvnoKqwZlC7EVA2UmTRiNEypE4Uo7YvzVY8Q==
23592359

23602360
collection-visit@^1.0.0:
23612361
version "1.0.0"
@@ -2556,6 +2556,13 @@ copy-descriptor@^0.1.0:
25562556
resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d"
25572557
integrity sha1-Z29us8OZl8LuGsOpJP1hJHSPV40=
25582558

2559+
copy-to-clipboard@^3.2.0:
2560+
version "3.2.0"
2561+
resolved "https://registry.yarnpkg.com/copy-to-clipboard/-/copy-to-clipboard-3.2.0.tgz#d2724a3ccbfed89706fac8a894872c979ac74467"
2562+
integrity sha512-eOZERzvCmxS8HWzugj4Uxl8OJxa7T2k1Gi0X5qavwydHIfuSHq2dTD09LOg/XyGq4Zpb5IsR/2OJ5lbOegz78w==
2563+
dependencies:
2564+
toggle-selection "^1.0.6"
2565+
25592566
core-js-compat@^3.0.0:
25602567
version "3.0.1"
25612568
resolved "https://registry.yarnpkg.com/core-js-compat/-/core-js-compat-3.0.1.tgz#bff73ba31ca8687431b9c88f78d3362646fb76f0"
@@ -4158,18 +4165,19 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.15, graceful-fs@^4.1.2, graceful-fs@^4.1.6
41584165
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.1.15.tgz#ffb703e1066e8a0eeaa4c8b80ba9253eeefbfb00"
41594166
integrity sha512-6uHUhOPEBgQ24HM+r6b/QwWfZq+yiFcipKFrOFiBEnWdy5sdzYoi+pJeQaPI5qOLRFqWmAXUPQNsielzdLoecA==
41604167

4161-
graphiql-explorer@^0.3.7:
4162-
version "0.3.7"
4163-
resolved "https://registry.yarnpkg.com/graphiql-explorer/-/graphiql-explorer-0.3.7.tgz#92f1c0860d6371754373cb5107db8672dc3ee381"
4164-
integrity sha512-i24+xTe012/OAMFH/r9YL5QarSr/YqkP0Si0NspCnXKmJv7HzHueFUTRpFRPF1Ka9dLqiszrmOg/5stU4gCQnA==
4168+
graphiql-explorer@^0.4.3:
4169+
version "0.4.3"
4170+
resolved "https://registry.yarnpkg.com/graphiql-explorer/-/graphiql-explorer-0.4.3.tgz#86a60292faf96462e73530035ae84e678f27c0ea"
4171+
integrity sha512-8WST2MfVPesgspCDYOucZHhWjeL0du8GB/vy2o44qrI/Y0iLaZ9BwCVw6jEz658S8AgxcAkKBl1jPCkMRxsdsQ==
41654172

4166-
"graphiql@npm:@benjie/graphiql@0.13.1-benjie.0":
4167-
version "0.13.1-benjie.0"
4168-
resolved "https://registry.yarnpkg.com/@benjie/graphiql/-/graphiql-0.13.1-benjie.0.tgz#4967bbbbb0d5f64ee41616e07e9f5adac3dce418"
4169-
integrity sha512-tjMOpubpkKBZY6/uGApPTsHav5gwxLFmVnJL+tzaaDi0G+O+LfbERbU8Stwix4OInAcwZ4qnSR+PGVzmKWSCHw==
4173+
graphiql@^0.13.2:
4174+
version "0.13.2"
4175+
resolved "https://registry.yarnpkg.com/graphiql/-/graphiql-0.13.2.tgz#cdd0f3eb656a82e9787531ae27433819ca455d58"
4176+
integrity sha512-4N2HmQQpUfApS1cxrTtoZ15tnR3EW88oUiqmza6GgNQYZZfDdBGphdQlBYsKcjAB/SnIOJort+RA1dB6kf4M7Q==
41704177
dependencies:
4171-
codemirror "^5.26.0"
4172-
codemirror-graphql "^0.7.1"
4178+
codemirror "^5.47.0"
4179+
codemirror-graphql "^0.8.3"
4180+
copy-to-clipboard "^3.2.0"
41734181
markdown-it "^8.4.0"
41744182

41754183
@@ -9168,6 +9176,11 @@ to-regex@^3.0.1, to-regex@^3.0.2:
91689176
regex-not "^1.0.2"
91699177
safe-regex "^1.1.0"
91709178

9179+
toggle-selection@^1.0.6:
9180+
version "1.0.6"
9181+
resolved "https://registry.yarnpkg.com/toggle-selection/-/toggle-selection-1.0.6.tgz#6e45b1263f2017fa0acc7d89d78b15b8bf77da32"
9182+
integrity sha1-bkWxJj8gF/oKzH2J14sVuL932jI=
9183+
91719184
91729185
version "3.0.3"
91739186
resolved "https://registry.yarnpkg.com/topo/-/topo-3.0.3.tgz#d5a67fb2e69307ebeeb08402ec2a2a6f5f7ad95c"

0 commit comments

Comments
 (0)