Skip to content

Instantly share code, notes, and snippets.

View benmccallum's full-sized avatar

Ben McCallum benmccallum

View GitHub Profile
@benmccallum
benmccallum / _Instructions.md
Last active August 26, 2023 14:36
git pre-commit hook preventing large files

Usage

You can use in two ways.

  1. Directly as the pre-commit hook in your .git/hooks folder.

  2. With Husky by updating your package.json with:

"husky": {
@benmccallum
benmccallum / AddDbIdFieldToNodeTypesTypeInterceptor.cs
Last active June 19, 2023 02:28
HotChocolate Node extensions for exposing dbId
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using HotChocolate.Configuration;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
namespace HotChocolate.Types.Relay
{
/// <summary>
@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 / 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 / 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 / NodeResolverMiddleware.cs
Last active June 17, 2022 06:20
NodeResolver when stitching in Hot Chocolate
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using HotChocolate;
using HotChocolate.Execution;
using HotChocolate.Language;
using HotChocolate.Resolvers;
using HotChocolate.Stitching;
@benmccallum
benmccallum / Annecy.md
Last active March 12, 2022 12:53
Annecy
@benmccallum
benmccallum / SqlKataExample.cs
Last active February 14, 2022 06:59
Cursor-based paging for connections with MS SQL CTEs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using AutoGuru.Client.Shared;
using AutoGuru.Client.Shared.Dtos;
using AutoGuru.Client.Shared.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
@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>