apollo-link-rest: Error when REST response is empty
I have a mutation request which when successful responds with a 204 “No content” status. When coupled with apollo-link-error I am constantly getting a network error: Unexpected end of JSON input.
It appears that apollo-link-rest is trying to parse the empty body of the response.
Any ideas how to mitigate this? BTW I am trying to talk to a SharePoint REST api, so there is not much I can to tweak the server’s response.
There should be a way to tell the apollo-link-rest how to deal with an empty response…
My mutation call:
<Mutation mutation={M_WRITE_PROJECT_DETAILS}>
{(writeProjectDetails, { data }) => (
<Form.Button
content="Save"
onClick={() => {
const token = localStorage.getItem("token");
writeProjectDetails({
variables: {
projectId: project.Id,
input: {
__metadata: {
type: "SP.Data.ProjectListItem"
},
Title: project.Title
}
},
context: {
headers: {
"X-RequestDigest": token,
"X-HTTP-Method": "MERGE",
"IF-MATCH": "*"
}
}
});
}}
/>
)}
</Mutation>
The corresponding gql query:
const M_WRITE_PROJECT_DETAILS = gql`
mutation writeToSPList($projectId: String, $input: String) {
writeToSPList(projectId: $projectId, input: $input)
@rest(
type: "Project"
path: "/web/lists/GetByTitle('Projects')/items(:projectId)"
method: "POST"
) {
NoResponse
}
}
`;
“NoResponse” is obviously null since there is no response, but then again I cannot send a mutation without any response fields… unless I am missing something.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 18 (7 by maintainers)
I think in order for the upstream GraphQL interpretation of the empty body to work we should return
{}which will then result inWhen you say “Could both behaviors be supported?” do you mean looking at the 204 status code and looking at the headers? I guess when the
Content-Lengthis actually present and also0then we could take that as another indication of an empty body and return the default.Although I proposed the
Content-Typeheader earlier I’m actually not exactly clear on what should be done when it is set to something other thanjson. I think if this header is to be interpreted it will probably need to be done with aresponseSerializerconfig option similar to the proposedbodySerializer. That is probably overkill though, and wouldn’t address the empty body issue in particular.I’m happy to do the 204 and Content-Length implementation.
Yes. The fix that was merged for this checks for a 204 status or a
Content-Length: 0header and returns an empty object.If you have a 200 empty response without a
Content-Lengthheader it will not work since the body is not parsed for this check.@isopterix If the server sets the
Content-Lengthto zero properly it should be working even on 200 responses, the intent is to check for that as well.