demarches-normaliennes/app/javascript/entrypoints/playground.ts

37 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-03-18 12:57:44 +01:00
import { createGraphiQLFetcher } from '@graphiql/toolkit';
2023-09-01 11:04:34 +02:00
import { explorerPlugin } from '@graphiql/plugin-explorer';
2023-03-18 12:57:44 +01:00
import { GraphiQL } from 'graphiql';
import { createElement, useState } from 'react';
import { createRoot } from 'react-dom/client';
import { getConfig, csrfToken } from '@utils';
import 'graphiql/graphiql.css';
import '@graphiql/plugin-explorer/dist/style.css';
const { defaultQuery, defaultVariables } = getConfig();
const fetcher = createGraphiQLFetcher({
url: '/api/v2/graphql',
headers: { 'x-csrf-token': csrfToken() ?? '' }
});
function GraphiQLWithExplorer() {
const [query, setQuery] = useState(defaultQuery);
2023-09-01 11:04:34 +02:00
const explorer = explorerPlugin({ showAttribution: false });
2023-03-18 12:57:44 +01:00
return createElement(GraphiQL, {
fetcher: fetcher,
defaultEditorToolsVisibility: true,
2023-09-01 11:04:34 +02:00
plugins: [explorer],
2023-03-18 12:57:44 +01:00
query: query,
variables: defaultVariables,
onEditQuery: setQuery,
isHeadersEditorEnabled: false
});
}
const element = document.getElementById('playground');
if (element) {
const root = createRoot(element);
root.render(createElement(GraphiQLWithExplorer));
}