Skip to content

Commit

Permalink
Update GraphiQL and expose 'headerEditorEnabled' property
Browse files Browse the repository at this point in the history
  • Loading branch information
chentsulin committed Nov 24, 2021
1 parent 4d5511d commit 1465ec8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ The `graphqlHTTP` function accepts the following options:
- **`defaultQuery`**: An optional GraphQL string to use when no query
is provided and no stored query exists from a previous session.
If undefined is provided, GraphiQL will use its own default query.

- **`headerEditorEnabled`**: An optional boolean which enables the header editor when true.
Defaults to false.

- **`editorTheme`**: By passing an object you may change the theme of GraphiQL.
Details are below in the [Custom GraphiQL themes](#custom-graphiql-themes) section.

Expand Down
2 changes: 1 addition & 1 deletion examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ app.use(
graphqlHTTP({
schema,
rootValue: root,
graphiql: true,
graphiql: { headerEditorEnabled: true },
}),
),
);
Expand Down
10 changes: 9 additions & 1 deletion src/renderGraphiQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export type GraphiQLOptions = {|
*/
defaultQuery?: ?string,

/**
* An optional boolean which enables the header editor when true.
* Defaults to false.
*/
headerEditorEnabled?: boolean,

/**
* By passing an object you may change the theme of GraphiQL.
*/
Expand All @@ -42,7 +48,7 @@ type EditorTheme =
const CODE_MIRROR_VERSION = '5.53.2';

// Ensures string values are safe to be used within a <script> tag.
function safeSerialize(data: ?string): string {
function safeSerialize(data: string | boolean | null | void): string {
return data != null
? JSON.stringify(data).replace(/\//g, '\\/')
: 'undefined';
Expand Down Expand Up @@ -94,6 +100,7 @@ export function renderGraphiQL(data: GraphiQLData): string {
data.result != null ? JSON.stringify(data.result, null, 2) : null;
const operationName = data.operationName;
const defaultQuery = data.options?.defaultQuery;
const headerEditorEnabled = data.options?.headerEditorEnabled;
const editorTheme = getEditorThemeParams(data.options.editorTheme);

return `<!--
Expand Down Expand Up @@ -229,6 +236,7 @@ add "&raw" to the end of the URL within a browser.
variables: ${safeSerialize(variablesString)},
operationName: ${safeSerialize(operationName)},
defaultQuery: ${safeSerialize(defaultQuery)},
headerEditorEnabled: ${safeSerialize(headerEditorEnabled)},
}),
document.getElementById('graphiql')
);
Expand Down

0 comments on commit 1465ec8

Please sign in to comment.