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

Commit b3ccce9

Browse files
committed
500 when response.data is null.
This matches the described spec behavior that when response.data is null, that indicates a query error. This happens when a top field is Non-Null and produces an error. In that case there is no data for the query and a 500 is an appropriate response status. Suggested in #118
1 parent 11719a3 commit b3ccce9

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

src/__tests__/http-test.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,14 @@ const QueryRootType = new GraphQLObjectType({
4545
},
4646
resolve: (root, { who }) => 'Hello ' + (who || 'World')
4747
},
48-
thrower: {
48+
nonNullThrower: {
4949
type: new GraphQLNonNull(GraphQLString),
5050
resolve: () => { throw new Error('Throws!'); }
5151
},
52+
thrower: {
53+
type: GraphQLString,
54+
resolve: () => { throw new Error('Throws!'); }
55+
},
5256
context: {
5357
type: GraphQLString,
5458
resolve: (obj, args, context) => context,
@@ -974,6 +978,30 @@ describe('test harness', () => {
974978

975979
expect(response.status).to.equal(200);
976980
expect(JSON.parse(response.text)).to.deep.equal({
981+
data: { thrower: null },
982+
errors: [ {
983+
message: 'Throws!',
984+
locations: [ { line: 1, column: 2 } ]
985+
} ]
986+
});
987+
});
988+
989+
it('handles query errors from non-null top field errors', async () => {
990+
const app = server();
991+
992+
app.use(urlString(), graphqlHTTP({
993+
schema: TestSchema
994+
}));
995+
996+
const error = await catchError(
997+
request(app)
998+
.get(urlString({
999+
query: '{nonNullThrower}',
1000+
}))
1001+
);
1002+
1003+
expect(error.response.status).to.equal(500);
1004+
expect(JSON.parse(error.response.text)).to.deep.equal({
9771005
data: null,
9781006
errors: [ {
9791007
message: 'Throws!',
@@ -999,7 +1027,7 @@ describe('test harness', () => {
9991027

10001028
expect(response.status).to.equal(200);
10011029
expect(JSON.parse(response.text)).to.deep.equal({
1002-
data: null,
1030+
data: { thrower: null },
10031031
errors: [ {
10041032
message: 'Custom error format: Throws!',
10051033
} ]
@@ -1027,7 +1055,7 @@ describe('test harness', () => {
10271055

10281056
expect(response.status).to.equal(200);
10291057
expect(JSON.parse(response.text)).to.deep.equal({
1030-
data: null,
1058+
data: { thrower: null },
10311059
errors: [ {
10321060
message: 'Throws!',
10331061
locations: [ { line: 1, column: 2 } ],

src/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,23 +234,31 @@ export default function graphqlHTTP(options: Options): Middleware {
234234
response.statusCode = error.status || 500;
235235
return { errors: [ error ] };
236236
}).then(result => {
237+
// If no data was included in the result, that indicates a runtime query
238+
// error, indicate as such with a generic status code.
239+
// Note: Information about the error itself will still be contained in
240+
// the resulting JSON payload.
241+
// http://facebook.github.io/graphql/#sec-Data
242+
if (result && result.data === null) {
243+
response.statusCode = 500;
244+
}
237245
// Format any encountered errors.
238246
if (result && result.errors) {
239247
result.errors = result.errors.map(formatErrorFn || formatError);
240248
}
241249
// If allowed to show GraphiQL, present it instead of JSON.
242250
if (showGraphiQL) {
243-
const data = renderGraphiQL({
251+
const payload = renderGraphiQL({
244252
query, variables,
245253
operationName, result
246254
});
247255
response.setHeader('Content-Type', 'text/html; charset=utf-8');
248-
sendResponse(response, data);
256+
sendResponse(response, payload);
249257
} else {
250258
// Otherwise, present JSON directly.
251-
const data = JSON.stringify(result, null, pretty ? 2 : 0);
259+
const payload = JSON.stringify(result, null, pretty ? 2 : 0);
252260
response.setHeader('Content-Type', 'application/json; charset=utf-8');
253-
sendResponse(response, data);
261+
sendResponse(response, payload);
254262
}
255263
});
256264
};

0 commit comments

Comments
 (0)