Skip to content

Commit de62d67

Browse files
cimdalliacaodimaMachina
committed
fix: use the default query on adding a new tab (#3441)
* Use default query on adding new tab * Fix eslint issue * First try to use `query` and `headers` props * add changeset * upd fix lint this should fix cypress fix * Update .changeset/brown-tigers-nail.md --------- Co-authored-by: Rikki Schulte <[email protected]> Co-authored-by: Dimitri POSTOLOV <[email protected]>
1 parent fd0e25f commit de62d67

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

.changeset/brown-tigers-nail.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
"@graphiql/react": patch
3+
"graphiql": patch
4+
---
5+
6+
fix: set query editor to `defaultQuery` while adding a new tab or GraphiQL's default query
7+
8+
```graphql
9+
# Welcome to GraphiQL
10+
#
11+
# GraphiQL is an in-browser tool for writing, validating, and
12+
# testing GraphQL queries.
13+
14+
...
15+
```
16+

packages/graphiql-react/src/editor/context.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
360360
headerEditor,
361361
responseEditor,
362362
});
363-
const { onTabChange, defaultHeaders, children } = props;
363+
const { onTabChange, defaultHeaders, defaultQuery, children } = props;
364364
const setEditorValues = useSetEditorValues({
365365
queryEditor,
366366
variableEditor,
@@ -374,7 +374,13 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
374374
// Make sure the current tab stores the latest values
375375
const updatedValues = synchronizeActiveTabValues(current);
376376
const updated = {
377-
tabs: [...updatedValues.tabs, createTab({ headers: defaultHeaders })],
377+
tabs: [
378+
...updatedValues.tabs,
379+
createTab({
380+
headers: defaultHeaders,
381+
query: defaultQuery ?? DEFAULT_QUERY,
382+
}),
383+
],
378384
activeTabIndex: updatedValues.tabs.length,
379385
};
380386
storeTabs(updated);
@@ -384,6 +390,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
384390
});
385391
}, [
386392
defaultHeaders,
393+
defaultQuery,
387394
onTabChange,
388395
setEditorValues,
389396
storeTabs,

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ const DEFAULT_HEADERS = '{"foo":2}';
33
describe('Headers', () => {
44
describe('`defaultHeaders`', () => {
55
it('should have default headers while open new tabs', () => {
6-
cy.visit(`/?query={test}&defaultHeaders=${DEFAULT_HEADERS}`);
6+
cy.visit(
7+
`/?query={test}&defaultHeaders=${DEFAULT_HEADERS}&defaultQuery=`,
8+
);
79
cy.assertHasValues({ query: '{test}', headersString: DEFAULT_HEADERS });
810
cy.get('.graphiql-tab-add').click();
911
cy.assertHasValues({ query: '', headersString: DEFAULT_HEADERS });
@@ -14,7 +16,7 @@ describe('Headers', () => {
1416
it('in case `headers` and `defaultHeaders` are set, `headers` should be on 1st tab and `defaultHeaders` for other opened tabs', () => {
1517
const HEADERS = '{"bar":true}';
1618
cy.visit(
17-
`/?query={test}&defaultHeaders=${DEFAULT_HEADERS}&headers=${HEADERS}`,
19+
`/?query={test}&defaultHeaders=${DEFAULT_HEADERS}&headers=${HEADERS}&defaultQuery=`,
1820
);
1921
cy.assertHasValues({ query: '{test}', headersString: HEADERS });
2022
cy.get('.graphiql-tab-add').click();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
describe('Tabs', () => {
22
it('Should store editor contents when switching between tabs', () => {
3-
cy.visit('/?query=');
3+
cy.visit('/?defaultQuery=&query=');
44

55
// Assert that tab visible when there's only one session
66
cy.get('.graphiql-tab-button').eq(0).should('exist');

packages/graphiql/resources/renderExample.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,6 @@ root.render(
9595
parameters.confirmCloseTab === 'true' ? confirmCloseTab : undefined,
9696
onTabChange,
9797
forcedTheme: parameters.forcedTheme,
98+
defaultQuery: parameters.defaultQuery,
9899
}),
99100
);

0 commit comments

Comments
 (0)