Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/SAHB.GraphQLClient/Batching/Internal/GraphQLBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@ internal class GraphQLBatch : IGraphQLBatch
{
private readonly GraphQLBatchMerger _batch;

internal GraphQLBatch(GraphQLOperationType graphQLOperationType, string url, HttpMethod httpMethod, IDictionary<string, string> headers, string authorizationToken, string authorizationMethod, IGraphQLHttpExecutor executor, IGraphQLFieldBuilder fieldBuilder, IGraphQLQueryGeneratorFromFields queryGenerator, IGraphQLDeserialization deserialization)
internal GraphQLBatch(GraphQLOperationType graphQLOperationType, string operationName, string url,
HttpMethod httpMethod, IDictionary<string, string> headers, string authorizationToken,
string authorizationMethod, IGraphQLHttpExecutor executor, IGraphQLFieldBuilder fieldBuilder,
IGraphQLQueryGeneratorFromFields queryGenerator, IGraphQLDeserialization deserialization)
{
_batch = new GraphQLBatchMerger(graphQLOperationType, operationName, url, httpMethod, headers,
authorizationToken, authorizationMethod, executor, fieldBuilder, queryGenerator, deserialization);
}

internal GraphQLBatch(GraphQLOperationType graphQLOperationType, string url, HttpMethod httpMethod,
IDictionary<string, string> headers, string authorizationToken, string authorizationMethod,
IGraphQLHttpExecutor executor, IGraphQLFieldBuilder fieldBuilder,
IGraphQLQueryGeneratorFromFields queryGenerator, IGraphQLDeserialization deserialization) : this(
graphQLOperationType, null, url, httpMethod, headers, authorizationToken, authorizationMethod, executor,
fieldBuilder, queryGenerator, deserialization)
{
_batch = new GraphQLBatchMerger(graphQLOperationType, url, httpMethod, headers, authorizationToken, authorizationMethod, executor, fieldBuilder, queryGenerator, deserialization);
}

/// <inheritdoc />
Expand Down
34 changes: 24 additions & 10 deletions src/SAHB.GraphQLClient/Batching/Internal/GraphQLBatchMerger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace SAHB.GraphQLClient.Batching.Internal
internal class GraphQLBatchMerger
{
private readonly GraphQLOperationType _graphQLOperationType;
private readonly string _operationName;
private readonly string _url;
private readonly HttpMethod _httpMethod;
private readonly IDictionary<string, string> _headers;
Expand All @@ -34,9 +35,22 @@ internal class GraphQLBatchMerger
private GraphQLDataResult<JObject> _result;
private string _executedQuery;

public GraphQLBatchMerger(GraphQLOperationType graphQLOperationType, string url, HttpMethod httpMethod, IDictionary<string, string> headers, string authorizationToken, string authorizationMethod, IGraphQLHttpExecutor executor, IGraphQLFieldBuilder fieldBuilder, IGraphQLQueryGeneratorFromFields queryGenerator, IGraphQLDeserialization graphQLDeserialization)
public GraphQLBatchMerger(GraphQLOperationType graphQLOperationType, string url,
HttpMethod httpMethod, IDictionary<string, string> headers, string authorizationToken,
string authorizationMethod, IGraphQLHttpExecutor executor, IGraphQLFieldBuilder fieldBuilder,
IGraphQLQueryGeneratorFromFields queryGenerator, IGraphQLDeserialization graphQLDeserialization) : this(
graphQLOperationType, null, url, httpMethod, headers, authorizationToken, authorizationMethod, executor,
fieldBuilder, queryGenerator, graphQLDeserialization)
{
}

public GraphQLBatchMerger(GraphQLOperationType graphQLOperationType, string operationName, string url,
HttpMethod httpMethod, IDictionary<string, string> headers, string authorizationToken,
string authorizationMethod, IGraphQLHttpExecutor executor, IGraphQLFieldBuilder fieldBuilder,
IGraphQLQueryGeneratorFromFields queryGenerator, IGraphQLDeserialization graphQLDeserialization)
{
_graphQLOperationType = graphQLOperationType;
_operationName = operationName;
_url = url;
_httpMethod = httpMethod;
_headers = headers;
Expand Down Expand Up @@ -78,16 +92,13 @@ public Task<T> GetValue<T>(string identifier, CancellationToken cancellationToke
return GetDeserializedResult<T>(identifier, cancellationToken);
}

public async Task<GraphQLDataResult<T>> GetDetailedValue<T>(string identifier, CancellationToken cancellationToken)
public async Task<GraphQLDataResult<T>> GetDetailedValue<T>(string identifier,
CancellationToken cancellationToken)
where T : class
{
var deserialized = await GetDeserializedResult<T>(identifier, cancellationToken).ConfigureAwait(false);

return new GraphQLDataResult<T>
{
Data = deserialized,
Headers = _result.Headers
};
return new GraphQLDataResult<T> {Data = deserialized, Headers = _result.Headers};
}

public async Task Execute(CancellationToken cancellationToken)
Expand All @@ -107,11 +118,13 @@ public async Task Execute(CancellationToken cancellationToken)
var fields = _fields.SelectMany(e => e.Value).ToList();

// Generate query
_executedQuery = _queryGenerator.GenerateQuery(_graphQLOperationType, fields,
_executedQuery = _queryGenerator.GenerateQuery(_graphQLOperationType, _operationName, fields,
_arguments.SelectMany(e => e.Value).ToArray());

// Execute query
var serverResult = await _executor.ExecuteQuery(query: _executedQuery, url: _url, method: _httpMethod, authorizationToken: _authorizationToken, authorizationMethod: _authorizationMethod, headers: _headers, cancellationToken: cancellationToken).ConfigureAwait(false);
var serverResult = await _executor.ExecuteQuery(query: _executedQuery, url: _url, method: _httpMethod,
authorizationToken: _authorizationToken, authorizationMethod: _authorizationMethod, headers: _headers,
cancellationToken: cancellationToken).ConfigureAwait(false);

// Deserilize result
_result = _graphQLDeserialization.DeserializeResult<JObject>(serverResult.Response, fields);
Expand Down Expand Up @@ -178,7 +191,8 @@ private async Task<T> GetDeserializedResult<T>(string identifier, CancellationTo
}

// Deserialize from
return _graphQLDeserialization.DeserializeResult<T>(jsonObject: deserilizeFrom, fields: _fields[identifier]);
return _graphQLDeserialization.DeserializeResult<T>(jsonObject: deserilizeFrom,
fields: _fields[identifier]);
}

public bool Executed => _isExecuted;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Reflection;
using SAHB.GraphQLClient.FieldBuilder;
using SAHB.GraphQLClient.FieldBuilder.Attributes;
using SAHB.GraphQLClient.QueryGenerator;
using SAHB.GraphQLClient.QueryGenerator.Attributes;

namespace SAHB.GraphQLClient.Extentions
{
Expand All @@ -24,8 +26,12 @@ public static string GetQuery<T>(this IGraphQLQueryGeneratorFromFields queryGene
{
if (queryGenerator == null) throw new ArgumentNullException(nameof(queryGenerator));
if (fieldBuilder == null) throw new ArgumentNullException(nameof(fieldBuilder));
var selectionSet = fieldBuilder.GenerateSelectionSet(typeof(T));
return queryGenerator.GenerateQuery(GraphQLOperationType.Query, selectionSet, arguments);

var type = typeof(T);
var selectionSet = fieldBuilder.GenerateSelectionSet(type);
var operationNameAttribute = type.GetTypeInfo().GetCustomAttribute<GraphQLOperationNameAttribute>();

return queryGenerator.GenerateQuery(GraphQLOperationType.Query, operationNameAttribute?.OperationName, selectionSet, arguments);
}

/// <summary>
Expand All @@ -41,8 +47,12 @@ public static string GetMutation<T>(this IGraphQLQueryGeneratorFromFields queryG
{
if (queryGenerator == null) throw new ArgumentNullException(nameof(queryGenerator));
if (fieldBuilder == null) throw new ArgumentNullException(nameof(fieldBuilder));
var selectionSet = fieldBuilder.GenerateSelectionSet(typeof(T));
return queryGenerator.GenerateQuery(GraphQLOperationType.Mutation, selectionSet, arguments);

var type = typeof(T);
var selectionSet = fieldBuilder.GenerateSelectionSet(type);
var operationNameAttribute = type.GetTypeInfo().GetCustomAttribute<GraphQLOperationNameAttribute>();

return queryGenerator.GenerateQuery(GraphQLOperationType.Mutation, operationNameAttribute?.OperationName, selectionSet, arguments);
}
}
}
Loading