Skip to content

Commit f8f7f13

Browse files
author
Samuel Abraham
committed
v9.3.0
1 parent c3fc275 commit f8f7f13

161 files changed

Lines changed: 9282 additions & 8679 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/TypeCache.GraphQL/Converters/GraphQLExecutionErrorJsonConverter.cs

Lines changed: 102 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright (c) 2021 Samuel Abraham
22

3+
using System.Collections;
34
using System.Text.Json;
45
using System.Text.Json.Serialization;
56
using GraphQL;
@@ -9,37 +10,128 @@ namespace TypeCache.GraphQL.Converters;
910

1011
public sealed class GraphQLExecutionErrorJsonConverter : JsonConverter<ExecutionError>
1112
{
13+
public override bool CanConvert(Type typeToConvert) => typeof(ExecutionError).IsAssignableFrom(typeToConvert);
14+
1215
/// <exception cref="NotImplementedException"></exception>
1316
public override ExecutionError Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1417
=> throw new NotImplementedException();
1518

16-
public override void Write(Utf8JsonWriter writer, ExecutionError? value, JsonSerializerOptions options)
19+
public override void Write(Utf8JsonWriter writer, ExecutionError? error, JsonSerializerOptions options)
1720
{
18-
if (value is null)
21+
if (error is null)
1922
{
2023
writer.WriteNullValue();
2124
return;
2225
}
2326

2427
writer.WriteStartObject();
25-
writer.WriteString("message", value.Message);
2628

27-
if (value.Locations is not null)
29+
if (error.Code.IsNotBlank)
30+
writer.WriteString("code", error.Code);
31+
32+
if (error.Message.IsNotBlank)
33+
writer.WriteString("message", error.Message);
34+
35+
if (error.Source.IsNotBlank)
36+
writer.WriteString("source", error.Source);
37+
38+
if (error.HelpLink.IsNotBlank)
39+
writer.WriteString("help-link", error.HelpLink);
40+
41+
if (error.Path?.Any() is true)
42+
{
43+
writer.WritePropertyName("path");
44+
45+
writer.WriteStartArray();
46+
error.Path.ForEach(_ => writer.WriteValue(_, options));
47+
writer.WriteEndArray();
48+
}
49+
50+
if (error.Data.Count > 0)
51+
{
52+
writer.WritePropertyName("data");
53+
54+
writer.WriteStartObject();
55+
error.Data.OfType<DictionaryEntry>().ForEach(_ =>
56+
{
57+
writer.WritePropertyName(_.Key.ToString()!);
58+
writer.WriteValue(_.Value, options);
59+
});
60+
writer.WriteEndObject();
61+
}
62+
63+
if (error.Locations?.Count > 0)
2864
{
2965
writer.WritePropertyName("locations");
30-
JsonSerializer.Serialize(writer, value.Locations, options);
66+
67+
writer.WriteStartArray();
68+
error.Locations.ForEach(_ =>
69+
{
70+
writer.WriteStartObject();
71+
writer.WriteNumber("line", _.Line);
72+
writer.WriteNumber("column", _.Column);
73+
writer.WriteEndObject();
74+
});
75+
writer.WriteEndArray();
3176
}
3277

33-
if (value.Path?.Any() is true)
78+
if (error.StackTrace.IsNotBlank)
3479
{
35-
writer.WritePropertyName("path");
36-
JsonSerializer.Serialize(writer, value.Path, options);
80+
writer.WritePropertyName("stack-trace");
81+
82+
writer.WriteStartArray();
83+
error.StackTrace.SplitEx(Environment.NewLine).ForEach(writer.WriteStringValue);
84+
writer.WriteEndArray();
3785
}
3886

39-
if (value.Extensions?.Any() is true)
87+
if (error.Extensions?.Count > 0)
4088
{
4189
writer.WritePropertyName("extensions");
42-
JsonSerializer.Serialize(writer, value.Extensions, options);
90+
91+
writer.WriteStartObject();
92+
error.Extensions.ForEach(_ =>
93+
{
94+
writer.WritePropertyName(_.Key);
95+
writer.WriteValue(_.Value, options);
96+
});
97+
writer.WriteEndObject();
98+
}
99+
100+
var innerError = error.InnerException;
101+
if (innerError is not null)
102+
{
103+
writer.WritePropertyName("inner-errors");
104+
105+
writer.WriteStartArray();
106+
do
107+
{
108+
Write(writer, innerError);
109+
innerError = innerError.InnerException;
110+
}
111+
while (innerError is not null);
112+
writer.WriteEndArray();
113+
}
114+
115+
writer.WriteEndObject();
116+
}
117+
118+
private static void Write(Utf8JsonWriter writer, Exception error)
119+
{
120+
writer.WriteStartObject();
121+
122+
writer.WriteString("type", error.GetType().CodeName);
123+
124+
if (error.Message.IsNotBlank)
125+
writer.WriteString("message", error.Message);
126+
127+
if (error.StackTrace.IsNotBlank)
128+
{
129+
writer.WritePropertyName("stack-trace");
130+
writer.WriteStartArray();
131+
132+
error.StackTrace.SplitEx(Environment.NewLine).ForEach(writer.WriteStringValue);
133+
134+
writer.WriteEndArray();
43135
}
44136

45137
writer.WriteEndObject();

src/TypeCache.GraphQL/Converters/GraphQLExecutionResultJsonConverter.cs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace TypeCache.GraphQL.Converters;
1010

1111
public sealed class GraphQLExecutionResultJsonConverter : JsonConverter<ExecutionResult>
1212
{
13+
public override bool CanConvert(Type typeToConvert) => typeof(ExecutionResult).IsAssignableFrom(typeToConvert);
14+
1315
/// <exception cref="NotImplementedException"></exception>
1416
public override ExecutionResult Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1517
=> throw new NotImplementedException();
@@ -27,28 +29,33 @@ public override void Write(Utf8JsonWriter writer, ExecutionResult? value, JsonSe
2729
if (value.Errors?.Any() is true)
2830
{
2931
writer.WritePropertyName("errors");
30-
JsonSerializer.Serialize(writer, value.Errors, options);
32+
33+
writer.WriteStartArray();
34+
value.Errors.ForEach(error => JsonSerializer.Serialize(writer, error, options));
35+
writer.WriteEndArray();
3136
}
3237

3338
if (value.Executed)
3439
{
3540
writer.WritePropertyName("data");
41+
3642
if (value.Data is ExecutionNode executionNode)
37-
WriteExecutionNode(writer, executionNode, options);
43+
Write(writer, executionNode, options);
3844
else
3945
JsonSerializer.Serialize(writer, value.Data, options);
4046
}
4147

4248
if (value.Extensions?.Any() is true)
4349
{
4450
writer.WritePropertyName("extensions");
51+
4552
JsonSerializer.Serialize(writer, value.Extensions, options);
4653
}
4754

4855
writer.WriteEndObject();
4956
}
5057

51-
private static void WriteExecutionNode(Utf8JsonWriter writer, ExecutionNode executionNode, JsonSerializerOptions options)
58+
private static void Write(Utf8JsonWriter writer, ExecutionNode executionNode, JsonSerializerOptions options)
5259
{
5360
switch (executionNode)
5461
{
@@ -61,17 +68,20 @@ private static void WriteExecutionNode(Utf8JsonWriter writer, ExecutionNode exec
6168
{
6269
var propertyName = options.PropertyNamingPolicy?.ConvertName(childNode.Name!) ?? childNode.Name!;
6370
writer.WritePropertyName(propertyName);
64-
WriteExecutionNode(writer, childNode, options);
71+
Write(writer, childNode, options);
6572
});
6673
writer.WriteEndObject();
6774
break;
6875
case ArrayExecutionNode arrayExecutionNode when arrayExecutionNode.Items is not null:
6976
writer.WriteStartArray();
70-
arrayExecutionNode.Items.ForEach(childNode => WriteExecutionNode(writer, childNode, options));
77+
arrayExecutionNode.Items.ForEach(childNode => Write(writer, childNode, options));
7178
writer.WriteEndArray();
7279
break;
80+
case ArrayExecutionNode arrayExecutionNode when arrayExecutionNode.SerializedResult is not null:
81+
JsonSerializer.Serialize(writer, arrayExecutionNode.SerializedResult, options);
82+
break;
83+
case ArrayExecutionNode arrayExecutionNode:
7384
case ObjectExecutionNode:
74-
case ArrayExecutionNode:
7585
case NullExecutionNode:
7686
case null:
7787
writer.WriteNullValue();

src/TypeCache.GraphQL/Data/Connection.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,33 @@ public static ObjectGraphType CreateGraphType(string name, IGraphType dataGraphT
4040
{
4141
var graphType = new ObjectGraphType
4242
{
43-
Name = Invariant($"{name}{nameof(Connection<T>)}"),
44-
Description = Type<Connection<T>>.Attributes.GraphQLDescription()
43+
Name = Invariant($"{name}{nameof(Connection<>)}"),
44+
Description = Type<Connection<T>>.Attributes.GraphQLDescription
4545
};
4646
var edgeGraphType = Edge<T>.CreateGraphType(name, dataGraphType);
4747

4848
graphType.AddField(new()
4949
{
50-
Name = nameof(Connection<T>.Edges),
50+
Name = nameof(Connection<>.Edges),
5151
ResolvedType = new ListGraphType(new NonNullGraphType(edgeGraphType)),
5252
Resolver = new FuncFieldResolver<Connection<T>, Edge<T>[]?>(context => context.Source.Edges)
5353
});
5454
graphType.AddField(new()
5555
{
56-
Name = nameof(Connection<T>.Items),
56+
Name = nameof(Connection<>.Items),
5757
ResolvedType = new ListGraphType(new NonNullGraphType(dataGraphType)),
5858
Resolver = new FuncFieldResolver<Connection<T>, T[]?>(context => context.Source.Items)
5959
});
6060
graphType.AddField(new()
6161
{
62-
Name = nameof(Connection<T>.PageInfo),
62+
Name = nameof(Connection<>.PageInfo),
6363
ResolvedType = new OutputGraphType<PageInfo>(Invariant($"{name}{nameof(PageInfo)}")),
6464
//Type = typeof(GraphQLObjectType<PageInfo>),
6565
Resolver = new FuncFieldResolver<Connection<T>, PageInfo?>(context => context.Source.PageInfo)
6666
});
6767
graphType.AddField(new()
6868
{
69-
Name = nameof(Connection<T>.TotalCount),
69+
Name = nameof(Connection<>.TotalCount),
7070
Type = ScalarType.Int32.ToGraphType(),
7171
Resolver = new FuncFieldResolver<Connection<T>, int?>(context => context.Source.TotalCount)
7272
});

src/TypeCache.GraphQL/Data/Edge.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ public static ObjectGraphType CreateGraphType(string name, IGraphType dataGraphT
1818
{
1919
var graphType = new ObjectGraphType
2020
{
21-
Name = Invariant($"{name}{nameof(Edge<T>)}"),
22-
Description = Type<Edge<T>>.Attributes.GraphQLDescription()
21+
Name = Invariant($"{name}{nameof(Edge<>)}"),
22+
Description = Type<Edge<T>>.Attributes.GraphQLDescription
2323
};
2424

2525
graphType.AddField(new()
2626
{
27-
Name = nameof(Edge<T>.Cursor),
27+
Name = nameof(Edge<>.Cursor),
2828
Type = ScalarType.Int32.ToGraphType(),
2929
Resolver = new FuncFieldResolver<Edge<T>, int>(context => context.Source.Cursor)
3030
});
3131
graphType.AddField(new()
3232
{
33-
Name = nameof(Edge<T>.Node),
33+
Name = nameof(Edge<>.Node),
3434
ResolvedType = new NonNullGraphType(dataGraphType),
3535
Resolver = new FuncFieldResolver<Edge<T>, T>(context => context.Source.Node)
3636
});

src/TypeCache.GraphQL/Extensions/ApplicationBuilderExtensions.cs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,20 @@ namespace TypeCache.GraphQL.Extensions;
1212

1313
public static class ApplicationBuilderExtensions
1414
{
15-
/// <summary>
16-
/// <c>=&gt; @<paramref name="this"/>.UseMiddleware&lt;<see cref="GraphQLMiddleware"/>&gt;(<paramref name="route"/>, <see langword="new"/> <see cref="ConfigureSchema"/>(<paramref name="configureSchema"/>));</c>
17-
/// </summary>
18-
/// <param name="route">The route to use for this <c><see cref="ISchema"/></c> instance.</param>
19-
public static IApplicationBuilder UseGraphQLSchema(this IApplicationBuilder @this, PathString route, IConfigureSchema configureSchema)
20-
=> @this.UseMiddleware<GraphQLMiddleware>(route, configureSchema);
15+
extension(IApplicationBuilder @this)
16+
{
17+
/// <summary>
18+
/// <c>=&gt; @this.UseMiddleware&lt;<see cref="GraphQLMiddleware"/>&gt;(<paramref name="route"/>, <see langword="new"/> <see cref="ConfigureSchema"/>(<paramref name="configureSchema"/>));</c>
19+
/// </summary>
20+
/// <param name="route">The route to use for this <c><see cref="ISchema"/></c> instance.</param>
21+
public IApplicationBuilder UseGraphQLSchema(PathString route, IConfigureSchema configureSchema)
22+
=> @this.UseMiddleware<GraphQLMiddleware>(route, configureSchema);
2123

22-
/// <summary>
23-
/// <c>=&gt; @<paramref name="this"/>.UseMiddleware&lt;<see cref="GraphQLMiddleware"/>&gt;(<paramref name="route"/>, <see langword="new"/> <see cref="ConfigureSchema"/>(<paramref name="configureSchema"/>));</c>
24-
/// </summary>
25-
/// <param name="route">The route to use for this <c><see cref="ISchema"/></c> instance.</param>
26-
public static IApplicationBuilder UseGraphQLSchema(this IApplicationBuilder @this, PathString route, Action<ISchema, IServiceProvider> configureSchema)
27-
=> @this.UseMiddleware<GraphQLMiddleware>(route, new ConfigureSchema(configureSchema));
24+
/// <summary>
25+
/// <c>=&gt; @this.UseMiddleware&lt;<see cref="GraphQLMiddleware"/>&gt;(<paramref name="route"/>, <see langword="new"/> <see cref="ConfigureSchema"/>(<paramref name="configureSchema"/>));</c>
26+
/// </summary>
27+
/// <param name="route">The route to use for this <c><see cref="ISchema"/></c> instance.</param>
28+
public IApplicationBuilder UseGraphQLSchema(PathString route, Action<ISchema, IServiceProvider> configureSchema)
29+
=> @this.UseMiddleware<GraphQLMiddleware>(route, new ConfigureSchema(configureSchema));
30+
}
2831
}

src/TypeCache.GraphQL/Extensions/AttributeExtensions.cs

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,33 @@ namespace TypeCache.GraphQL.Extensions;
77

88
public static class AttributeExtensions
99
{
10-
[MethodImpl(AggressiveInlining), DebuggerHidden]
11-
public static string? GraphQLDeprecationReason(this IEnumerable<Attribute> @this)
12-
=> @this.OfType<GraphQLDeprecationReasonAttribute>().FirstOrDefault()?.DeprecationReason?.NullIfBlank();
10+
extension(IEnumerable<Attribute> @this)
11+
{
12+
[DebuggerHidden]
13+
public string? GraphQLDeprecationReason => @this.OfType<GraphQLDeprecationReasonAttribute>().FirstOrDefault()?.DeprecationReason?.IfBlank(null);
1314

14-
[MethodImpl(AggressiveInlining), DebuggerHidden]
15-
public static string? GraphQLDescription(this IEnumerable<Attribute> @this)
16-
=> @this.OfType<GraphQLDescriptionAttribute>().FirstOrDefault()?.Description?.NullIfBlank();
15+
[DebuggerHidden]
16+
public string? GraphQLDescription => @this.OfType<GraphQLDescriptionAttribute>().FirstOrDefault()?.Description?.IfBlank(null);
1717

18-
[MethodImpl(AggressiveInlining), DebuggerHidden]
19-
public static bool GraphQLIgnore(this IEnumerable<Attribute> @this)
20-
=> @this.Any<GraphQLIgnoreAttribute>();
18+
[DebuggerHidden]
19+
public bool GraphQLIgnore => @this.Any<GraphQLIgnoreAttribute>();
2120

22-
public static string? GraphQLInputName(this IEnumerable<Attribute> @this)
23-
{
24-
var name = @this.OfType<GraphQLInputNameAttribute>().FirstOrDefault()?.Name?.NullIfBlank();
25-
if (name.IsNotBlank())
26-
return name;
21+
[DebuggerHidden]
22+
public string? GraphQLInputName => @this.OfType<GraphQLInputNameAttribute>().FirstOrDefault()?.Name?.IfBlank(null);
2723

28-
name = @this.GraphQLName();
29-
if (name.IsNotBlank())
30-
return Invariant($"{@this.GraphQLName()}Input");
24+
[DebuggerHidden]
25+
public bool GraphQLMutation => @this.Any<GraphQLMutationAttribute>();
3126

32-
return null;
33-
}
27+
[DebuggerHidden]
28+
public string? GraphQLName => @this.OfType<GraphQLNameAttribute>().FirstOrDefault()?.Name?.IfBlank(null);
3429

35-
[MethodImpl(AggressiveInlining), DebuggerHidden]
36-
public static string? GraphQLName(this IEnumerable<Attribute> @this)
37-
=> @this.OfType<GraphQLNameAttribute>().FirstOrDefault()?.Name?.NullIfBlank();
30+
[DebuggerHidden]
31+
public bool GraphQLQuery => @this.Any<GraphQLQueryAttribute>();
3832

39-
[MethodImpl(AggressiveInlining), DebuggerHidden]
40-
public static Type? GraphQLType(this IEnumerable<Attribute> @this)
41-
=> @this.OfType<GraphQLTypeAttribute>().FirstOrDefault()?.Type;
33+
[DebuggerHidden]
34+
public bool GraphQLSubscription => @this.Any<GraphQLSubscriptionAttribute>();
35+
36+
[DebuggerHidden]
37+
public Type? GraphQLType => @this.OfType<GraphQLTypeAttribute>().FirstOrDefault()?.Type;
38+
}
4239
}

0 commit comments

Comments
 (0)