App.tsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { createGraphiQLFetcher } from '@graphiql/toolkit';
  2. import { GraphiQL } from 'graphiql';
  3. import 'graphiql/graphiql.css';
  4. import './overrides.css';
  5. import React, { useCallback } from 'react';
  6. import { embeddedModeStorage } from './embedded-mode-storage';
  7. interface AppProps {
  8. apiType: 'admin' | 'shop';
  9. }
  10. const App: React.FC<AppProps> = ({ apiType }) => {
  11. const { adminApiUrl, shopApiUrl } = window.GRAPHIQL_SETTINGS ?? {
  12. adminApiUrl: 'http://localhost:3000/admin-api',
  13. shopApiUrl: 'http://localhost:3000/shop-api',
  14. };
  15. const apiUrl = apiType === 'admin' ? adminApiUrl : shopApiUrl;
  16. const apiName = apiType === 'admin' ? 'Admin API' : 'Shop API';
  17. const params = new URLSearchParams(window.location.search);
  18. const query = params.get('query') ?? undefined;
  19. const embeddedMode = params.get('embeddedMode') === 'true';
  20. const storage = embeddedMode ? embeddedModeStorage : undefined;
  21. const fetcher = useCallback(() => {
  22. return createGraphiQLFetcher({
  23. url: apiUrl,
  24. subscriptionUrl: apiUrl.replace(/^http/, 'ws'),
  25. });
  26. }, [apiUrl]);
  27. const handleSwitchApi = (newApiType: 'admin' | 'shop') => {
  28. if (newApiType !== apiType) {
  29. const pathParts = window.location.pathname.split('/');
  30. const basePath = pathParts.length > 1 ? `/${pathParts[1]}` : '';
  31. window.location.href = `${basePath}/${newApiType}`;
  32. }
  33. };
  34. return (
  35. <div className="graphiql-app">
  36. {!embeddedMode ? <div className="vendure-header">
  37. <h1>Vendure GraphiQL - {apiName}</h1>
  38. <div className="switch-api">
  39. <span>Switch API:</span>
  40. <a
  41. href="#"
  42. className={apiType === 'admin' ? 'active' : ''}
  43. onClick={e => {
  44. e.preventDefault();
  45. handleSwitchApi('admin');
  46. }}
  47. >
  48. Admin API
  49. </a>
  50. <a
  51. href="#"
  52. className={apiType === 'shop' ? 'active' : ''}
  53. onClick={e => {
  54. e.preventDefault();
  55. handleSwitchApi('shop');
  56. }}
  57. >
  58. Shop API
  59. </a>
  60. </div>
  61. </div> : null}
  62. <div className="graphiql-wrapper">
  63. <GraphiQL className={embeddedMode ? 'embedded-mode' : undefined} fetcher={fetcher()} storage={storage}
  64. defaultEditorToolsVisibility={embeddedMode ? false : true} query={query} />
  65. </div>
  66. </div>
  67. );
  68. };
  69. export default App;