From eb2d91fa8e4a03cb5663f27f724db2c95989a40f Mon Sep 17 00:00:00 2001 From: Harshith Pabbati Date: Sat, 10 Jul 2021 15:51:51 +0530 Subject: [PATCH] fix: new util to manage historyStore outside of query history component (#1914) --- .changeset/short-mirrors-occur.md | 6 + packages/graphiql/README.md | 1 + packages/graphiql/src/components/GraphiQL.tsx | 21 ++- .../graphiql/src/components/QueryHistory.tsx | 177 +++++------------- .../components/__tests__/GraphiQL.spec.tsx | 15 ++ packages/graphiql/src/utility/HistoryStore.ts | 158 ++++++++++++++++ 6 files changed, 246 insertions(+), 132 deletions(-) create mode 100644 .changeset/short-mirrors-occur.md create mode 100644 packages/graphiql/src/utility/HistoryStore.ts diff --git a/.changeset/short-mirrors-occur.md b/.changeset/short-mirrors-occur.md new file mode 100644 index 00000000000..d6e233f420d --- /dev/null +++ b/.changeset/short-mirrors-occur.md @@ -0,0 +1,6 @@ +--- +'graphiql': minor +--- + +fix: history can now be saved even when query history panel is not opened +feat: create a new maxHistoryLength prop to allow more than 20 queries in history panel diff --git a/packages/graphiql/README.md b/packages/graphiql/README.md index 9e8b01bd611..47c44f37cff 100644 --- a/packages/graphiql/README.md +++ b/packages/graphiql/README.md @@ -216,6 +216,7 @@ For more details on props, see the [API Docs](https://graphiql-test.netlify.app/ | `onEditHeaders` | `Function` | called when the request headers editor changes. The argument to the function will be the headers string. | | `onEditOperationName` | `Function` | called when the operation name to be executed changes. | | `onToggleDocs` | `Function` | called when the docs will be toggled. The argument to the function will be a boolean whether the docs are now open or closed. | +| `maxHistoryLength` | `number` | **Default:** 20. allows you to increase the number of queries in the history component | 20 | ### Children (this pattern will be dropped in 2.0.0) diff --git a/packages/graphiql/src/components/GraphiQL.tsx b/packages/graphiql/src/components/GraphiQL.tsx index 943953958cc..c90276353af 100644 --- a/packages/graphiql/src/components/GraphiQL.tsx +++ b/packages/graphiql/src/components/GraphiQL.tsx @@ -64,6 +64,7 @@ import type { Unsubscribable, FetcherResultPayload, } from '@graphiql/toolkit'; +import HistoryStore from '../utility/HistoryStore'; const DEFAULT_DOC_EXPLORER_WIDTH = 350; @@ -123,6 +124,7 @@ export type GraphiQLProps = { readOnly?: boolean; docExplorerOpen?: boolean; toolbar?: GraphiQLToolbarConfig; + maxHistoryLength?: number; }; export type GraphiQLState = { @@ -147,6 +149,7 @@ export type GraphiQLState = { variableToType?: VariableToType; operations?: OperationDefinitionNode[]; documentAST?: DocumentNode; + maxHistoryLength: number; }; /** @@ -185,6 +188,7 @@ export class GraphiQL extends React.Component { variableEditorComponent: Maybe; headerEditorComponent: Maybe; _queryHistory: Maybe; + _historyStore: Maybe; editorBarComponent: Maybe; queryEditorComponent: Maybe; resultViewerElement: Maybe; @@ -200,6 +204,10 @@ export class GraphiQL extends React.Component { // Cache the storage instance this._storage = new StorageAPI(props.storage); + const maxHistoryLength = props.maxHistoryLength ?? 20; + + this._historyStore = new HistoryStore(this._storage, maxHistoryLength); + // Disable setState when the component is not mounted this.componentIsMounted = false; @@ -285,6 +293,7 @@ export class GraphiQL extends React.Component { DEFAULT_DOC_EXPLORER_WIDTH, isWaitingForResponse: false, subscription: null, + maxHistoryLength, ...queryFacts, }; } @@ -493,6 +502,7 @@ export class GraphiQL extends React.Component { variables={this.state.variables} onSelectQuery={this.handleSelectHistoryQuery} storage={this._storage} + maxHistoryLength={this.state.maxHistoryLength} queryID={this._editorQueryID}>