Skip to content

Commit

Permalink
feat: Change GraphQL acceptable media type application/graphql+json
Browse files Browse the repository at this point in the history
… to `application/graphql-response+json`

As per graphql/graphql-over-http#215
  • Loading branch information
enisdenjo committed Aug 16, 2022
1 parent 58a0411 commit d86318a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ val is Response

### AcceptableMediaType

Ƭ **AcceptableMediaType**: ``"application/graphql+json"`` \| ``"application/json"``
Ƭ **AcceptableMediaType**: ``"application/graphql-response+json"`` \| ``"application/json"``

Request's Media-Type that the server accepts.

Expand Down
54 changes: 27 additions & 27 deletions src/__tests__/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ beforeAll(() => {
});

describe('Media Types', () => {
it('must accept application/graphql+json and match the content-type', async () => {
it('must accept application/graphql-response+json and match the content-type', async () => {
const url = new URL(serverUrl);
url.searchParams.set('query', '{ __typename }');

const res = await fetch(url.toString(), {
headers: {
accept: 'application/graphql+json',
accept: 'application/graphql-response+json',
},
});
expect(res.status).toBe(200);
expect(res.headers.get('content-type')).toContain(
'application/graphql+json',
'application/graphql-response+json',
);
});

Expand All @@ -38,7 +38,7 @@ describe('Media Types', () => {
expect(res.headers.get('content-type')).toContain('application/json');
});

it('must accept */* and use application/graphql+json for the content-type', async () => {
it('must accept */* and use application/graphql-response+json for the content-type', async () => {
const url = new URL(serverUrl);
url.searchParams.set('query', '{ __typename }');

Expand All @@ -49,18 +49,18 @@ describe('Media Types', () => {
});
expect(res.status).toBe(200);
expect(res.headers.get('content-type')).toContain(
'application/graphql+json',
'application/graphql-response+json',
);
});

it('must assume application/graphql+json content-type when accept is missing', async () => {
it('must assume application/graphql-response+json content-type when accept is missing', async () => {
const url = new URL(serverUrl);
url.searchParams.set('query', '{ __typename }');

const res = await fetch(url.toString());
expect(res.status).toBe(200);
expect(res.headers.get('content-type')).toContain(
'application/graphql+json',
'application/graphql-response+json',
);
});

Expand All @@ -79,7 +79,7 @@ describe('Media Types', () => {

const res = await fetch(url.toString(), {
headers: {
accept: 'application/graphql+json; charset=iso-8859-1',
accept: 'application/graphql-response+json; charset=iso-8859-1',
},
});
expect(res.status).toBe(406);
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('Request', () => {

const res = await fetch(url.toString(), {
headers: {
accept: 'application/graphql+json',
accept: 'application/graphql-response+json',
},
});
expect(res.status).toBe(405);
Expand Down Expand Up @@ -152,7 +152,7 @@ describe('Request', () => {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/graphql+json',
accept: 'application/graphql-response+json',
},
body: JSON.stringify({ notquery: '{ __typename }' }),
});
Expand All @@ -165,7 +165,7 @@ describe('Request', () => {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/graphql+json',
accept: 'application/graphql-response+json',
},
body: JSON.stringify({
query: invalid,
Expand All @@ -179,7 +179,7 @@ describe('Request', () => {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/graphql+json',
accept: 'application/graphql-response+json',
},
body: JSON.stringify({
query: '{ __typename }',
Expand All @@ -195,7 +195,7 @@ describe('Request', () => {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/graphql+json',
accept: 'application/graphql-response+json',
},
body: JSON.stringify({
operationName: invalid,
Expand All @@ -210,7 +210,7 @@ describe('Request', () => {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/graphql+json',
accept: 'application/graphql-response+json',
},
body: JSON.stringify({
operationName: 'Query',
Expand All @@ -227,7 +227,7 @@ describe('Request', () => {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/graphql+json',
accept: 'application/graphql-response+json',
},
body: JSON.stringify({
query: '{ __typename }',
Expand All @@ -242,7 +242,7 @@ describe('Request', () => {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/graphql+json',
accept: 'application/graphql-response+json',
},
body: JSON.stringify({
query: 'query Type($name: String!) { __type(name: $name) { name } }',
Expand Down Expand Up @@ -275,7 +275,7 @@ describe('Request', () => {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/graphql+json',
accept: 'application/graphql-response+json',
},
body: JSON.stringify({
query: '{ __typename }',
Expand All @@ -290,7 +290,7 @@ describe('Request', () => {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/graphql+json',
accept: 'application/graphql-response+json',
},
body: JSON.stringify({
query: '{ __typename }',
Expand Down Expand Up @@ -350,13 +350,13 @@ describe('Response', () => {
});
});

describe('application/graphql+json', () => {
describe('application/graphql-response+json', () => {
it('must use 4xx or 5xx status codes on JSON parsing failure', async () => {
const res = await fetch(serverUrl, {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/graphql+json',
accept: 'application/graphql-response+json',
},
body: '{ "not a JSON',
});
Expand All @@ -368,7 +368,7 @@ describe('Response', () => {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'application/graphql+json',
accept: 'application/graphql-response+json',
},
body: '{ "not a JSON',
});
Expand All @@ -380,7 +380,7 @@ describe('Response', () => {
url.searchParams.set('qeury' /* typo */, '{ __typename }');
const res = await fetch(url.toString(), {
method: 'GET',
headers: { accept: 'application/graphql+json' },
headers: { accept: 'application/graphql-response+json' },
});
expect(res.status).toBeGreaterThanOrEqual(400);
expect(res.status).toBeLessThanOrEqual(599);
Expand All @@ -390,7 +390,7 @@ describe('Response', () => {
url.searchParams.set('qeury' /* typo */, '{ __typename }');
const res = await fetch(url.toString(), {
method: 'GET',
headers: { accept: 'application/graphql+json' },
headers: { accept: 'application/graphql-response+json' },
});
expect(res.status).toBe(400);
});
Expand All @@ -400,7 +400,7 @@ describe('Response', () => {
url.searchParams.set('query', '{');
const res = await fetch(url.toString(), {
method: 'GET',
headers: { accept: 'application/graphql+json' },
headers: { accept: 'application/graphql-response+json' },
});
expect(res.status).toBeGreaterThanOrEqual(400);
expect(res.status).toBeLessThanOrEqual(599);
Expand All @@ -410,7 +410,7 @@ describe('Response', () => {
url.searchParams.set('query', '{');
const res = await fetch(url.toString(), {
method: 'GET',
headers: { accept: 'application/graphql+json' },
headers: { accept: 'application/graphql-response+json' },
});
expect(res.status).toBe(400);
});
Expand All @@ -420,7 +420,7 @@ describe('Response', () => {
url.searchParams.set('query', '{ 8f31403dfe404bccbb0e835f2629c6a7 }'); // making sure the field doesnt exist
const res = await fetch(url.toString(), {
method: 'GET',
headers: { accept: 'application/graphql+json' },
headers: { accept: 'application/graphql-response+json' },
});
expect(res.status).toBeGreaterThanOrEqual(400);
expect(res.status).toBeLessThanOrEqual(599);
Expand All @@ -430,7 +430,7 @@ describe('Response', () => {
url.searchParams.set('query', '{ 8f31403dfe404bccbb0e835f2629c6a7 }'); // making sure the field doesnt exist
const res = await fetch(url.toString(), {
method: 'GET',
headers: { accept: 'application/graphql+json' },
headers: { accept: 'application/graphql-response+json' },
});
expect(res.status).toBe(400);
});
Expand Down
4 changes: 2 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ export function createClient(options: ClientOptions): Client {
headers: {
...headers,
'content-type': 'application/json; charset=utf-8',
accept: 'application/graphql+json, application/json',
accept: 'application/graphql-response+json, application/json',
},
credentials,
referrer,
Expand All @@ -254,7 +254,7 @@ export function createClient(options: ClientOptions): Client {
const contentType = res.headers.get('content-type');
if (!contentType) throw new Error('Missing response content-type');
if (
!contentType.includes('application/graphql+json') &&
!contentType.includes('application/graphql-response+json') &&
!contentType.includes('application/json')
) {
throw new Error(
Expand Down
12 changes: 6 additions & 6 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export function createHandler<RawRequest = unknown, Context = unknown>(
statusText: 'Not Acceptable',
headers: {
accept:
'application/graphql+json; charset=utf-8, application/json; charset=utf-8',
'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
},
},
];
Expand Down Expand Up @@ -570,7 +570,7 @@ export function createHandler<RawRequest = unknown, Context = unknown>(
* @category Server
*/
export type AcceptableMediaType =
| 'application/graphql+json'
| 'application/graphql-response+json'
| 'application/json';

/**
Expand Down Expand Up @@ -600,12 +600,12 @@ export function getAcceptableMediaType(
}

if (
(mediaType === 'application/graphql+json' ||
(mediaType === 'application/graphql-response+json' ||
mediaType === 'application/*' ||
mediaType === '*/*') &&
charset === 'charset=utf8'
) {
acceptedMediaType = 'application/graphql+json';
acceptedMediaType = 'application/graphql-response+json';
break;
}
}
Expand Down Expand Up @@ -639,7 +639,7 @@ export function makeResponse(
'content-type':
acceptedMediaType === 'application/json'
? 'application/json; charset=utf-8'
: 'application/graphql+json; charset=utf-8',
: 'application/graphql-response+json; charset=utf-8',
},
},
];
Expand Down Expand Up @@ -667,7 +667,7 @@ export function makeResponse(
'content-type':
acceptedMediaType === 'application/json'
? 'application/json; charset=utf-8'
: 'application/graphql+json; charset=utf-8',
: 'application/graphql-response+json; charset=utf-8',
},
},
];
Expand Down

0 comments on commit d86318a

Please sign in to comment.