Skip to content

Instantly share code, notes, and snippets.

Avatar

Ben McCallum benmccallum

View GitHub Profile
@benmccallum
benmccallum / NodeExAttribute.cs
Created Apr 7, 2021
HotChocolate Node extensions for exposing dbId
View NodeExAttribute.cs
#nullable enable
using System;
using System.Linq;
using System.Reflection;
using HotChocolate.Types.Descriptors;
using HotChocolate.Utilities;
namespace HotChocolate.Types.Relay
{
View NodeResolverMiddleware.cs
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 Mar 23, 2021
Verify your GraphQL schema changes
View GraphQLSchemaTests.cs
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 Apr 2, 2021
Resolver scoping middleware (MediatR) for HotChocolate
View HC_ResolverScopedService
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 Nov 12, 2020
Abstraction around EF Core 3 DbContextPool
View IDbContextPool.cs
#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 Feb 20, 2021
Prevent introspection queries on HotChocolate
View HCv10_NoIntrospectionValidationRule.cs
/// <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 Jul 17, 2020
Get db-wide columns info
View script.sql
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 Jul 16, 2020
Disable nullable reference types in all files
View disabler.linqpad
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 Jun 18, 2020
Find a value anywhere in a MSSQL database
View FindValueInDatabase.sql
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 Jan 6, 2021
graphql-dotnet Enum value naming convention for HotChocolate
View NamingConventions.cs
/// <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