Skip to content

Commit a9e9f9b

Browse files
committed
Add GraphiQL Explorer plugin
1 parent 3283d0b commit a9e9f9b

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

Diff for: graphene_django/static/graphene_django/graphiql.js

+37-15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
React,
77
ReactDOM,
88
graphqlWs,
9+
GraphiQLPluginExplorer,
910
fetch,
1011
history,
1112
location,
@@ -111,24 +112,44 @@
111112
function updateURL() {
112113
history.replaceState(null, null, locationQuery(parameters));
113114
}
114-
var options = {
115-
fetcher: graphQLFetcher,
116-
onEditQuery: onEditQuery,
117-
onEditVariables: onEditVariables,
118-
onEditOperationName: onEditOperationName,
119-
isHeadersEditorEnabled: GRAPHENE_SETTINGS.graphiqlHeaderEditorEnabled,
120-
shouldPersistHeaders: GRAPHENE_SETTINGS.graphiqlShouldPersistHeaders,
121-
query: parameters.query,
122-
};
123-
if (parameters.variables) {
124-
options.variables = parameters.variables;
125-
}
126-
if (parameters.operation_name) {
127-
options.operationName = parameters.operation_name;
115+
116+
function GraphiQLWithExplorer() {
117+
var [query, setQuery] = React.useState(parameters.query);
118+
119+
function handleQuery(query) {
120+
setQuery(query);
121+
onEditQuery(query);
122+
}
123+
124+
var explorerPlugin = GraphiQLPluginExplorer.useExplorerPlugin({
125+
query: query,
126+
onEdit: handleQuery,
127+
});
128+
129+
var options = {
130+
fetcher: graphQLFetcher,
131+
plugins: [explorerPlugin],
132+
defaultEditorToolsVisibility: true,
133+
onEditQuery: handleQuery,
134+
onEditVariables: onEditVariables,
135+
onEditOperationName: onEditOperationName,
136+
isHeadersEditorEnabled: GRAPHENE_SETTINGS.graphiqlHeaderEditorEnabled,
137+
shouldPersistHeaders: GRAPHENE_SETTINGS.graphiqlShouldPersistHeaders,
138+
query: query,
139+
};
140+
if (parameters.variables) {
141+
options.variables = parameters.variables;
142+
}
143+
if (parameters.operation_name) {
144+
options.operationName = parameters.operation_name;
145+
}
146+
147+
return React.createElement(GraphiQL, options);
128148
}
149+
129150
// Render <GraphiQL /> into the body.
130151
ReactDOM.render(
131-
React.createElement(GraphiQL, options),
152+
React.createElement(GraphiQLWithExplorer),
132153
document.getElementById("editor"),
133154
);
134155
})(
@@ -139,6 +160,7 @@
139160
window.React,
140161
window.ReactDOM,
141162
window.graphqlWs,
163+
window.GraphiQLPluginExplorer,
142164
window.fetch,
143165
window.history,
144166
window.location,

Diff for: graphene_django/templates/graphene/graphiql.html

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
<script src="https://cdn.jsdelivr.net/npm/graphql-ws@{{subscriptions_transport_ws_version}}/umd/graphql-ws.min.js"
3737
integrity="{{subscriptions_transport_ws_sri}}"
3838
crossorigin="anonymous"></script>
39+
<script src="https://cdn.jsdelivr.net/npm/@graphiql/plugin-explorer@{{graphiql_plugin_explorer_version}}/dist/graphiql-plugin-explorer.umd.js"
40+
integrity="{{graphiql_plugin_explorer_sri}}"
41+
crossorigin="anonymous"></script>
3942
</head>
4043
<body>
4144
<div id="editor"></div>

Diff for: graphene_django/views.py

+5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ class GraphQLView(View):
7676
"sha256-EZhvg6ANJrBsgLvLAa0uuHNLepLJVCFYS+xlb5U/bqw="
7777
)
7878

79+
graphiql_plugin_explorer_version = "0.1.15"
80+
graphiql_plugin_explorer_sri = "sha256-3hUuhBXdXlfCj6RTeEkJFtEh/kUG+TCDASFpFPLrzvE="
81+
7982
schema = None
8083
graphiql = False
8184
middleware = None
@@ -158,6 +161,8 @@ def dispatch(self, request, *args, **kwargs):
158161
graphiql_css_sri=self.graphiql_css_sri,
159162
subscriptions_transport_ws_version=self.subscriptions_transport_ws_version,
160163
subscriptions_transport_ws_sri=self.subscriptions_transport_ws_sri,
164+
graphiql_plugin_explorer_version=self.graphiql_plugin_explorer_version,
165+
graphiql_plugin_explorer_sri=self.graphiql_plugin_explorer_sri,
161166
# The SUBSCRIPTION_PATH setting.
162167
subscription_path=self.subscription_path,
163168
# GraphiQL headers tab,

0 commit comments

Comments
 (0)