Skip to content

Instantly share code, notes, and snippets.

View andyrichardson's full-sized avatar

Andy Richardson andyrichardson

View GitHub Profile

Docker 101

Grammar

  • server: A physical machine connected to the web
  • service: A deployed program (e.g. GraphQL endpoint / REST endpoint)
  • environment: The OS that a service runs in

Before docker

Websocket vs Subscriptions

What is a websocket

Websockets create a sustained connection between a client and a server

Messages can be sent on this connection in either direction.

It is a transport protocol.

@andyrichardson
andyrichardson / ttl-cache.ts
Last active December 9, 2022 17:12
A ttl cache for use with Dataloader's cacheMap option
export const ttlCache = <K extends string, V = any>({ ttl, maxSize }: { ttl: number, maxSize?: number }) => {
let cache: Record<K, { value: V; expiresAt: number } | undefined> = Object.create(null);
return {
set: (key: K, value: V) => {
const keys = Object.keys(cache) as K[];
if (maxSize && keys.length === maxSize) {
delete cache[keys[0]];
}
@andyrichardson
andyrichardson / useSynchronousReducer.tsx
Last active November 25, 2020 16:08
A reducer hook which calls the provided reducer function synchronously and returns the updated state from the dispatch.
import { useState, useCallback, useRef } from 'react';
export const useSynchronousReducer = <S, A>(
reducer: (s: S, a: A) => S,
initialState: S
) => {
const stateRef = useRef(initialState);
const [state, setState] = useState<S>(stateRef.current);
const dispatch = useCallback<(a: A) => S>((action) => {
@andyrichardson
andyrichardson / storybook-react-router.md
Last active November 17, 2020 11:30
Example decorator for using

.storybook/preview.tsx

import { addDecorator, DecoratorFn } from '@storybook/react';

/** Adds react-router context */
const routerDecorator: DecoratorFn = (Story, context) => {
  const router = context.parameters.router;
@andyrichardson
andyrichardson / storybook-in-jest.md
Last active November 17, 2020 11:10
Example approach for testing storybook fixtures in jest.

About

Here's a quick example of how to get a 1-1 reproduction of storybook fixtures in jest.

Note: If you use storybook, definitely do this - it removes the need to set up context manually for jest testing

Create a providing component for jest

// .storybook/preview.tsx
@andyrichardson
andyrichardson / isIntrospectionQuery.ts
Created November 12, 2020 12:48
A secure method for checking if an incoming query is an introspection query.
const isIntrospectionQuery = (arg: string) => {
const query = parse(arg);
const opDefs = query.definitions.filter(d => d.kind == "OperationDefinition") as OperationDefinitionNode[];
// Must only have one definition
if (opDefs.length > 1) {
return false;
}
const selections = opDefs[0].selectionSet.selections;
@andyrichardson
andyrichardson / serializing a binary tree in javascript.md
Last active February 26, 2023 14:41
A cheat sheet for serializing and deserializing a binary tree in JavaScript

About

I absolutely failed at a timed challenge on how to serialize and deserialize a binary tree in JS.

This is my way of compensating for that failure 🤦‍

Hopefully this helps someone!

Data structure

@andyrichardson
andyrichardson / ApolloFixturesTemplate.tsx
Last active January 24, 2021 17:51
Template for triggering network and data states in fixtures that consume Apollo Client
import { ApolloProvider } from '@apollo/react-hooks';
import { ApolloClient, RequestHandler, InMemoryCache, ApolloLink, Observable } from 'apollo-boost';
import { GraphQLError } from 'graphql';
import React from 'react';
import { Users } from './Users';
export default {
title: 'Pages/Users',
};
@andyrichardson
andyrichardson / Suspense Urql.js
Created April 8, 2020 13:34
Quick and dirty Urql Suspense example
const useQuery = (query, opts) => {
const source = useRef();
const [state, setState] = useState();
const makeRead = useCallback(() => {
let status = "pending";
let response;
let promise = new Promise((resolve) => {
source.current = pipe(
client.createOperation(query, opts),