Skip to content

Commit cff3da5

Browse files
authored
graphiql 5: extract graphiql sidebar to react component (#4017)
* extract sidebar into component * upd * add changeset * fix cypress
1 parent c4d3925 commit cff3da5

File tree

10 files changed

+374
-324
lines changed

10 files changed

+374
-324
lines changed

.changeset/orange-spies-poke.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphiql/react': minor
3+
'graphiql': minor
4+
---
5+
6+
extract graphiql sidebar to react component

packages/graphiql-react/src/stores/editor.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,11 @@ export const createEditorSlice: CreateEditorSlice = initial => (set, get) => {
467467
set(newState);
468468
},
469469
setOperationName(operationName) {
470-
const { onEditOperationName, actions } = get();
471-
set({ operationName });
472-
actions.updateActiveTabValues({ operationName });
473-
onEditOperationName?.(operationName);
470+
set(({ onEditOperationName, actions }) => {
471+
actions.updateActiveTabValues({ operationName });
472+
onEditOperationName?.(operationName);
473+
return { operationName };
474+
});
474475
},
475476
setShouldPersistHeaders(persist) {
476477
const { headerEditor, tabs, activeTabIndex } = get();
@@ -486,8 +487,8 @@ export const createEditorSlice: CreateEditorSlice = initial => (set, get) => {
486487
storage.set(STORAGE_KEY.headers, '');
487488
clearHeadersFromTabs();
488489
}
489-
set({ shouldPersistHeaders: persist });
490490
storage.set(STORAGE_KEY.persistHeaders, persist.toString());
491+
set({ shouldPersistHeaders: persist });
491492
},
492493
storeTabs({ tabs, activeTabIndex }) {
493494
const { storage } = storageStore.getState();

packages/graphiql-react/src/stores/execution.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@ import {
88
isObservable,
99
Unsubscribable,
1010
} from '@graphiql/toolkit';
11-
import {
12-
ExecutionResult,
13-
FragmentDefinitionNode,
14-
GraphQLError,
15-
print,
16-
} from 'graphql';
11+
import { ExecutionResult, GraphQLError, print } from 'graphql';
1712
import { getFragmentDependenciesForAST } from 'graphql-language-service';
1813
import setValue from 'set-value';
1914
import getValue from 'get-value';
@@ -185,9 +180,10 @@ export const createExecutionSlice: CreateExecutionSlice =
185180
queryId: 0,
186181
actions: {
187182
stop() {
188-
const { subscription } = get();
189-
subscription?.unsubscribe();
190-
set({ isFetching: false, subscription: null });
183+
set(({ subscription }) => {
184+
subscription?.unsubscribe();
185+
return { isFetching: false, subscription: null };
186+
});
191187
},
192188
async run() {
193189
const {
@@ -258,12 +254,9 @@ export const createExecutionSlice: CreateExecutionSlice =
258254
const fragmentDependencies = documentAST
259255
? getFragmentDependenciesForAST(documentAST, externalFragments)
260256
: [];
261-
if (fragmentDependencies.length > 0) {
257+
if (fragmentDependencies.length) {
262258
query +=
263-
'\n' +
264-
fragmentDependencies
265-
.map((node: FragmentDefinitionNode) => print(node))
266-
.join('\n');
259+
'\n' + fragmentDependencies.map(node => print(node)).join('\n');
267260
}
268261

269262
setResponse('');

packages/graphiql-react/src/stores/plugin.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,18 @@ export const createPluginSlice: CreatePluginSlice = initial => set => ({
9898
...initial,
9999
actions: {
100100
setVisiblePlugin(plugin = null) {
101-
set(({ visiblePlugin, plugins, onTogglePluginVisibility }) => {
101+
set(current => {
102+
const {
103+
visiblePlugin: currentVisiblePlugin,
104+
plugins,
105+
onTogglePluginVisibility,
106+
} = current;
102107
const byTitle = typeof plugin === 'string';
103108
const newVisiblePlugin: PluginSlice['visiblePlugin'] =
104109
(plugin && plugins.find(p => (byTitle ? p.title : p) === plugin)) ||
105110
null;
106-
if (newVisiblePlugin === visiblePlugin) {
107-
return { visiblePlugin };
111+
if (newVisiblePlugin === currentVisiblePlugin) {
112+
return current;
108113
}
109114
onTogglePluginVisibility?.(newVisiblePlugin);
110115
const { storage } = storageStore.getState();

packages/graphiql-react/src/utility/tabs.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,25 @@ export interface TabState extends TabDefinition {
2626
* A GUID value generated when the tab was created.
2727
*/
2828
id: string;
29+
2930
/**
3031
* A hash that is unique for a combination of the contents of the query
3132
* editor, the variable editor and the header editor (i.e. all the editor
3233
* where the contents are persisted in storage).
3334
*/
3435
hash: string;
36+
3537
/**
3638
* The title of the tab shown in the tab element.
3739
*/
3840
title: string;
41+
3942
/**
4043
* The operation name derived from the contents of the query editor of this
4144
* tab.
4245
*/
4346
operationName: string | null;
47+
4448
/**
4549
* The contents of the response editor of this tab.
4650
*/

packages/graphiql/cypress/e2e/docs.cy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ describeOrSkip('GraphQL DocExplorer - deprecated arguments', () => {
117117
);
118118
cy.get('.graphiql-markdown-deprecation').should(
119119
'have.text',
120-
'deprecated argument\n',
120+
'Argument "deprecatedArg" is deprecated. Use "string" instead.\n',
121121
);
122122
});
123123
});

0 commit comments

Comments
 (0)