Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use the default query on adding a new tab #3441

Merged
merged 9 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/brown-tigers-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
"@graphiql/react": patch
"graphiql": patch
---

fix: set query editor to `defaultQuery` while adding a new tab or GraphiQL's default query

```graphql
# Welcome to GraphiQL
#
# GraphiQL is an in-browser tool for writing, validating, and
# testing GraphQL queries.

...
```

11 changes: 9 additions & 2 deletions packages/graphiql-react/src/editor/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
headerEditor,
responseEditor,
});
const { onTabChange, defaultHeaders, children } = props;
const { onTabChange, defaultHeaders, defaultQuery, children } = props;
const setEditorValues = useSetEditorValues({
queryEditor,
variableEditor,
Expand All @@ -374,7 +374,13 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
// Make sure the current tab stores the latest values
const updatedValues = synchronizeActiveTabValues(current);
const updated = {
tabs: [...updatedValues.tabs, createTab({ headers: defaultHeaders })],
tabs: [
...updatedValues.tabs,
createTab({
headers: defaultHeaders,
query: defaultQuery ?? DEFAULT_QUERY,
}),
],
activeTabIndex: updatedValues.tabs.length,
};
storeTabs(updated);
Expand All @@ -384,6 +390,7 @@ export function EditorContextProvider(props: EditorContextProviderProps) {
});
}, [
defaultHeaders,
defaultQuery,
onTabChange,
setEditorValues,
storeTabs,
Expand Down
6 changes: 4 additions & 2 deletions packages/graphiql/cypress/e2e/headers.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const DEFAULT_HEADERS = '{"foo":2}';
describe('Headers', () => {
describe('`defaultHeaders`', () => {
it('should have default headers while open new tabs', () => {
cy.visit(`/?query={test}&defaultHeaders=${DEFAULT_HEADERS}`);
cy.visit(
`/?query={test}&defaultHeaders=${DEFAULT_HEADERS}&defaultQuery=`,
);
cy.assertHasValues({ query: '{test}', headersString: DEFAULT_HEADERS });
cy.get('.graphiql-tab-add').click();
cy.assertHasValues({ query: '', headersString: DEFAULT_HEADERS });
Expand All @@ -14,7 +16,7 @@ describe('Headers', () => {
it('in case `headers` and `defaultHeaders` are set, `headers` should be on 1st tab and `defaultHeaders` for other opened tabs', () => {
const HEADERS = '{"bar":true}';
cy.visit(
`/?query={test}&defaultHeaders=${DEFAULT_HEADERS}&headers=${HEADERS}`,
`/?query={test}&defaultHeaders=${DEFAULT_HEADERS}&headers=${HEADERS}&defaultQuery=`,
);
cy.assertHasValues({ query: '{test}', headersString: HEADERS });
cy.get('.graphiql-tab-add').click();
Expand Down
2 changes: 1 addition & 1 deletion packages/graphiql/cypress/e2e/tabs.cy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('Tabs', () => {
it('Should store editor contents when switching between tabs', () => {
cy.visit('/?query=');
cy.visit('/?defaultQuery=&query=');

// Assert that no tab visible when there's only one session
cy.get('#graphiql-session-tab-0').should('not.exist');
Expand Down
1 change: 1 addition & 0 deletions packages/graphiql/resources/renderExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,6 @@ root.render(
parameters.confirmCloseTab === 'true' ? confirmCloseTab : undefined,
onTabChange,
forcedTheme: parameters.forcedTheme,
defaultQuery: parameters.defaultQuery,
}),
);