Is it possible to share GraphiQL queries and variables via URL parameters? #3450
-
|
Background Query Sharing Feature Documentation Review Specifics Additional Information Thank you for your assistance and for the great work on GraphiQL! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Yes it's possible! Our demo implementation ships with this: It can also now be achieved with the react sdk, without using props, here is an example: |
Beta Was this translation helpful? Give feedback.
-
|
I'm not using the React SDK in the way it's used at saleor/saleor-graphql-playground#23, so I opted for the first option, but that link is broken...fortunately I found it in an earlier commit: Also, the API has changed slightly so I had to make a few changes. Here is my working version: import React from 'react';
import ReactDOM from 'react-dom/client';
import { GraphiQL, HISTORY_PLUGIN } from 'graphiql';
import { createGraphiQLFetcher } from '@graphiql/toolkit';
import { explorerPlugin } from '@graphiql/plugin-explorer';
import 'graphiql/setup-workers/esm.sh';
// Below is some custom URL handling code to include the query in the URL, so it can
// be shared with others, using the recommendation at https://github.com/graphql/graphiql/discussions/3450
// Parse the search string to get url parameters.
var search = window.location.search;
var parameters = {};
search
.substr(1)
.split('&')
.forEach(function (entry) {
var eq = entry.indexOf('=');
if (eq >= 0) {
parameters[decodeURIComponent(entry.slice(0, eq))] = decodeURIComponent(
entry.slice(eq + 1),
);
}
});
// When the query and variables string is edited, update the URL bar so
// that it can be easily shared.
function onEditQuery(newQuery) {
parameters.query = newQuery;
updateURL();
}
function onEditVariables(newVariables) {
parameters.variables = newVariables;
updateURL();
}
function onEditHeaders(newHeaders) {
parameters.headers = newHeaders;
updateURL();
}
function onEditOperationName(newOperationName) {
parameters.operationName = newOperationName;
updateURL();
}
function onTabChange(tabsState) {
const activeTab = tabsState.tabs[tabsState.activeTabIndex];
parameters.query = activeTab.query;
parameters.variables = activeTab.variables;
parameters.headers = activeTab.headers;
parameters.operationName = activeTab.operationName;
updateURL();
}
function updateURL() {
var newSearch =
'?' +
Object.keys(parameters)
.filter(function (key) {
return Boolean(parameters[key]);
})
.map(function (key) {
return (
encodeURIComponent(key) + '=' + encodeURIComponent(parameters[key])
);
})
.join('&');
history.replaceState(null, null, newSearch);
}
const fetcher = createGraphiQLFetcher({
url: '/graphql',
});
const plugins = [HISTORY_PLUGIN, explorerPlugin()];
function App() {
return React.createElement(GraphiQL, {
fetcher,
plugins,
defaultEditorToolsVisibility: true,
initialQuery: parameters.query,
initialVariables: parameters.variables,
initialHeaders: parameters.headers,
initialOperationName: parameters.operationName,
onEditQuery,
onEditVariables,
onEditHeaders,
onEditOperationName,
onTabChange,
});
}
const container = document.getElementById('graphiql');
const root = ReactDOM.createRoot(container);
root.render(React.createElement(App));Bonus: if you want to default to the dark theme, add this after the declaration of // default to dark theme
if (!window.localStorage.getItem('graphiql:theme')) {
window.localStorage.setItem('graphiql:theme', 'dark');
} |
Beta Was this translation helpful? Give feedback.
Yes it's possible! Our demo implementation ships with this:
https://github.com/graphql/graphiql/blob/main/packages/graphiql/resources/renderExample.js
It can also now be achieved with the react sdk, without using props, here is an example:
saleor/saleor-graphql-playground#23