Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit 3412b88

Browse files
Allow to override GraphiQL default query (#508)
Use a lot of ideas from #507 by @rybon
1 parent a17df73 commit 3412b88

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,13 @@ The `graphqlHTTP` function accepts the following options:
6464
A `schema` *must* be provided.
6565

6666
* **`graphiql`**: If `true`, presents [GraphiQL][] when the GraphQL endpoint is
67-
loaded in a browser. We recommend that you set
68-
`graphiql` to `true` when your app is in development, because it's
69-
quite useful. You may or may not want it in production.
67+
loaded in a browser. We recommend that you set `graphiql` to `true` when your
68+
app is in development, because it's quite useful. You may or may not want it
69+
in production.
70+
Alternatively, instead of `true` you can pass in an options object:
71+
* **`defaultQuery`**: An optional GraphQL string to use when no query
72+
is provided and no stored query exists from a previous session.
73+
If undefined is provided, GraphiQL will use its own default query.
7074

7175
* **`rootValue`**: A value to pass as the `rootValue` to the `graphql()`
7276
function from [`GraphQL.js/src/execute.js`](https://github.com/graphql/graphql-js/blob/master/src/execution/execute.js#L119).

src/__tests__/http-test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,29 @@ function urlString(urlParams?: ?{ [param: string]: mixed }) {
16381638
expect(response.text).to.include('graphiql.min.js');
16391639
});
16401640

1641+
it('contains a default query within GraphiQL', async () => {
1642+
const app = server();
1643+
1644+
get(
1645+
app,
1646+
urlString(),
1647+
graphqlHTTP({
1648+
schema: TestSchema,
1649+
graphiql: { defaultQuery: 'query testDefaultQuery { hello }' },
1650+
}),
1651+
);
1652+
1653+
const response = await request(app)
1654+
.get(urlString())
1655+
.set('Accept', 'text/html');
1656+
1657+
expect(response.status).to.equal(200);
1658+
expect(response.type).to.equal('text/html');
1659+
expect(response.text).to.include(
1660+
'defaultQuery: "query testDefaultQuery { hello }"',
1661+
);
1662+
});
1663+
16411664
it('contains a pre-run response within GraphiQL', async () => {
16421665
const app = server();
16431666

src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
} from 'graphql';
3535

3636
import { parseBody } from './parseBody';
37-
import { renderGraphiQL } from './renderGraphiQL';
37+
import { renderGraphiQL, type GraphiQLOptions } from './renderGraphiQL';
3838

3939
/**
4040
* Used to configure the graphqlHTTP middleware by providing a schema
@@ -128,8 +128,9 @@ export type OptionsData = {|
128128

129129
/**
130130
* A boolean to optionally enable GraphiQL mode.
131+
* Alternatively, instead of `true` you can pass in an options object.
131132
*/
132-
graphiql?: ?boolean,
133+
graphiql?: ?boolean | ?GraphiQLOptions,
133134

134135
/**
135136
* A resolver function to use when one is not provided by the schema.
@@ -199,7 +200,7 @@ function graphqlHTTP(options: Options): Middleware {
199200
let executeFn = execute;
200201
let parseFn = parse;
201202
let extensionsFn;
202-
let showGraphiQL;
203+
let showGraphiQL = false;
203204
let query;
204205

205206
let documentAST;
@@ -254,7 +255,7 @@ function graphqlHTTP(options: Options): Middleware {
254255
query = params.query;
255256
variables = params.variables;
256257
operationName = params.operationName;
257-
showGraphiQL = graphiql && canDisplayGraphiQL(request, params);
258+
showGraphiQL = canDisplayGraphiQL(request, params) && graphiql;
258259

259260
// If there is no query, but GraphiQL will be displayed, do not produce
260261
// a result, otherwise return a 400: Bad Request.
@@ -383,6 +384,7 @@ function graphqlHTTP(options: Options): Middleware {
383384
variables,
384385
operationName,
385386
result,
387+
options: typeof showGraphiQL !== 'boolean' ? showGraphiQL : {},
386388
});
387389
return sendResponse(response, 'text/html', payload);
388390
}

src/renderGraphiQL.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ type GraphiQLData = {|
1414
variables: ?{ [name: string]: mixed },
1515
operationName: ?string,
1616
result?: mixed,
17+
options: GraphiQLOptions,
18+
|};
19+
20+
export type GraphiQLOptions = {|
21+
/**
22+
* An optional GraphQL string to use when no query is provided and no stored
23+
* query exists from a previous session. If undefined is provided, GraphiQL
24+
* will use its own default query.
25+
*/
26+
defaultQuery?: ?string,
1727
|};
1828

1929
// Current latest version of GraphiQL.
@@ -40,6 +50,7 @@ export function renderGraphiQL(data: GraphiQLData): string {
4050
? JSON.stringify(data.result, null, 2)
4151
: null;
4252
const operationName = data.operationName;
53+
const defaultQuery = data.options.defaultQuery;
4354

4455
return `<!--
4556
The request to this GraphQL server provided the header "Accept: text/html"
@@ -158,6 +169,7 @@ add "&raw" to the end of the URL within a browser.
158169
response: ${safeSerialize(resultString)},
159170
variables: ${safeSerialize(variablesString)},
160171
operationName: ${safeSerialize(operationName)},
172+
defaultQuery: ${safeSerialize(defaultQuery)},
161173
}),
162174
document.getElementById('graphiql')
163175
);

0 commit comments

Comments
 (0)