Skip to content

Instantly share code, notes, and snippets.

View benmccallum's full-sized avatar

Ben McCallum benmccallum

View GitHub Profile
@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 / .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:
@benmccallum
benmccallum / GraphQLSchemaTests.cs
Last active August 26, 2021 10:21
Verify your GraphQL schema changes
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Testing;
using Snapshooter.Json;
using Xunit;
namespace MyCompany.MyService
{
public class GraphQLSchemaTests
: IClassFixture<WebApplicationFactory<Startup>>
@benmccallum
benmccallum / _Instructions.md
Last active July 25, 2021 03:35
nuxtjs, bootstrap-vue with custom bootstrap build

Important! This guide is out of date (circa 2017) and should no longer be used. For the latest advice, please refer to the BootstrapVue docs and their Nuxt.js module. https://bootstrap-vue.org/docs#nuxt-js

General setup:

  1. Install bootstrap as a dev dependency, and its dependencies (node-sass and sass-loader) npm install --save-dev bootstrap@4.0.0-beta.2 node-sass sass-loader
@benmccallum
benmccallum / HCv10_NoIntrospectionValidationRule.cs
Last active May 20, 2021 21:59
Prevent introspection queries on HotChocolate
/// <summary>
/// Prevents use of certain field names typically used in introspection queries.
/// Useful for production environments where you may want to guard against such queries.
/// </summary>
/// <remarks>
/// Compatible with v10 of HotChocolate.
/// </summary>
public class NoIntrospectionValidationRule : IQueryValidationRule
{
private static readonly HashSet<string> _bannedFieldNames = new HashSet<string>() { "__schema", "__type" };
@benmccallum
benmccallum / HC_ResolverScopedService
Last active April 2, 2021 14:43
Resolver scoping middleware (MediatR) for HotChocolate
A middleware to provide resolvers with their own, scoped instance, of a service. Example here is IMediator, but this could be made generic.
@benmccallum
benmccallum / NamingConventions.cs
Last active January 6, 2021 16:24
graphql-dotnet Enum value naming convention for HotChocolate
/// <summary>
/// Custom naming conventions.
///
/// Usage in v10:
/// <c>services.AddSingleton&lt;INamingConventions, NamingConventions&gt;()</c>
/// <c>IRequestExecutorBuilder.AddConvention<INamingConventions>(new NamingConventions())</c>
/// Usage in v11:
///
/// </summary>
public class NamingConventions : DefaultNamingConventions
@benmccallum
benmccallum / IDbContextPool.cs
Last active November 12, 2020 15:34
Abstraction around EF Core 3 DbContextPool
#nullable enable
using Microsoft.EntityFrameworkCore;
namespace MyCompany
{
/// <summary>
/// An abstraction around EF Core's DbContextPool (as it's an internal API that becomes public in .NET 5)
/// for renting a DbContext.
/// </summary>
public interface IDbContextPool<TDbContext>