diff --git a/src/GraphQL.Client.Abstractions/GraphQLClientExtensions.cs b/src/GraphQL.Client.Abstractions/GraphQLClientExtensions.cs index cce6db1a..1efb0475 100644 --- a/src/GraphQL.Client.Abstractions/GraphQLClientExtensions.cs +++ b/src/GraphQL.Client.Abstractions/GraphQLClientExtensions.cs @@ -6,22 +6,39 @@ public static class GraphQLClientExtensions { public static Task> SendQueryAsync(this IGraphQLClient client, [StringSyntax("GraphQL")] string query, object? variables = null, - string? operationName = null, Func defineResponseType = null, CancellationToken cancellationToken = default) + string? operationName = null, Func? defineResponseType = null, CancellationToken cancellationToken = default) { _ = defineResponseType; return client.SendQueryAsync(new GraphQLRequest(query, variables, operationName), cancellationToken: cancellationToken); } +#if NET7_0_OR_GREATER + public static Task> SendQueryAsync(this IGraphQLClient client, + GraphQLQuery query, object? variables = null, + string? operationName = null, Func? defineResponseType = null, + CancellationToken cancellationToken = default) + => SendQueryAsync(client, query.Text, variables, operationName, defineResponseType, + cancellationToken); +#endif + public static Task> SendMutationAsync(this IGraphQLClient client, [StringSyntax("GraphQL")] string query, object? variables = null, - string? operationName = null, Func defineResponseType = null, CancellationToken cancellationToken = default) + string? operationName = null, Func? defineResponseType = null, CancellationToken cancellationToken = default) { _ = defineResponseType; return client.SendMutationAsync(new GraphQLRequest(query, variables, operationName), cancellationToken: cancellationToken); } +#if NET7_0_OR_GREATER + public static Task> SendMutationAsync(this IGraphQLClient client, + GraphQLQuery query, object? variables = null, string? operationName = null, Func? defineResponseType = null, + CancellationToken cancellationToken = default) + => SendMutationAsync(client, query.Text, variables, operationName, defineResponseType, + cancellationToken); +#endif + public static Task> SendQueryAsync(this IGraphQLClient client, GraphQLRequest request, Func defineResponseType, CancellationToken cancellationToken = default) { diff --git a/src/GraphQL.Client/GraphQL.Client.csproj b/src/GraphQL.Client/GraphQL.Client.csproj index 241e3bcc..7c5fa9af 100644 --- a/src/GraphQL.Client/GraphQL.Client.csproj +++ b/src/GraphQL.Client/GraphQL.Client.csproj @@ -1,7 +1,7 @@ - netstandard2.0;net461 + netstandard2.0;net461;net7.0;net8.0 GraphQL.Client.Http diff --git a/src/GraphQL.Client/GraphQLHttpRequest.cs b/src/GraphQL.Client/GraphQLHttpRequest.cs index b89d1814..dbb752ec 100644 --- a/src/GraphQL.Client/GraphQLHttpRequest.cs +++ b/src/GraphQL.Client/GraphQLHttpRequest.cs @@ -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? extensions = null) + : base(query, variables, operationName, extensions) + { + } +#endif + public GraphQLHttpRequest(GraphQLRequest other) : base(other) { diff --git a/src/GraphQL.Client/GraphQLSubscriptionException.cs b/src/GraphQL.Client/GraphQLSubscriptionException.cs index ccaab426..21dd8982 100644 --- a/src/GraphQL.Client/GraphQLSubscriptionException.cs +++ b/src/GraphQL.Client/GraphQLSubscriptionException.cs @@ -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() { } @@ -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 } diff --git a/src/GraphQL.Client/Websocket/GraphQLWebsocketConnectionException.cs b/src/GraphQL.Client/Websocket/GraphQLWebsocketConnectionException.cs index f9811b98..aedebb90 100644 --- a/src/GraphQL.Client/Websocket/GraphQLWebsocketConnectionException.cs +++ b/src/GraphQL.Client/Websocket/GraphQLWebsocketConnectionException.cs @@ -1,4 +1,6 @@ +#if !NET8_0_OR_GREATER using System.Runtime.Serialization; +#endif namespace GraphQL.Client.Http.Websocket; @@ -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 + } diff --git a/src/GraphQL.Primitives/GraphQLQuery.cs b/src/GraphQL.Primitives/GraphQLQuery.cs new file mode 100644 index 00000000..ec2c5089 --- /dev/null +++ b/src/GraphQL.Primitives/GraphQLQuery.cs @@ -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 diff --git a/src/GraphQL.Primitives/GraphQLRequest.cs b/src/GraphQL.Primitives/GraphQLRequest.cs index 7aecb656..dd5d3b5a 100644 --- a/src/GraphQL.Primitives/GraphQLRequest.cs +++ b/src/GraphQL.Primitives/GraphQLRequest.cs @@ -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? extensions = null) + : this(query.Text, variables, operationName, extensions) + { + } +#endif + public GraphQLRequest(GraphQLRequest other) : base(other) { } /// diff --git a/tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs b/tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs index cb25adc6..e20fdd39 100644 --- a/tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs +++ b/tests/GraphQL.Integration.Tests/QueryAndMutationTests/Base.cs @@ -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 } });