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

Commit 162d0c6

Browse files
author
Matt Myers
committed
New formatResponse option allowing more customized responses.
1 parent 6fc7baf commit 162d0c6

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,16 @@ The `graphqlHTTP` function accepts the following options:
7575
function from [`GraphQL.js/src/execute.js`](https://github.com/graphql/graphql-js/blob/master/src/execution/execute.js#L122). If `context` is not provided, the
7676
`request` object is passed as the context.
7777

78-
* **`pretty`**: If `true`, any JSON response will be pretty-printed.
78+
* **`pretty`**: If `true`, any JSON response will be pretty-printed. When set to `true`, this will override `formatResponse` option.
7979

8080
* **`formatError`**: An optional function which will be used to format any
8181
errors produced by fulfilling a GraphQL operation. If no function is
82-
provided, GraphQL's default spec-compliant [`formatError`][] function will be used.
82+
provided, GraphQL's default spec-compliant [`formatError`][] function will be used.π
83+
84+
* **`formatResponse`**: An optional string value to customize the response format. Valid options are:
85+
`result`, `pretty`, or `json`. The default value is `json`. When specifying `result`, the result data will
86+
be returned instead of output. Otherwise both `json` and `pretty` will output the appropriate
87+
response as plain or pretty json respectively.
8388

8489
* **`extensions`**: An optional function for adding additional metadata to the
8590
GraphQL response as a key-value object. The result will be added to

src/index.js

+28-11
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ export type OptionsData = {
6363
*/
6464
pretty?: ?boolean,
6565

66+
/**
67+
* A string to configure the format of the output.
68+
* Options: result, pretty, or json
69+
* Defaults to: json
70+
*/
71+
formatResponse?: ?string,
72+
6673
/**
6774
* An optional function which will be used to format any errors produced by
6875
* fulfilling a GraphQL operation. If no function is provided, GraphQL's
@@ -119,7 +126,7 @@ export type RequestInfo = {
119126
result: ?mixed,
120127
};
121128

122-
type Middleware = (request: $Request, response: $Response) => Promise<void>;
129+
type Middleware = (request: $Request, response: $Response) => Promise<mixed>;
123130

124131
/**
125132
* Middleware for express; takes an options object or function as input to
@@ -135,11 +142,11 @@ function graphqlHTTP(options: Options): Middleware {
135142
// Higher scoped variables are referred to at various stages in the
136143
// asynchronous state machine below.
137144
let params;
138-
let pretty;
139145
let formatErrorFn;
140146
let extensionsFn;
141147
let showGraphiQL;
142148
let query;
149+
let formatResponse;
143150

144151
let documentAST;
145152
let variables;
@@ -176,9 +183,15 @@ function graphqlHTTP(options: Options): Middleware {
176183
const context = optionsData.context || request;
177184
const rootValue = optionsData.rootValue;
178185
const graphiql = optionsData.graphiql;
179-
pretty = optionsData.pretty;
186+
180187
formatErrorFn = optionsData.formatError;
181188
extensionsFn = optionsData.extensions;
189+
formatResponse = optionsData.formatResponse || 'json';
190+
191+
// Backwards compatability
192+
if (optionsData.pretty === true) {
193+
formatResponse = 'pretty';
194+
}
182195

183196
let validationRules = specifiedRules;
184197
if (optionsData.validationRules) {
@@ -321,15 +334,19 @@ function graphqlHTTP(options: Options): Middleware {
321334
throw httpError(500, 'Internal Error');
322335
}
323336

324-
// If "pretty" JSON isn't requested, and the server provides a
325-
// response.json method (express), use that directly.
326-
// Otherwise use the simplified sendResponse method.
327-
if (!pretty && typeof response.json === 'function') {
328-
response.json(result);
329-
} else {
330-
const payload = JSON.stringify(result, null, pretty ? 2 : 0);
331-
sendResponse(response, 'application/json', payload);
337+
// Return the correct formatted response
338+
if (formatResponse === 'result') {
339+
return Promise.resolve(result);
340+
}
341+
if (formatResponse === 'pretty') {
342+
return sendResponse(response, 'application/json', JSON.stringify(result, null, 2));
332343
}
344+
if (typeof response.json === 'function') {
345+
return response.json(result);
346+
}
347+
348+
// Default json response
349+
return sendResponse(response, 'application/json', JSON.stringify(result, null, 0));
333350
});
334351
};
335352
}

0 commit comments

Comments
 (0)