-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraphQLClient.cs
More file actions
149 lines (129 loc) · 5.7 KB
/
GraphQLClient.cs
File metadata and controls
149 lines (129 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
namespace GraphQLSharp;
public class GraphQLClient<TRequest, TOptions, TQueryRoot, TMutationRoot> : GraphQLClient<TRequest, TOptions, TQueryRoot>
where TRequest : GraphQLRequest
where TOptions : class, IGraphQLClientOptions<TOptions, TRequest>
where TQueryRoot : class
where TMutationRoot : class
{
public GraphQLClient(TOptions options) : base(options)
{
}
public Task<GraphQLResponse<TMutationRoot>> MutationAsync(TRequest request, CancellationToken cancellationToken = default)
{
return ExecuteAsync<TMutationRoot>(request, cancellationToken);
}
}
public class GraphQLClient<TRequest, TOptions, TQueryRoot> : GraphQLClient<TRequest, TOptions>
where TRequest : GraphQLRequest
where TOptions : class, IGraphQLClientOptions<TOptions, TRequest>
where TQueryRoot : class
{
public GraphQLClient(TOptions options) : base(options)
{
}
public Task<GraphQLResponse<TQueryRoot>> QueryAsync(TRequest request, CancellationToken cancellationToken = default)
{
return ExecuteAsync<TQueryRoot>(request, cancellationToken);
}
}
public class GraphQLCLient : GraphQLClient<GraphQLRequest, GraphQLClientOptions>
{
public GraphQLCLient(GraphQLClientOptions options) : base(options)
{
}
}
public class GraphQLClient<TRequest, TOptions>
where TRequest : GraphQLRequest
where TOptions : class, IGraphQLClientOptions<TOptions, TRequest>
{
private static readonly HttpClient _defaultHttpClient = new(new SocketsHttpHandler
{
AutomaticDecompression = DecompressionMethods.All
})
{
DefaultRequestVersion = HttpVersion.Version20,
DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower,
};
private static readonly ProductInfoHeaderValue _defaultUserAgent = new(typeof(GraphQLClient<TRequest, TOptions>).Assembly.GetName().Name!, typeof(GraphQLClient<TRequest, TOptions>).Assembly.GetName().Version!.ToString());
protected readonly TOptions _options;
public GraphQLClient(TOptions options)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
}
public Task<GraphQLResponse<JsonElement?>> ExecuteAsync(TRequest request, CancellationToken cancellationToken = default)
{
//returing JsonElement? and not JsonDocument because JsonDocument is disposable and we don't want to force the user to dispose it
//JsonElement is a struct so we return a nullable value
return ExecuteAsync<JsonElement?>(request, cancellationToken);
}
public async Task<GraphQLResponse<T>> ExecuteAsync<T>(TRequest request, CancellationToken cancellationToken = default)
{
var interceptor = _options.Interceptor ?? NoOpInterceptor<TRequest, TOptions>.Instance;
try
{
return await interceptor.InterceptRequestAsync(request, _options, cancellationToken, ExecuteCoreAsync<T>);
}
catch (Exception ex) when (ex is not GraphQLException)
{
throw new GraphQLInterceptorException(request, interceptor, ex);
}
}
private async Task<GraphQLResponse<T>> ExecuteCoreAsync<T>(TRequest request, CancellationToken cancellationToken = default)
{
HttpResponse httpResponse = null;
try
{
using HttpRequestMessage requestMessage = CreateHttpRequest(request);
var httpClient = _options.HttpClient ?? _defaultHttpClient;
using var httpResponseMsg = await httpClient.SendAsync(requestMessage, cancellationToken);
//httpResponseMsg needs to disposed so we create a small copy of basic information
httpResponse = new HttpResponse(httpResponseMsg);
try
{
httpResponseMsg.EnsureSuccessStatusCode();
}
catch (Exception httpEx)
{
throw new GraphQLHttpException(request, httpResponse, httpEx);
}
GraphQLResponse<T> res;
try
{
res = await httpResponseMsg.Content.ReadFromJsonAsync<GraphQLResponse<T>>(_options.JsonSerializerOptions ?? Serializer.GetOptions(), cancellationToken);
if (res == null)
throw new GraphQLException(request, httpResponse, $"Failed to deserialize null GraphQL response. Request: {request}");
}
catch (Exception jsonEx)
{
throw new GraphQLException(request, httpResponse, $"Failed to deserialize GraphQL response. Request: {request}", jsonEx);
}
res.Request = request;
res.HttpResponse = httpResponse;
if (_options.ThrowOnGraphQLErrors ?? true)
res.ThrowIfAnyError();
return res;
}
catch (Exception ex) when (ex is not GraphQLException)
{
throw new GraphQLException(request, httpResponse, $"Unexpected GraphQL error: {request}", ex);
}
}
private HttpRequestMessage CreateHttpRequest(TRequest request)
{
_ = request.query ?? throw new ArgumentNullException(nameof(request.query));
var uri = _options.Uri ?? throw new ArgumentNullException($"{nameof(IGraphQLClientOptions<TOptions, TRequest>.Uri)} must a non-null URI.");
var requestMessage = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = uri,
Content = JsonContent.Create(request, options: _options.JsonSerializerOptions ?? Serializer.GetOptions()),
};
requestMessage.Headers.UserAgent.Add(_defaultUserAgent);
_options.ConfigureHttpRequestHeaders?.Invoke(requestMessage.Headers);
return requestMessage;
}
}