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 default behavior for 401/403 http response codes #429

Merged
merged 4 commits into from
Aug 1, 2022
Merged

Conversation

sungam3r
Copy link
Member

This PR aligns behavior with server project that returns 401/403.

@sungam3r sungam3r requested a review from rose-a July 26, 2022 20:26
@sungam3r sungam3r self-assigned this Jul 26, 2022
@sungam3r sungam3r added the enhancement New feature or request label Jul 26, 2022
/// </summary>
public Func<HttpResponseMessage, bool> IsValidResponseToDeserialize { get; set; } = r => r.IsSuccessStatusCode || r.StatusCode == HttpStatusCode.BadRequest;
public Func<HttpResponseMessage, bool> IsValidResponseToDeserialize { get; set; } = r => r.IsSuccessStatusCode || r.StatusCode == HttpStatusCode.BadRequest || r.Content.Headers.ContentType.MediaType == "application/graphql+json";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tries to parse all responses in which the server manages to set the content type header to application/graphql+json (including 5xx responses). Are u sure this is a "bulletproof" assumption?

I think we should at least catch parsing exceptions in GraphQLHttpClient and wrap those up in a GraphQLHttpRequestException to pass the raw response content to the user (this probably won't hurt, anyway).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If content type is application/graphql+json then the response can be deserialized as a valid graphql response.

Regarding 5xx error codes from server - see https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#applicationgraphqljson :
изображение

@rose-a
Copy link
Collaborator

rose-a commented Jul 27, 2022

What's missing, too, is that this client doesn't set the Accept request header as defined in the spec draft, so the server will only respond with application/graphql+json if you explicitly specify the accept header in your application code.

We should set it to application/graphql+json; charset=utf-8, application/json; charset=utf-8 as recommended. I propose to do that in GraphQLHttpRequest.ToHttpRequestMessage.

@sungam3r
Copy link
Member Author

OK, will do tomorrow.

@@ -38,6 +39,9 @@ public virtual HttpRequestMessage ToHttpRequestMessage(GraphQLHttpClientOptions
{
Content = new StringContent(serializer.SerializeToString(this), Encoding.UTF8, options.MediaType)
};
message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/graphql+json"));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MediaTypeWithQualityHeaderValue does not accept application/graphql+json; charset=utf-8. It throws System.FormatException : The format of value 'application/graphql+json; charset=utf-8' is invalid. I added Accept-Charset header that should work the same.

@sungam3r sungam3r requested a review from rose-a July 31, 2022 06:41
Copy link
Collaborator

@rose-a rose-a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me..

@rose-a rose-a merged commit b4c6c82 into master Aug 1, 2022
@rose-a rose-a deleted the delegate branch August 1, 2022 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants