Exemplo n.º 1
0
        public async Task <TModel> SendQueryAsync <TModel>(Request <TModel> request, Priority priority, int retryCount, Func <Exception, bool> shouldRetry, int timeout, CancellationToken cancellationToken = default)
            where TModel : class, new()
        {
            var service = _graphQLService.GetByPriority(priority);

            service.EndPoint = new Uri(_graphQLService.BaseUrl + GetGraphQLEndpoint());

            var policy = GetWrappedPolicy(retryCount, shouldRetry, timeout);
            var result = await policy.ExecuteAsync(async() => await service.SendQueryAsync(request, cancellationToken));

            if (result.Errors != null && result.Errors.Any())
            {
                string message = "GraphQL" + (result.Errors.Length == 1 ? " Error: " : " Errors: ");
                message += string.Join(" /// ", result.Errors.Select(e => e.Message));
                throw new Exception(message);
            }

            var resultData = result?.Data as JObject;

            if (resultData == null)
            {
                throw new InvalidCastException("Result is not a valid Json");
            }
            var model = JsonConvert.DeserializeObject <TModel>(resultData.ToString());

            return(model);
        }