File tree Expand file tree Collapse file tree 3 files changed +16
-3
lines changed
tests/GraphQL.Integration.Tests/QueryAndMutationTests Expand file tree Collapse file tree 3 files changed +16
-3
lines changed Original file line number Diff line number Diff line change @@ -52,8 +52,12 @@ public class GraphQLHttpClientOptions
5252
5353 /// <summary>
5454 /// Delegate to determine if GraphQL response may be properly deserialized into <see cref="GraphQLResponse{T}"/>.
55+ /// Note that compatible to the draft graphql-over-http spec GraphQL Server MAY return 4xx status codes (401/403, etc.)
56+ /// with well-formed GraphQL response containing errors collection.
5557 /// </summary>
56- public Func < HttpResponseMessage , bool > IsValidResponseToDeserialize { get ; set ; } = r => r . IsSuccessStatusCode || r . StatusCode == HttpStatusCode . BadRequest ;
58+ public Func < HttpResponseMessage , bool > IsValidResponseToDeserialize { get ; set ; } = r =>
59+ // Why not application/json? See https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#processing-the-response
60+ r . IsSuccessStatusCode || r . StatusCode == HttpStatusCode . BadRequest || r . Content . Headers . ContentType . MediaType == "application/graphql+json" ;
5761
5862 /// <summary>
5963 /// This callback is called after successfully establishing a websocket connection but before any regular request is made.
Original file line number Diff line number Diff line change 1+ using System . Net . Http . Headers ;
12using System . Runtime . Serialization ;
23using System . Text ;
34using GraphQL . Client . Abstractions ;
@@ -38,6 +39,9 @@ public virtual HttpRequestMessage ToHttpRequestMessage(GraphQLHttpClientOptions
3839 {
3940 Content = new StringContent ( serializer . SerializeToString ( this ) , Encoding . UTF8 , options . MediaType )
4041 } ;
42+ message . Headers . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/graphql+json" ) ) ;
43+ message . Headers . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
44+ message . Headers . AcceptCharset . Add ( new StringWithQualityHeaderValue ( "utf-8" ) ) ;
4145
4246#pragma warning disable CS0618 // Type or member is obsolete
4347 PreprocessHttpRequestMessage ( message ) ;
Original file line number Diff line number Diff line change 11using System . Net ;
2+ using System . Net . Http . Headers ;
23using FluentAssertions ;
34using GraphQL . Client . Abstractions ;
45using GraphQL . Client . Http ;
@@ -173,9 +174,13 @@ public async void PreprocessHttpRequestMessageIsCalled()
173174#pragma warning restore CS0618 // Type or member is obsolete
174175 } ;
175176
176- var defaultHeaders = StarWarsClient . HttpClient . DefaultRequestHeaders ;
177+ var expectedHeaders = new HttpRequestMessage ( ) . Headers ;
178+ expectedHeaders . UserAgent . Add ( new ProductInfoHeaderValue ( "GraphQL.Client" , typeof ( GraphQLHttpClient ) . Assembly . GetName ( ) . Version . ToString ( ) ) ) ;
179+ expectedHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/graphql+json" ) ) ;
180+ expectedHeaders . Accept . Add ( new MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
181+ expectedHeaders . AcceptCharset . Add ( new StringWithQualityHeaderValue ( "utf-8" ) ) ;
177182 var response = await StarWarsClient . SendQueryAsync ( graphQLRequest , ( ) => new { Human = new { Name = string . Empty } } ) ;
178- callbackTester . Should ( ) . HaveBeenInvokedWithPayload ( ) . Which . Headers . Should ( ) . BeEquivalentTo ( defaultHeaders ) ;
183+ callbackTester . Should ( ) . HaveBeenInvokedWithPayload ( ) . Which . Headers . Should ( ) . BeEquivalentTo ( expectedHeaders ) ;
179184 Assert . Null ( response . Errors ) ;
180185 Assert . Equal ( "Luke" , response . Data . Human . Name ) ;
181186 }
You can’t perform that action at this time.
0 commit comments