Skip to content

Instantly share code, notes, and snippets.

View derrh's full-sized avatar

Derrick Hathaway derrh

View GitHub Profile
@derrh
derrh / SwiftUI+GraphQL.md
Created October 16, 2023 19:53
GraphQL + SwiftUI: Declarative Data and Declarative UI

Imagining what the future might be like with direct support for GraphQL in SwiftUI.

Rationale

SwiftUI and GraphQL both represent new paradigms, these new paradigms—when paired together—offer an unparalleled, streamlined, client development experience. Today the most widely used and robust Swift GraphQL clients lack direct SwiftUI support, as well as support for #8 and #9 of Jordan Eldridge's "GraphQL Maturity Model".

Goals

  • Each View declares it's data requirements using Fragments
  • Each View observes it's fragment data from the GraphQL normalized cache, limiting large-tree re-renders
  • Sensible defaults for cache policies and other configuration cover most cases, but can be overridden in intuitive ways
  • As Views are decomposed into subviews, queries are decomposed into fragments
type RouteLeaf = string | number | bigint | boolean | null | undefined
// there's probably a tidier way to get around TS's lack of support for recursive `type`'s
type RouteBranch<SubBranch = RouteLeaf> = Record<string | number | symbol, RouteLeaf | SubBranch>
type RouteComponent =
| RouteLeaf
| RouteBranch<RouteBranch>
| RouteBranch<RouteBranch<RouteBranch>>
| RouteBranch<RouteBranch<RouteBranch<RouteBranch>>>
| RouteBranch<RouteBranch<RouteBranch<RouteBranch<RouteBranch>>>>
@derrh
derrh / Interview.tsx
Created January 7, 2020 17:07
Device Orientation
import React from 'react';
import { Orientation } from 'react-native-device-orientation';
// Orientation.addSubscriber(callback: (orientation: 'portrait' | 'landscape') => {})
// Orientation.removeSubscriber(callback)
@derrh
derrh / Optional.swift
Created May 2, 2018 23:32
Hello, Silicon Slopes!
func format(date: Date) -> String { ... }
let dueDate = Date?
print("""
Assignment
--
due: \(dueDate.map(format(date:)) ?? "none")
"""
@derrh
derrh / PolymorphicValueTypes.swift
Created December 13, 2016 06:06
Polymorphism with Swift Value Types: Mind Blown
protocol Thing {
func foo()
}
extension Thing {
func foo() {
print("just a thing")
}
}
Process: carthage [25835]
Path: /usr/local/bin/carthage
Identifier: carthage
Version: 0
Code Type: X86-64 (Native)
Parent Process: bash [21316]
Responsible: Terminal [21310]
User ID: 1745180192
Date/Time: 2015-05-26 10:54:31.718 -0600
//
// NSManagedObjectContext+Project.swift
//
// Created by Derrick Hathaway on 2/22/15.
// Copyright (c) 2015 The Best Bits LLC.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
import UIKit
func +<A,B>(lhs: [A:B], rhs: [A:B]) -> [A:B] {
var dict : [A:B] = [:]
for (key, value) in lhs {
dict[key] = value
}
for (key, value) in rhs {
dict[key] = value
@derrh
derrh / CategoryProperty.h
Created June 25, 2013 20:21
Synthesize + objc_get/setAssociatedObject
#import <objc/runtime.h>
#define SYNTHESIZE(class, getter, setter) \
static const void *getter##PropertyKey = &getter##PropertyKey;\
- (type *)getter { \
return objc_getAssociatedObject(self, getter##PropertyKey); \
} \
\
- (void)setter:(class *object) { \
objc_setAssociatedObject(self, getter##PropertyKey, object, OBJC_ASSOCIATION_RETAIN);\
@derrh
derrh / boolean_variety.h
Last active December 16, 2015 03:59
Our code is so boring sometimes. This will help.
// boolean_variety.h
//
// Copyright (c) 2013 Derrick Hathaway
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions: