Skip to content

Commit

Permalink
Use the application/graphql+json content-type.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydenseric committed Jun 6, 2020
1 parent a6b7728 commit 013480b
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 66 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Major

- Added a [package `exports` field](https://nodejs.org/api/esm.html#esm_package_entry_points) with [conditional exports](https://nodejs.org/api/esm.html#esm_conditional_exports) to support native ESM in Node.js and keep internal code private, [whilst avoiding the dual package hazard](https://nodejs.org/api/esm.html#esm_approach_1_use_an_es_module_wrapper). Published files have been reorganized, so previously undocumented deep imports will need to be rewritten according to the newly documented paths.
- Use the `application/graphql+json` content-type instead of `application/json` for GraphQL requests and responses. This content-type [is being standardized](https://github.com/APIs-guru/graphql-over-http/issues/31) in the [GraphQL over HTTP specification](https://github.com/APIs-guru/graphql-over-http).

### Patch

Expand Down
3 changes: 3 additions & 0 deletions public/errorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ module.exports = function errorHandler() {
ctx.response.status = httpError.status;
}

// Set the content-type.
ctx.response.type = 'application/graphql+json';

// Support Koa app error listeners.
ctx.app.emit('error', error, ctx);
}
Expand Down
9 changes: 8 additions & 1 deletion public/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ const ALLOWED_EXECUTE_OPTIONS_OVERRIDE = ALLOWED_EXECUTE_OPTIONS_STATIC.filter(
*
* const app = new Koa()
* .use(errorHandler())
* .use(bodyParser())
* .use(bodyParser({
* extendTypes: {
* json: 'application/graphql+json',
* },
* }))
* .use(execute({ schema }));
* ```
*/
Expand Down Expand Up @@ -237,6 +241,9 @@ module.exports = function execute(options) {
throw error;
}

// Set the content-type.
ctx.response.type = 'application/graphql+json';

await next();
};
};
8 changes: 7 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ _A basic GraphQL API._
>
> const app = new Koa()
> .use(errorHandler())
> .use(bodyParser())
> .use(
> bodyParser({
> extendTypes: {
> json: 'application/graphql+json',
> },
> })
> )
> .use(execute({ schema }));
> ```
Expand Down
10 changes: 5 additions & 5 deletions test/fetchJsonAtPort.js → test/fetchGraphQL.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
const fetch = require('node-fetch');

/**
* Fetches JSON at a given localhost port.
* Fetches GraphQL.
* @kind function
* @name fetchJsonAtPort
* @name fetchGraphQL
* @param {number} port Localhost port.
* @param {object} options Fetch options.
* @returns {Promise<Response>} Fetch response.
* @ignore
*/
module.exports = function fetchJsonAtPort(port, options = {}) {
module.exports = function fetchGraphQL(port, options = {}) {
return fetch(`http://localhost:${port}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
'Content-Type': 'application/graphql+json',
Accept: 'application/graphql+json',
},
...options,
});
Expand Down
26 changes: 21 additions & 5 deletions test/public/errorHandler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { deepStrictEqual, ok, strictEqual } = require('assert');
const createHttpError = require('http-errors');
const Koa = require('koa');
const errorHandler = require('../../public/errorHandler');
const fetchJsonAtPort = require('../fetchJsonAtPort');
const fetchGraphQL = require('../fetchGraphQL');
const listen = require('../listen');

module.exports = (tests) => {
Expand All @@ -25,9 +25,13 @@ module.exports = (tests) => {
const { port, close } = await listen(app);

try {
const response = await fetchJsonAtPort(port);
const response = await fetchGraphQL(port);

strictEqual(koaError, null);
strictEqual(
response.headers.get('Content-Type'),
'application/graphql+json'
);
deepStrictEqual(await response.json(), {
errors: [{ message: 'Internal Server Error' }],
});
Expand All @@ -54,14 +58,18 @@ module.exports = (tests) => {
const { port, close } = await listen(app);

try {
const response = await fetchJsonAtPort(port);
const response = await fetchGraphQL(port);

ok(koaError instanceof Error);
strictEqual(koaError.name, 'Error');
strictEqual(koaError.message, 'Message.');
strictEqual(koaError.status, 500);
strictEqual(koaError.expose, false);
strictEqual(response.status, 500);
strictEqual(
response.headers.get('Content-Type'),
'application/graphql+json'
);
deepStrictEqual(await response.json(), {
errors: [
{
Expand Down Expand Up @@ -90,14 +98,18 @@ module.exports = (tests) => {
const { port, close } = await listen(app);

try {
const response = await fetchJsonAtPort(port);
const response = await fetchGraphQL(port);

ok(koaError instanceof Error);
strictEqual(koaError.name, 'ForbiddenError');
strictEqual(koaError.message, 'Message.');
strictEqual(koaError.status, 403);
strictEqual(koaError.expose, true);
strictEqual(response.status, 403);
strictEqual(
response.headers.get('Content-Type'),
'application/graphql+json'
);
deepStrictEqual(await response.json(), {
errors: [
{
Expand Down Expand Up @@ -132,14 +144,18 @@ module.exports = (tests) => {
const { port, close } = await listen(app);

try {
const response = await fetchJsonAtPort(port);
const response = await fetchGraphQL(port);

ok(koaError instanceof Error);
strictEqual(koaError.name, 'Error');
strictEqual(koaError.message, 'Message.');
strictEqual(koaError.status, 500);
strictEqual(koaError.expose, false);
strictEqual(response.status, 500);
strictEqual(
response.headers.get('Content-Type'),
'application/graphql+json'
);
deepStrictEqual(await response.json(), {
data: {},
errors: [
Expand Down
Loading

0 comments on commit 013480b

Please sign in to comment.