Skip to content

Response content types of application/graphql-response+json are not always handled #676

@Shane32

Description

@Shane32

Some GraphQL servers, such as those based on current versions of Hot Chocolate, only respond with application/graphql-response+json. In this case, the server may respond with various 4xx status codes, expecting the client to parse them. This is noted in the source code. However, any status code that is not 2xx or 400 is rejected.

/// <summary>
/// Delegate to determine if GraphQL response may be properly deserialized into <see cref="GraphQLResponse{T}"/>.
/// Note that compatible to the draft graphql-over-http spec GraphQL Server MAY return 4xx status codes (401/403, etc.)
/// with well-formed GraphQL response containing errors collection.
/// </summary>
public Func<HttpResponseMessage, bool> IsValidResponseToDeserialize { get; set; } = DefaultIsValidResponseToDeserialize;
private static readonly IReadOnlyCollection<string> _acceptedResponseContentTypes = new[] { "application/graphql+json", "application/json", "application/graphql-response+json" };
public static bool DefaultIsValidResponseToDeserialize(HttpResponseMessage r)
{
if (r.Content.Headers.ContentType?.MediaType != null && !_acceptedResponseContentTypes.Contains(r.Content.Headers.ContentType.MediaType))
return false;
return r.IsSuccessStatusCode || r.StatusCode == HttpStatusCode.BadRequest;
}

This shouldn't matter most of the time for servers that support application/json per the GraphQL over HTTP draft spec, since the default Accept header is set to application/json, and the server should return 200 for all responses that can be parsed with that content type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions