Skip to content

Commit

Permalink
Allow to override GraphiQL default query (graphql#508)
Browse files Browse the repository at this point in the history
Use a lot of ideas from graphql#507 by @rybon
  • Loading branch information
IvanGoncharov authored and jliu670 committed Aug 14, 2020
1 parent aaebd85 commit d1fd605
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ The `graphqlHTTP` function accepts the following options:
A `schema` *must* be provided.

* **`graphiql`**: If `true`, presents [GraphiQL][] when the GraphQL endpoint is
loaded in a browser. We recommend that you set
`graphiql` to `true` when your app is in development, because it's
quite useful. You may or may not want it in production.
loaded in a browser. We recommend that you set `graphiql` to `true` when your
app is in development, because it's quite useful. You may or may not want it
in production.
Alternatively, instead of `true` you can pass in an options object:
* **`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.

* **`rootValue`**: A value to pass as the `rootValue` to the `graphql()`
function from [`GraphQL.js/src/execute.js`](https://github.com/graphql/graphql-js/blob/master/src/execution/execute.js#L119).
Expand Down
23 changes: 23 additions & 0 deletions src/__tests__/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1638,6 +1638,29 @@ function urlString(urlParams?: ?{ [param: string]: mixed }) {
expect(response.text).to.include('graphiql.min.js');
});

it('contains a default query within GraphiQL', async () => {
const app = server();

get(
app,
urlString(),
graphqlHTTP({
schema: TestSchema,
graphiql: { defaultQuery: 'query testDefaultQuery { hello }' },
}),
);

const response = await request(app)
.get(urlString())
.set('Accept', 'text/html');

expect(response.status).to.equal(200);
expect(response.type).to.equal('text/html');
expect(response.text).to.include(
'defaultQuery: "query testDefaultQuery { hello }"',
);
});

it('contains a pre-run response within GraphiQL', async () => {
const app = server();

Expand Down
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
} from 'graphql';

import { parseBody } from './parseBody';
import { renderGraphiQL } from './renderGraphiQL';
import { renderGraphiQL, type GraphiQLOptions } from './renderGraphiQL';

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

/**
* A boolean to optionally enable GraphiQL mode.
* Alternatively, instead of `true` you can pass in an options object.
*/
graphiql?: ?boolean,
graphiql?: ?boolean | ?GraphiQLOptions,

/**
* A resolver function to use when one is not provided by the schema.
Expand Down Expand Up @@ -199,7 +200,7 @@ function graphqlHTTP(options: Options): Middleware {
let executeFn = execute;
let parseFn = parse;
let extensionsFn;
let showGraphiQL;
let showGraphiQL = false;
let query;

let documentAST;
Expand Down Expand Up @@ -254,7 +255,7 @@ function graphqlHTTP(options: Options): Middleware {
query = params.query;
variables = params.variables;
operationName = params.operationName;
showGraphiQL = graphiql && canDisplayGraphiQL(request, params);
showGraphiQL = canDisplayGraphiQL(request, params) && graphiql;

// If there is no query, but GraphiQL will be displayed, do not produce
// a result, otherwise return a 400: Bad Request.
Expand Down Expand Up @@ -383,6 +384,7 @@ function graphqlHTTP(options: Options): Middleware {
variables,
operationName,
result,
options: typeof showGraphiQL !== 'boolean' ? showGraphiQL : {},
});
return sendResponse(response, 'text/html', payload);
}
Expand Down
12 changes: 12 additions & 0 deletions src/renderGraphiQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ type GraphiQLData = {|
variables: ?{ [name: string]: mixed },
operationName: ?string,
result?: mixed,
options: GraphiQLOptions,
|};

export type GraphiQLOptions = {|
/**
* 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.
*/
defaultQuery?: ?string,
|};

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

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

0 comments on commit d1fd605

Please sign in to comment.