Skip to content

Instantly share code, notes, and snippets.

View benmccallum's full-sized avatar

Ben McCallum benmccallum

View GitHub Profile
@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 / 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 / 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 / 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 / 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>
@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 / script.sql
Created July 17, 2020 14:28
Get db-wide columns info
select
schema_name(tab.schema_id) as schema_name,
tab.name as table_name,
--col.column_id,
col.name as column_name,
col.is_nullable,
t.name as data_type,
col.precision,
col.scale
from sys.tables as tab
@benmccallum
benmccallum / disabler.linqpad
Created July 16, 2020 09:36
Disable nullable reference types in all files
const string sourcePath = @"C:\src\autoguru\src\Microservices";
const string enabledHeader = "#nullable enable";
const string disabledHeader = "#nullable disable";
var files = Directory.GetFiles(sourcePath, "*.cs", SearchOption.AllDirectories);
var projFiles = Directory.GetFiles(sourcePath, "*.csproj", SearchOption.AllDirectories);
foreach (var file in files)
{
var contents = File.ReadAllText(file);
@benmccallum
benmccallum / FindValueInDatabase.sql
Created June 18, 2020 07:19
Find a value anywhere in a MSSQL database
declare @SearchTerm nvarchar(4000) -- Can be max for SQL2005+
declare @ColumnName sysname
--------------------------------------------------------------------------------
-- SET THESE!
--------------------------------------------------------------------------------
set @SearchTerm = N'285' -- Term to be searched for, wildcards okay
set @ColumnName = N'ShippingSuburbID' -- Use to restrict the search to certain columns, wildcards okay, null or empty string for all cols
--------------------------------------------------------------------------------
-- END SET
@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