Skip to content

Instantly share code, notes, and snippets.

View benmccallum's full-sized avatar

Ben McCallum benmccallum

View GitHub Profile
@benmccallum
benmccallum / .Intructions.md
Last active February 5, 2020 14:25
Strongly-typed GraphQL Client solution

This gist demonstrates a solution to generating a strongly-typed client for a GraphQL service.

  1. Create a .csproj project (e.g. MyCompany.GraphQL.Queries)
  2. Add some queries using .graphql files
  3. Add the .ps1 script above
  4. Install quicktype
  5. Add a reference to Newtonsoft.Json to the project (required by the generated query files, though that may change in the future to System.Text.Json)
  6. Add a client wrapper that can utilise the query types and deserialize into them (see TypedGraphQLClient.cs)
  7. Use it elsewhere.
@benmccallum
benmccallum / main.yml
Created April 19, 2020 14:32
GitHub Action static site deployment to Azure Blog Storage fronted by a CDN
name: Deploy to Azure
on:
push:
branches:
- master
paths:
- 'ui/*'
jobs:
@benmccallum
benmccallum / index.js
Created April 23, 2020 09:34
Fully hide/show "Viewed Files" in the GitHub PR "Files changed" tab
// DevTools > Sources > Snippets > + New snippet > paste the below code in.
// You'll now be able to right-click and run the snippet to toggle them in and out
// (You may need to keep scrolling down to the bottom to fire the lazy-loading/infinite scrolling of files until the spinner is gone)
let styleEle = document.getElementById('asdfasdgsdfg');
if (Boolean(styleEle)) {
styleEle.remove();
} else {
styleEle = document.createElement('style');
styleEle.innerHTML = 'div[data-file-user-viewed] { display: none; }';
@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 / 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 / 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 / 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 / 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 / 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 / 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.