Skip to content

Commit

Permalink
add GraphQLQuery record type for reusable query declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
rose-a committed Apr 22, 2024
1 parent fd2cf06 commit d569b43
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 20 deletions.
21 changes: 19 additions & 2 deletions src/GraphQL.Client.Abstractions/GraphQLClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,39 @@ public static class GraphQLClientExtensions
{
public static Task<GraphQLResponse<TResponse>> SendQueryAsync<TResponse>(this IGraphQLClient client,
[StringSyntax("GraphQL")] string query, object? variables = null,
string? operationName = null, Func<TResponse> defineResponseType = null, CancellationToken cancellationToken = default)
string? operationName = null, Func<TResponse>? defineResponseType = null, CancellationToken cancellationToken = default)
{
_ = defineResponseType;
return client.SendQueryAsync<TResponse>(new GraphQLRequest(query, variables, operationName),
cancellationToken: cancellationToken);
}

#if NET7_0_OR_GREATER
public static Task<GraphQLResponse<TResponse>> SendQueryAsync<TResponse>(this IGraphQLClient client,
GraphQLQuery query, object? variables = null,
string? operationName = null, Func<TResponse>? defineResponseType = null,
CancellationToken cancellationToken = default)
=> SendQueryAsync(client, query.Text, variables, operationName, defineResponseType,
cancellationToken);
#endif

public static Task<GraphQLResponse<TResponse>> SendMutationAsync<TResponse>(this IGraphQLClient client,
[StringSyntax("GraphQL")] string query, object? variables = null,
string? operationName = null, Func<TResponse> defineResponseType = null, CancellationToken cancellationToken = default)
string? operationName = null, Func<TResponse>? defineResponseType = null, CancellationToken cancellationToken = default)
{
_ = defineResponseType;
return client.SendMutationAsync<TResponse>(new GraphQLRequest(query, variables, operationName),
cancellationToken: cancellationToken);
}

#if NET7_0_OR_GREATER
public static Task<GraphQLResponse<TResponse>> SendMutationAsync<TResponse>(this IGraphQLClient client,
GraphQLQuery query, object? variables = null, string? operationName = null, Func<TResponse>? defineResponseType = null,
CancellationToken cancellationToken = default)
=> SendMutationAsync(client, query.Text, variables, operationName, defineResponseType,
cancellationToken);
#endif

public static Task<GraphQLResponse<TResponse>> SendQueryAsync<TResponse>(this IGraphQLClient client,
GraphQLRequest request, Func<TResponse> defineResponseType, CancellationToken cancellationToken = default)
{
Expand Down
2 changes: 1 addition & 1 deletion src/GraphQL.Client/GraphQL.Client.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net461;net7.0;net8.0</TargetFrameworks>
<RootNamespace>GraphQL.Client.Http</RootNamespace>
</PropertyGroup>

Expand Down
7 changes: 7 additions & 0 deletions src/GraphQL.Client/GraphQLHttpRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ public GraphQLHttpRequest([StringSyntax("GraphQL")] string query, object? variab
{
}

#if NET7_0_OR_GREATER
public GraphQLHttpRequest(GraphQLQuery query, object? variables = null, string? operationName = null, Dictionary<string, object?>? extensions = null)
: base(query, variables, operationName, extensions)
{
}
#endif

public GraphQLHttpRequest(GraphQLRequest other)
: base(other)
{
Expand Down
11 changes: 4 additions & 7 deletions src/GraphQL.Client/GraphQLSubscriptionException.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
#if !NET8_0_OR_GREATER
using System.Runtime.Serialization;
#endif

namespace GraphQL.Client.Http;

[Serializable]
public class GraphQLSubscriptionException : Exception
{
//
// For guidelines regarding the creation of new exception types, see
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp
// and
// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp
//

public GraphQLSubscriptionException()
{
}
Expand All @@ -20,9 +15,11 @@ public GraphQLSubscriptionException(object error) : base(error.ToString())
{
}

#if !NET8_0_OR_GREATER
protected GraphQLSubscriptionException(
SerializationInfo info,
StreamingContext context) : base(info, context)
{
}
#endif
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if !NET8_0_OR_GREATER
using System.Runtime.Serialization;
#endif

namespace GraphQL.Client.Http.Websocket;

Expand All @@ -9,15 +11,18 @@ public GraphQLWebsocketConnectionException()
{
}

protected GraphQLWebsocketConnectionException(SerializationInfo info, StreamingContext context) : base(info, context)
public GraphQLWebsocketConnectionException(string message) : base(message)
{
}

public GraphQLWebsocketConnectionException(string message) : base(message)
public GraphQLWebsocketConnectionException(string message, Exception innerException) : base(message, innerException)
{
}

public GraphQLWebsocketConnectionException(string message, Exception innerException) : base(message, innerException)
#if !NET8_0_OR_GREATER
protected GraphQLWebsocketConnectionException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
#endif

}
11 changes: 11 additions & 0 deletions src/GraphQL.Primitives/GraphQLQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#if NET7_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;

namespace GraphQL;

public readonly record struct GraphQLQuery([StringSyntax("GraphQL")] string Text)
{
public static implicit operator string(GraphQLQuery query)
=> query.Text;
};
#endif
8 changes: 8 additions & 0 deletions src/GraphQL.Primitives/GraphQLRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public GraphQLRequest([StringSyntax("GraphQL")] string query, object? variables
Extensions = extensions;
}

#if NET7_0_OR_GREATER
public GraphQLRequest(GraphQLQuery query, object? variables = null, string? operationName = null,
Dictionary<string, object?>? extensions = null)
: this(query.Text, variables, operationName, extensions)
{
}
#endif

public GraphQLRequest(GraphQLRequest other) : base(other) { }

/// <summary>
Expand Down
16 changes: 9 additions & 7 deletions tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,15 @@ public async void QueryWithDynamicReturnTypeTheory(int id, string name)
[ClassData(typeof(StarWarsHumans))]
public async void QueryWitVarsTheory(int id, string name)
{
var graphQLRequest = new GraphQLRequest(@"
query Human($id: String!){
human(id: $id) {
name
}
}",
new { id = id.ToString() });
var query = new GraphQLQuery("""
query Human($id: String!){
human(id: $id) {
name
}
}
""");

var graphQLRequest = new GraphQLRequest(query, new { id = id.ToString() });

var response = await StarWarsClient.SendQueryAsync(graphQLRequest, () => new { Human = new { Name = string.Empty } });

Expand Down

0 comments on commit d569b43

Please sign in to comment.