Skip to content

Commit

Permalink
move HistoryStore to @graphiql/toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheyenbrock committed May 19, 2022
1 parent d816dc2 commit 966dee3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 51 deletions.
6 changes: 6 additions & 0 deletions .changeset/friendly-cougars-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'graphiql': minor
'@graphiql/toolkit': minor
---

Move HistoryStore from `graphiql` package to `@graphiql/toolkit`
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
/**
* Copyright (c) 2021 GraphQL Contributors.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { QueryStore, QueryStoreItem, StorageAPI } from '@graphiql/toolkit';
import { parse } from 'graphql';
import {
HandleEditLabelFn,
HandleToggleFavoriteFn,
} from '../components/HistoryQuery';

import { StorageAPI } from './base';
import { QueryStore, QueryStoreItem } from './query';

const MAX_QUERY_SIZE = 100000;

export default class HistoryStore {
export class HistoryStore {
queries: Array<QueryStoreItem>;
history: QueryStore;
favorite: QueryStore;
Expand All @@ -26,15 +18,16 @@ export default class HistoryStore {
);
// favorites are not automatically deleted, so there's no need for a max length
this.favorite = new QueryStore('favorites', this.storage, null);
this.queries = this.fetchAllQueries();

this.queries = [...this.history.fetchAll(), ...this.favorite.fetchAll()];
}

shouldSaveQuery = (
private shouldSaveQuery(
query?: string,
variables?: string,
headers?: string,
lastQuerySaved?: QueryStoreItem,
) => {
) {
if (!query) {
return false;
}
Expand Down Expand Up @@ -70,15 +63,8 @@ export default class HistoryStore {
}
}
return true;
};

fetchAllQueries = () => {
const historyQueries = this.history.fetchAll();
const favoriteQueries = this.favorite.fetchAll();
return historyQueries.concat(favoriteQueries);
};
}

// Public API
updateHistory = (
query?: string,
variables?: string,
Expand All @@ -105,15 +91,14 @@ export default class HistoryStore {
}
};

// Public API
toggleFavorite: HandleToggleFavoriteFn = (
query,
variables,
headers,
operationName,
label,
favorite,
) => {
toggleFavorite(
query?: string,
variables?: string,
headers?: string,
operationName?: string,
label?: string,
favorite?: boolean,
) {
const item: QueryStoreItem = {
query,
variables,
Expand All @@ -129,17 +114,16 @@ export default class HistoryStore {
this.favorite.delete(item);
}
this.queries = [...this.history.items, ...this.favorite.items];
};
}

// Public API
editLabel: HandleEditLabelFn = (
query,
variables,
headers,
operationName,
label,
favorite,
) => {
editLabel(
query?: string,
variables?: string,
headers?: string,
operationName?: string,
label?: string,
favorite?: boolean,
) {
const item = {
query,
variables,
Expand All @@ -153,5 +137,5 @@ export default class HistoryStore {
this.history.edit(item);
}
this.queries = [...this.history.items, ...this.favorite.items];
};
}
}
1 change: 1 addition & 0 deletions packages/graphiql-toolkit/src/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './base';
export * from './history';
export * from './query';
11 changes: 5 additions & 6 deletions packages/graphiql/src/components/GraphiQL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,20 @@ import {
fetcherReturnToPromise,
formatError,
formatResult,
isPromise,
isObservable,
HistoryStore,
isAsyncIterable,
isObservable,
isPromise,
StorageAPI,
} from '@graphiql/toolkit';
import type {
Fetcher,
FetcherResult,
FetcherOpts,
FetcherResult,
FetcherResultPayload,
SyncFetcherResult,
Unsubscribable,
FetcherResultPayload,
Storage,
} from '@graphiql/toolkit';
import HistoryStore from '../utility/HistoryStore';

import { validateSchema } from 'graphql';
import { Tab, TabAddButton, Tabs } from './Tabs';
Expand Down
4 changes: 2 additions & 2 deletions packages/graphiql/src/components/QueryHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
* LICENSE file in the root directory of this source tree.
*/

import { QueryStoreItem, StorageAPI } from '@graphiql/toolkit';
import { HistoryStore, QueryStoreItem, StorageAPI } from '@graphiql/toolkit';
import React, { ReactNode } from 'react';

import HistoryQuery, {
HandleEditLabelFn,
HandleSelectQueryFn,
HandleToggleFavoriteFn,
} from './HistoryQuery';
import HistoryStore from '../utility/HistoryStore';

type QueryHistoryProps = {
onSelectQuery: HandleSelectQueryFn;
Expand Down

0 comments on commit 966dee3

Please sign in to comment.