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

Update the header editor to work in the 2.0 version of GraphiQL #1590

Closed
Closed
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
2 changes: 1 addition & 1 deletion packages/graphiql/resources/renderExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function graphQLFetcher(graphQLParams, opts = { headers: {} }) {
let headers = opts.headers;
// Convert headers to an object.
if (typeof headers === 'string') {
headers = JSON.parse(opts.headers);
headers = JSON.parse(headers);
}

return fetch(api, {
Expand Down
10 changes: 10 additions & 0 deletions packages/graphiql/src/api/actions/sessionActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export enum SessionActionTypes {
EditorLoaded = 'EditorLoaded',
OperationChanged = 'OperationChanged',
VariablesChanged = 'VariablesChanged',
HeadersChanged = 'HeadersChanged',
OperationSucceeded = 'OperationSucceeded',
OperationErrored = 'OperationErrored',
TabChanged = 'TabChanged',
Expand All @@ -19,6 +20,7 @@ export type SessionAction =
| EditorLoadedAction
| OperationChangedAction
| VariablesChangedAction
| HeadersChangedAction
| OperationSucceededAction
| OperationErroredAction
| TabChangedAction;
Expand Down Expand Up @@ -62,6 +64,14 @@ export const variableChangedAction = (value: string, sessionId: number) =>

export type VariablesChangedAction = ReturnType<typeof variableChangedAction>;

export const headerChangedAction = (value: string, sessionId: number) =>
({
type: SessionActionTypes.HeadersChanged,
payload: { value, sessionId },
} as const);

export type HeadersChangedAction = ReturnType<typeof headerChangedAction>;

export const operationSucceededAction = (result: string, sessionId: number) =>
({
type: SessionActionTypes.OperationSucceeded,
Expand Down
30 changes: 28 additions & 2 deletions packages/graphiql/src/api/providers/GraphiQLSessionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
operationRequestAction,
operationSucceededAction,
variableChangedAction,
headerChangedAction,
operationChangedAction,
operationErroredAction,
tabChangedAction,
Expand All @@ -24,6 +25,7 @@ export type SessionReducer = React.Reducer<SessionState, SessionAction>;
export interface SessionHandlers {
changeOperation: (operation: string) => void;
changeVariables: (variables: string) => void;
changeHeaders: (headers: string) => void;
changeTab: (pane: string, tabId: number) => void;
executeOperation: (operationName?: string) => Promise<void>;
operationError: (error: Error) => void;
Expand All @@ -34,6 +36,7 @@ export const initialState: SessionState = {
sessionId: 0,
operation: { uri: 'graphql://graphiql/operations/0.graphql' },
variables: { uri: 'graphql://graphiql/variables/0.graphql' },
headers: { uri: 'graphql://graphiql/variables/1.graphql' },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is what we want here? The problem would be that the session state would be trying to pull from whatever the second tab is regardless of whether the headers editor is enabled.

results: { uri: 'graphql://graphiql/results/0.graphql' },
currentTabs: {
operation: 0,
Expand All @@ -51,6 +54,7 @@ export const initialContextState: SessionState & SessionHandlers = {
executeOperation: async () => {},
changeOperation: () => null,
changeVariables: () => null,
changeHeaders: () => null,
changeTab: () => null,
operationError: () => null,
dispatch: () => null,
Expand Down Expand Up @@ -90,6 +94,16 @@ const sessionReducer: SessionReducer = (state, action) => {
},
};
}
case SessionActionTypes.HeadersChanged: {
const { value } = action.payload;
return {
...state,
headers: {
...state.headers,
text: value,
},
};
}
case SessionActionTypes.OperationSucceeded: {
const { result } = action.payload;
return {
Expand Down Expand Up @@ -164,6 +178,12 @@ export function SessionProvider({
[sessionId],
);

const changeHeaders = React.useCallback(
(headersText: string) =>
dispatch(headerChangedAction(headersText, sessionId)),
[sessionId],
);

const changeTab = React.useCallback(
(pane: string, tabId: number) => dispatch(tabChangedAction(pane, tabId)),
[],
Expand All @@ -173,9 +193,14 @@ export function SessionProvider({
async (operationName?: string) => {
try {
dispatch(operationRequestAction());
const { operation: op, variables: vars } = editorsState.editors;
const {
operation: op,
variables: vars,
headers: hdrs,
} = editorsState.editors;
const operation = op.editor.getValue();
const variables = vars.editor.getValue();
const headers = hdrs.editor.getValue();

const fetchValues: GraphQLParams = {
query: operation ?? '',
Expand All @@ -187,7 +212,7 @@ export function SessionProvider({
fetchValues.operationName = operationName as string;
}
const result = await observableToPromise<FetcherResult>(
fetcher(fetchValues),
fetcher(fetchValues, { headers }),
);
dispatch(operationSucceededAction(result, sessionId));
} catch (err) {
Expand Down Expand Up @@ -229,6 +254,7 @@ export function SessionProvider({
executeOperation,
changeOperation,
changeVariables,
changeHeaders,
changeTab,
operationError,
dispatch,
Expand Down
1 change: 1 addition & 0 deletions packages/graphiql/src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type SessionState = {
sessionId: number;
operation: File;
variables: File;
headers: File;
results: File;
operationLoading: boolean;
operationErrors: Error[] | null;
Expand Down
29 changes: 27 additions & 2 deletions packages/graphiql/src/components/GraphiQL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ExecuteButton } from './ExecuteButton';
import { ToolbarButton } from './ToolbarButton';
import { QueryEditor } from './QueryEditor';
import { VariableEditor } from './VariableEditor';
import { HeaderEditor } from './HeaderEditor';
import { ResultViewer } from './ResultViewer';
import { DocExplorer } from './DocExplorer';
import { QueryHistory } from './QueryHistory';
Expand Down Expand Up @@ -92,6 +93,7 @@ export type GraphiQLProps = {
formatResult?: (result: any) => string;
formatError?: (rawError: Error) => string;
variablesEditorOptions?: monaco.editor.IStandaloneEditorConstructionOptions;
headersEditorOptions?: monaco.editor.IStandaloneEditorConstructionOptions;
operationEditorOptions?: monaco.editor.IStandaloneEditorConstructionOptions;
resultsEditorOptions?: monaco.editor.IStandaloneEditorConstructionOptions;
} & Partial<Formatters>;
Expand Down Expand Up @@ -174,6 +176,7 @@ class GraphiQLInternals extends React.Component<
graphiqlContainer: Maybe<HTMLDivElement>;
resultComponent: Maybe<typeof ResultViewer>;
variableEditorComponent: Maybe<typeof VariableEditor>;
headerEditorComponent: Maybe<typeof HeaderEditor>;
_queryHistory: Maybe<typeof QueryHistory>;
editorBarComponent: Maybe<HTMLDivElement>;
queryEditorComponent: Maybe<typeof QueryEditor>;
Expand Down Expand Up @@ -278,9 +281,13 @@ class GraphiQLInternals extends React.Component<
</section>
);

// Include the 'Headers' if the headers editor is enabled.
const variablesTabs = this.props.headerEditorEnabled
? [`Variables`, `Headers`, `Console`]
: [`Variables`, `Console`];
const variables = (
<section aria-label="Query Variables">
<SessionTabs tabs={[`Variables`, `Console`]} name={`variables`}>
<SessionTabs tabs={variablesTabs} name={`variables`}>
<VariableEditor
onHintInformationRender={this.handleHintInformationRender}
onPrettifyQuery={this.handlePrettifyQuery}
Expand All @@ -289,6 +296,16 @@ class GraphiQLInternals extends React.Component<
readOnly={this.props.readOnly}
editorOptions={this.props.variablesEditorOptions}
/>
{this.props.headerEditorEnabled && (
<HeaderEditor
onHintInformationRender={this.handleHintInformationRender}
onPrettifyQuery={this.handlePrettifyQuery}
onMergeQuery={this.handleMergeQuery}
editorTheme={this.props.editorTheme}
readOnly={this.props.readOnly}
editorOptions={this.props.headersEditorOptions}
/>
)}
<div>{`Console`}</div>
</SessionTabs>
</section>
Expand Down Expand Up @@ -388,13 +405,21 @@ class GraphiQLInternals extends React.Component<
{session => {
return (
<QueryHistory
onSelectQuery={(operation, vars, _opName) => {
onSelectQuery={(
operation,
vars,
headers,
_opName,
) => {
if (operation) {
session.changeOperation(operation);
}
if (vars) {
session.changeVariables(vars);
}
if (headers) {
session.changeHeaders(headers);
}
}}
storage={this._storage}
queryID={this._editorQueryID}>
Expand Down
Loading