|
1 |
| -#pragma warning disable IDE0005 |
2 |
| -// see https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/8.0/implicit-global-using-netfx |
3 |
| -using System.Net.Http; |
4 |
| -#pragma warning restore IDE0005 |
5 |
| -using System.Net.Http.Headers; |
6 |
| -using System.Text; |
7 |
| -using GraphQL.Client.Abstractions; |
8 |
| - |
9 |
| -namespace GraphQL.Client.Http; |
10 |
| - |
11 |
| -public class GraphQLHttpRequest : GraphQLRequest |
12 |
| -{ |
13 |
| - public GraphQLHttpRequest() |
14 |
| - { |
15 |
| - } |
16 |
| - |
17 |
| - public GraphQLHttpRequest(string query, object? variables = null, string? operationName = null, Dictionary<string, object?>? extensions = null) |
18 |
| - : base(query, variables, operationName, extensions) |
19 |
| - { |
20 |
| - } |
21 |
| - |
22 |
| - public GraphQLHttpRequest(GraphQLRequest other) |
23 |
| - : base(other) |
24 |
| - { |
25 |
| - } |
26 |
| - |
27 |
| - /// <summary> |
28 |
| - /// Creates a <see cref="HttpRequestMessage"/> from this <see cref="GraphQLHttpRequest"/>. |
29 |
| - /// Used by <see cref="GraphQLHttpClient"/> to convert GraphQL requests when sending them as regular HTTP requests. |
30 |
| - /// </summary> |
31 |
| - /// <param name="options">the <see cref="GraphQLHttpClientOptions"/> passed from <see cref="GraphQLHttpClient"/></param> |
32 |
| - /// <param name="serializer">the <see cref="IGraphQLJsonSerializer"/> passed from <see cref="GraphQLHttpClient"/></param> |
33 |
| - /// <returns></returns> |
34 |
| - public virtual HttpRequestMessage ToHttpRequestMessage(GraphQLHttpClientOptions options, IGraphQLJsonSerializer serializer) |
35 |
| - { |
36 |
| - var message = new HttpRequestMessage(HttpMethod.Post, options.EndPoint) |
37 |
| - { |
38 |
| - Content = new StringContent(serializer.SerializeToString(this), Encoding.UTF8, options.MediaType) |
39 |
| - }; |
40 |
| - message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/graphql-response+json")); |
41 |
| - message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
| 1 | +#pragma warning disable IDE0005 |
| 2 | +// see https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/8.0/implicit-global-using-netfx |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Net.Http; |
| 5 | +#pragma warning restore IDE0005 |
| 6 | +using System.Net.Http.Headers; |
| 7 | +using System.Text; |
| 8 | +using GraphQL.Client.Abstractions; |
| 9 | + |
| 10 | +namespace GraphQL.Client.Http; |
| 11 | + |
| 12 | +public class GraphQLHttpRequest : GraphQLRequest |
| 13 | +{ |
| 14 | + public GraphQLHttpRequest() |
| 15 | + { |
| 16 | + } |
| 17 | + |
| 18 | + public GraphQLHttpRequest([StringSyntax("GraphQL")] string query, object? variables = null, string? operationName = null, Dictionary<string, object?>? extensions = null) |
| 19 | + : base(query, variables, operationName, extensions) |
| 20 | + { |
| 21 | + } |
| 22 | + |
| 23 | + public GraphQLHttpRequest(GraphQLRequest other) |
| 24 | + : base(other) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Creates a <see cref="HttpRequestMessage"/> from this <see cref="GraphQLHttpRequest"/>. |
| 30 | + /// Used by <see cref="GraphQLHttpClient"/> to convert GraphQL requests when sending them as regular HTTP requests. |
| 31 | + /// </summary> |
| 32 | + /// <param name="options">the <see cref="GraphQLHttpClientOptions"/> passed from <see cref="GraphQLHttpClient"/></param> |
| 33 | + /// <param name="serializer">the <see cref="IGraphQLJsonSerializer"/> passed from <see cref="GraphQLHttpClient"/></param> |
| 34 | + /// <returns></returns> |
| 35 | + public virtual HttpRequestMessage ToHttpRequestMessage(GraphQLHttpClientOptions options, IGraphQLJsonSerializer serializer) |
| 36 | + { |
| 37 | + var message = new HttpRequestMessage(HttpMethod.Post, options.EndPoint) |
| 38 | + { |
| 39 | + Content = new StringContent(serializer.SerializeToString(this), Encoding.UTF8, options.MediaType) |
| 40 | + }; |
| 41 | + message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/graphql-response+json")); |
| 42 | + message.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); |
42 | 43 | message.Headers.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8"));
|
43 | 44 |
|
44 | 45 | // Explicitly setting content header to avoid issues with some GrahQL servers
|
45 |
| - message.Content.Headers.ContentType = new MediaTypeHeaderValue(options.MediaType); |
46 |
| - |
47 |
| - if (options.DefaultUserAgentRequestHeader != null) |
48 |
| - message.Headers.UserAgent.Add(options.DefaultUserAgentRequestHeader); |
49 |
| - |
50 |
| - return message; |
51 |
| - } |
52 |
| -} |
| 46 | + message.Content.Headers.ContentType = new MediaTypeHeaderValue(options.MediaType); |
| 47 | + |
| 48 | + if (options.DefaultUserAgentRequestHeader != null) |
| 49 | + message.Headers.UserAgent.Add(options.DefaultUserAgentRequestHeader); |
| 50 | + |
| 51 | + return message; |
| 52 | + } |
| 53 | +} |
0 commit comments