-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from graphql-dotnet/invalid-empty-and-profiling
Invalidate empty query, and profiling extensions
- Loading branch information
Showing
16 changed files
with
277 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using GraphQL.Conventions; | ||
using GraphQL.Conventions.Attributes; | ||
using GraphQL.Conventions.Attributes.Collectors; | ||
using GraphQL.Conventions.Execution; | ||
using GraphQL.Conventions.Types.Descriptors; | ||
|
||
namespace GraphQL.Conventions.Extensions | ||
{ | ||
public class ChaosAttribute : ExecutionFilterAttributeBase | ||
{ | ||
public static bool IsEnabled = false; | ||
|
||
private const int DefaultSuccessRate = 50; | ||
|
||
private static readonly Random _random = new Random(); | ||
|
||
private readonly int _successRate; | ||
|
||
public ChaosAttribute(int successRate = DefaultSuccessRate) | ||
{ | ||
_successRate = successRate; | ||
} | ||
|
||
public override Task<object> Execute(IResolutionContext context, FieldResolutionDelegate next) | ||
{ | ||
if (_random.Next(0, 100) > _successRate) | ||
{ | ||
var path = $"{context.FieldInfo.DeclaringType.Name}.{context.FieldInfo.Name}"; | ||
throw new ChaosException($"Only {_successRate} % of requests will succeed.", path); | ||
} | ||
return next(context); | ||
} | ||
} | ||
|
||
public class ChaosMetaDataAttribute : MetaDataAttributeBase, IDefaultAttribute | ||
{ | ||
public override void MapField(GraphFieldInfo entity, MemberInfo memberInfo) | ||
{ | ||
if (ChaosAttribute.IsEnabled) | ||
{ | ||
entity.ExecutionFilters.Add(new ChaosAttribute()); | ||
} | ||
} | ||
} | ||
|
||
public class ChaosException : Exception | ||
{ | ||
public ChaosException(string message, string path) | ||
: base(message) | ||
{ | ||
Path = path; | ||
} | ||
|
||
public string Path { get; private set; } | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/GraphQL.Conventions/Extensions/NameNormalizerAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Reflection; | ||
using GraphQL.Conventions.Attributes; | ||
using GraphQL.Conventions.Attributes.Collectors; | ||
using GraphQL.Conventions.Types.Descriptors; | ||
|
||
namespace GraphQL.Conventions.Extensions | ||
{ | ||
public class NameNormalizerAttribute : MetaDataAttributeBase, IDefaultAttribute | ||
{ | ||
public NameNormalizerAttribute() | ||
: base(AttributeApplicationPhase.Override) | ||
{ | ||
} | ||
|
||
public override void MapType(GraphTypeInfo entity, TypeInfo typeInfo) | ||
{ | ||
if (entity.Name?.EndsWith("Dto") ?? false) | ||
{ | ||
entity.Name = entity.Name.Remove(entity.Name.Length - 3); | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/GraphQL.Conventions/Extensions/Profiling/PerformanceRecord.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Collections.Generic; | ||
using Newtonsoft.Json; | ||
|
||
namespace GraphQL.Conventions.Extensions | ||
{ | ||
public class PerformanceRecord | ||
{ | ||
[JsonProperty(PropertyName = "path")] | ||
public string Path { get; set; } | ||
|
||
[JsonProperty(PropertyName = "start")] | ||
public long StartTimeInMs { get; set; } | ||
|
||
[JsonProperty(PropertyName = "end")] | ||
public long EndTimeInMs { get; set; } | ||
|
||
[JsonProperty(PropertyName = "type")] | ||
public string ParentType { get; set; } | ||
|
||
[JsonProperty(PropertyName = "field")] | ||
public string Field { get; set; } | ||
|
||
[JsonProperty(PropertyName = "args")] | ||
public Dictionary<string, object> Arguments { get; set; } | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/GraphQL.Conventions/Extensions/Profiling/ProfilingResultEnricher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using GraphQL.Conventions; | ||
using GraphQL.Conventions.Web; | ||
|
||
namespace GraphQL.Conventions.Extensions | ||
{ | ||
public static class ProfilingResultEnricher | ||
{ | ||
public static void EnrichWithProfilingInformation(this Response response) | ||
{ | ||
var perf = response?.ExecutionResult?.Perf; | ||
if (perf == null) { return; } | ||
|
||
var records = new List<PerformanceRecord>(); | ||
foreach (var record in perf) | ||
{ | ||
if (record.Category != "field") { continue; } | ||
|
||
records.Add(new PerformanceRecord | ||
{ | ||
Path = string.Join(".", record.Metadata["path"] as List<string>), | ||
StartTimeInMs = record.Start, | ||
EndTimeInMs = record.End, | ||
ParentType = record.Metadata["typeName"] as string, | ||
Field = record.Metadata["fieldName"] as string, | ||
Arguments = record.Metadata["arguments"] as Dictionary<string, object>, | ||
}); | ||
} | ||
|
||
if (records.Any()) | ||
{ | ||
response.AddExtra("profile", records.OrderBy(record => record.Path)); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using GraphQL.Conventions; | ||
using GraphQL.Conventions.Web; | ||
|
||
namespace GraphQL.Conventions.Extensions | ||
{ | ||
public static class Utilities | ||
{ | ||
public static string IdentifierForTypeOrNull<T>(this string id) => | ||
id.IsIdentifierForType<T>() ? id.IdentifierForType<T>() : null; | ||
|
||
public static string IdentifierForTypeOrNull<T>(this NonNull<string> id) => | ||
id.IsIdentifierForType<T>() ? id.IdentifierForType<T>() : null; | ||
|
||
public static string IdentifierForType<T>(this string id) => | ||
new Id(id).IdentifierForType<T>(); | ||
|
||
public static string IdentifierForType<T>(this NonNull<string> id) => | ||
id.Value.IdentifierForType<T>(); | ||
|
||
public static bool IsIdentifierForType<T>(this string id) => | ||
new Id(id).IsIdentifierForType<T>(); | ||
|
||
public static bool IsIdentifierForType<T>(this NonNull<string> id) => | ||
id.Value.IsIdentifierForType<T>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters