Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of empty JSON object response and no field selection #248

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/__tests__/restLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2578,6 +2578,61 @@ describe('Mutation', () => {
});
});

it.only('returns null on empty object response without typePatcher', async () => {
expect.assertions(1);

const link = new RestLink({ uri: '/api' });

const post = { id: '1', title: 'Love apollo' };
fetchMock.post('/api/posts', {
status: 204,
body: {},
});

const createPostMutation = gql`
mutation publishPost($input: PublishablePostInput!) {
publishedPost(input: $input) @rest(path: "/posts", method: "POST")
}
`;
const response = await toPromise<Result>(
execute(link, {
operationName: 'publishPost',
query: createPostMutation,
variables: { input: { title: post.title } },
}),
);

expect(response.data.publishedPost).toEqual(null);
});

it.only('returns null on empty object response with typePatcher', async () => {
expect.assertions(1);

const link = new RestLink({
uri: '/api',
typePatcher: { Post: data => data },
});

const post = { id: '1', title: 'Love apollo' };

fetchMock.post('/api/posts', { status: 204, body: {} });

const createPostMutation = gql`
mutation publishPost($input: PublishablePostInput!) {
publishedPost(input: $input) @rest(path: "/posts", method: "POST")
}
`;
const response = await toPromise<Result>(
execute(link, {
operationName: 'publishPost',
query: createPostMutation,
variables: { input: { title: post.title } },
}),
);

expect(response.data.publishedPost).toEqual(null);
});

it('returns an empty object on successful posts with zero Content-Length', async () => {
// In Node.js parsing an empty body doesn't throw an error, so the best test is
// to provide body data and ensure the zero length still triggers the empty response
Expand Down
5 changes: 5 additions & 0 deletions src/restLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ function insertNullsForAnyOmittedFields(
currentSelectionSet: SelectionSetNode,
): void {
if (
currentSelectionSet == null ||
null == current ||
typeof current === 'number' ||
typeof current === 'boolean' ||
Expand Down Expand Up @@ -1183,6 +1184,8 @@ export class RestLink extends ApolloLink {

if (typePatcher == null) {
this.typePatcher = (result, __typename, _2) => {
if (!Object.keys(result).length) return null;

return { __typename, ...result };
};
} else if (
Expand All @@ -1202,6 +1205,8 @@ export class RestLink extends ApolloLink {
outerType: string,
patchDeeper: RestLink.FunctionalTypePatcher,
) => {
if (!Object.keys(data).length) return null;

const __typename = data.__typename || outerType;
if (Array.isArray(data)) {
return data.map(d => patchDeeper(d, __typename, patchDeeper));
Expand Down