Skip to content

Instantly share code, notes, and snippets.

View benmccallum's full-sized avatar

Ben McCallum benmccallum

View GitHub Profile
@benmccallum
benmccallum / HotChocolateAuthorizeRequestExecutorBuilder.cs
Last active May 11, 2023 01:12
HCv13 AddAuthorization extension method
using System;
using HotChocolate;
using HotChocolate.AspNetCore.Authorization;
using HotChocolate.Execution.Configuration;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection
{
/// <summary>
@benmccallum
benmccallum / .README.md
Last active November 15, 2022 03:57
Wrappers around ASP.NET Core Session

Wrappers to ensure distributed session loading is done asynchronously as per advice in MS docs.

Note: It looks like currently it's kind of pointless to try and optimize LoadAsync to only occur where needed (as in my middleware below checking for at least the cookie, or as per suggestions in closed issues to have an attribute that could be sprinkled on controllers/actions) as the standard session middleware always runs CommitAsync (see here), which in turn checks ISession.IsAvailable, which calls Load and ends up doing a sync load... So as far as I can tell, as soon as you've got UseSession() in your pipeline, bar shortcircuited handling for things like static files, you're going to have this CommitAsync run, regardless of what your app code is doing (setting or getting values from Session). Still testing if there's something else that avoids the unnecessary load though.

Usage:

// Services setup
services.Configure<OurDistributedSessionSettings>(
    Config.GetSection("OurDistributedSes
@benmccallum
benmccallum / RemoveFieldsRewriter.cs
Created July 27, 2022 23:52
Removing fields with ITypeRewriter
using System;
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Language;
using HotChocolate.Stitching.Merge;
using HotChocolate.Stitching.Merge.Rewriters;
using HotChocolate.Types;
namespace MyCompany.GraphQLService.Stitching
{
@benmccallum
benmccallum / Annecy.md
Last active March 12, 2022 12:53
Annecy
@benmccallum
benmccallum / MergeDirectivesHandler.cs
Last active January 27, 2022 15:19
Hot Chocolate merge handlers for stitching
using System;
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Stitching.Merge;
namespace MyCompany.GatewayService.Stitching
{
/// <summary>
/// <para>This merge handler deals with duplicate directives when schemas are stitched.</para>
/// <para>It currently handles dupes of: authorize.</para>
@benmccallum
benmccallum / README.md
Last active January 19, 2022 09:32
Remove internal GraphQL fields ITypeRewriter

Removes any fields that look to be internal based on presence of "dbId" in field name or arg.

services.
  .AddGraphQLServer()
  .AddTypeRewriter(new RemoveInternalFieldsRewriter())
@benmccallum
benmccallum / OurDiagnosticEventListener.cs
Last active August 11, 2022 03:57
HotChocolate logging example
using System;
using System.Collections.Generic;
using HotChocolate;
using HotChocolate.Execution;
using HotChocolate.Execution.Instrumentation;
using HotChocolate.Resolvers;
using Microsoft.Extensions.Logging;
namespace MyCompany.GraphQL.Execution.Instrumentation
{
@benmccallum
benmccallum / .readme.md
Last active November 18, 2021 08:43
EnumConverterUsingEnumParse

A converter setup for System.Text.Json that:

  1. Deserializes numeric value or the string (member name or numeric) into the enum.
  2. Serializes out as the numeric value.
  3. Serializes out as the member name when needed as a property name (e.g. used as a dictionary key).

Helpful in transition off Newtonsoft if your clients were passing string values but you still want to serialize out as numeric. Related issue that might make this redundant: dotnet/runtime#61726

Usage: options.Converters.Add(new EnumConverterUsingEnumParseFactory());

@benmccallum
benmccallum / VerifyExtensions.cs
Created November 3, 2021 10:36
Verify - ScrubMatches - regex replacement with incrementing id
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text.RegularExpressions;
namespace VerifyTests
{
public static class VerifySettingsExtensions
{
@benmccallum
benmccallum / IRazorEngineExtensions.cs
Last active November 3, 2021 08:15
RazorEngineCore HTML encoding with layout
using System;
using System.Collections.Generic;
using System.Linq;
using RazorEngineCore;
namespace MyCompany.Templating.RazorEngineCore
{
public static class IRazorEngineExtensions
{
// See: