Skip to content

Commit

Permalink
new hooks: useOperationsEditorState()
Browse files Browse the repository at this point in the history
this abstracts away some editor specific details
until we can leverage `<Suspense />`

- add useOperationsEditorState()
- add useVariablesEditorState()
- match up @graphiql/react globals
- simplify plugin implementations
  • Loading branch information
acao committed Jul 12, 2023
1 parent 595525c commit 35937bd
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 18 deletions.
7 changes: 3 additions & 4 deletions packages/graphiql-plugin-code-exporter/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEditorContext, type GraphiQLPlugin } from '@graphiql/react';
import { useOperationsEditorState, type GraphiQLPlugin } from '@graphiql/react';
import React from 'react';
import GraphiQLCodeExporter, {
GraphiQLCodeExporterProps,
Expand All @@ -10,13 +10,12 @@ import './index.css';
type GraphiQLCodeExporterPluginProps = Omit<GraphiQLCodeExporterProps, 'query'>;

function GraphiQLCodeExporterPlugin(props: GraphiQLCodeExporterPluginProps) {
const { queryEditor } = useEditorContext({ nonNull: true });

const [operationsString] = useOperationsEditorState();
return (
<GraphiQLCodeExporter
codeMirrorTheme="graphiql"
{...props}
query={queryEditor!.getValue()}
query={operationsString}
/>
);
}
Expand Down
1 change: 1 addition & 0 deletions packages/graphiql-plugin-code-exporter/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default defineConfig({
output: {
chunkFileNames: '[name].[format].js',
globals: {
'@graphiql/react': 'GraphiQL.React',
graphql: 'GraphiQL.GraphQL',
react: 'React',
'react-dom': 'ReactDOM',
Expand Down
28 changes: 15 additions & 13 deletions packages/graphiql-plugin-explorer/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
useEditorContext,
useExecutionContext,
useSchemaContext,
useOperationsEditorState,
} from '@graphiql/react';
import {
Explorer as GraphiQLExplorer,
Expand Down Expand Up @@ -116,30 +117,29 @@ const styles = {
};

export type GraphiQLExplorerPluginProps = Omit<
Omit<GraphiQLExplorerProps, 'query'>,
'onEdit'
GraphiQLExplorerProps,
'onEdit' | 'query'
>;

function ExplorerPlugin(props: GraphiQLExplorerPluginProps) {
const { setOperationName, queryEditor } = useEditorContext({ nonNull: true });
const { setOperationName } = useEditorContext({ nonNull: true });
const { schema } = useSchemaContext({ nonNull: true });
const { run } = useExecutionContext({ nonNull: true });

// handle running the current operation from the plugin
const handleRunOperation = useCallback(
(operationName: string | null) => {
if (operationName) {
// set the plugin-defined operation name before executing
setOperationName(operationName);
}
run();
},
[run, setOperationName],
);
// todo: document how to do this!
const handleEditOperation = useCallback(
(value: string) => queryEditor?.setValue(value),
[queryEditor],
);

const operationDocument = queryEditor?.getValue() ?? '';
// load the current editor tab state into the explorer
const [operationsString, handleEditOperations] = useOperationsEditorState();

return (
<GraphiQLExplorer
Expand All @@ -152,14 +152,16 @@ function ExplorerPlugin(props: GraphiQLExplorerPluginProps) {
checkboxUnchecked={checkboxUnchecked}
checkboxChecked={checkboxChecked}
styles={styles}
query={operationDocument}
onEdit={handleEditOperation}
query={operationsString}
onEdit={handleEditOperations}
{...props}
/>
);
}

export function explorerPlugin(props: GraphiQLExplorerPluginProps) {
export function explorerPlugin(
props: GraphiQLExplorerPluginProps,
): GraphiQLPlugin {
return {
title: 'GraphiQL Explorer',
icon: () => (
Expand All @@ -185,5 +187,5 @@ export function explorerPlugin(props: GraphiQLExplorerPluginProps) {
</svg>
),
content: () => <ExplorerPlugin {...props} />,
} as GraphiQLPlugin;
};
}
46 changes: 45 additions & 1 deletion packages/graphiql-react/src/editor/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { EditorChange, EditorConfiguration } from 'codemirror';
import type { SchemaReference } from 'codemirror-graphql/utils/SchemaReference';
import copyToClipboard from 'copy-to-clipboard';
import { parse, print } from 'graphql';
import { useCallback, useEffect } from 'react';
import { useCallback, useEffect, useMemo } from 'react';

import { useExplorerContext } from '../explorer';
import { usePluginContext } from '../plugin';
Expand Down Expand Up @@ -332,3 +332,47 @@ export function useAutoCompleteLeafs({
return result;
}, [getDefaultFieldNames, queryEditor, schema]);
}

// https://react.dev/learn/you-might-not-need-an-effect

/**
* useState-like hook for current tab operations editor state
*/
export function useOperationsEditorState(): [
opString: string,
handleEditOperations: (content: string) => void,
] {
const { queryEditor } = useEditorContext({
nonNull: true,
});
const opString = queryEditor?.getValue() ?? '';
const handleEditOperations = useCallback(
(value: string) => queryEditor?.setValue(value),
[queryEditor],
);
return useMemo(
() => [opString, handleEditOperations],
[opString, handleEditOperations],
);
}

/**
* useState-like hook for variables tab operations editor state
*/
export function useVariablesEditorState(): [
varsString: string,
handleEditVariables: (content: string) => void,
] {
const { variableEditor } = useEditorContext({
nonNull: true,
});
const varsString = variableEditor?.getValue() ?? '';
const handleEditVariables = useCallback(
(value: string) => variableEditor?.setValue(value),
[variableEditor],
);
return useMemo(
() => [varsString, handleEditVariables],
[varsString, handleEditVariables],
);
}
2 changes: 2 additions & 0 deletions packages/graphiql-react/src/editor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export {
useCopyQuery,
useMergeQuery,
usePrettifyEditors,
useOperationsEditorState,
useVariablesEditorState,
} from './hooks';
export { useQueryEditor } from './query-editor';
export { useResponseEditor } from './response-editor';
Expand Down
2 changes: 2 additions & 0 deletions packages/graphiql-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export {
useQueryEditor,
useResponseEditor,
useVariableEditor,
useOperationsEditorState,
useVariablesEditorState,
VariableEditor,
} from './editor';
export {
Expand Down

0 comments on commit 35937bd

Please sign in to comment.