Skip to content

Commit f77b413

Browse files
kiendangsuperlevure
authored andcommitted
Add GraphiQL Explorer plugin (graphql-python#1397)
1 parent fd4178c commit f77b413

File tree

3 files changed

+45
-15
lines changed

3 files changed

+45
-15
lines changed

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,
@@ -98,24 +99,44 @@
9899
function updateURL() {
99100
history.replaceState(null, null, locationQuery(parameters));
100101
}
101-
var options = {
102-
fetcher: graphQLFetcher,
103-
onEditQuery: onEditQuery,
104-
onEditVariables: onEditVariables,
105-
onEditOperationName: onEditOperationName,
106-
isHeadersEditorEnabled: GRAPHENE_SETTINGS.graphiqlHeaderEditorEnabled,
107-
shouldPersistHeaders: GRAPHENE_SETTINGS.graphiqlShouldPersistHeaders,
108-
query: parameters.query,
109-
};
110-
if (parameters.variables) {
111-
options.variables = parameters.variables;
112-
}
113-
if (parameters.operation_name) {
114-
options.operationName = parameters.operation_name;
102+
103+
function GraphiQLWithExplorer() {
104+
var [query, setQuery] = React.useState(parameters.query);
105+
106+
function handleQuery(query) {
107+
setQuery(query);
108+
onEditQuery(query);
109+
}
110+
111+
var explorerPlugin = GraphiQLPluginExplorer.useExplorerPlugin({
112+
query: query,
113+
onEdit: handleQuery,
114+
});
115+
116+
var options = {
117+
fetcher: graphQLFetcher,
118+
plugins: [explorerPlugin],
119+
defaultEditorToolsVisibility: true,
120+
onEditQuery: handleQuery,
121+
onEditVariables: onEditVariables,
122+
onEditOperationName: onEditOperationName,
123+
isHeadersEditorEnabled: GRAPHENE_SETTINGS.graphiqlHeaderEditorEnabled,
124+
shouldPersistHeaders: GRAPHENE_SETTINGS.graphiqlShouldPersistHeaders,
125+
query: query,
126+
};
127+
if (parameters.variables) {
128+
options.variables = parameters.variables;
129+
}
130+
if (parameters.operation_name) {
131+
options.operationName = parameters.operation_name;
132+
}
133+
134+
return React.createElement(GraphiQL, options);
115135
}
136+
116137
// Render <GraphiQL /> into the body.
117138
ReactDOM.render(
118-
React.createElement(GraphiQL, options),
139+
React.createElement(GraphiQLWithExplorer),
119140
document.getElementById("editor"),
120141
);
121142
})(
@@ -126,6 +147,7 @@
126147
window.React,
127148
window.ReactDOM,
128149
window.graphqlWs,
150+
window.GraphiQLPluginExplorer,
129151
window.fetch,
130152
window.history,
131153
window.location,

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>

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)