Skip to content

Instantly share code, notes, and snippets.

View AndrewIngram's full-sized avatar
🦊
Being foxy

Andy Ingram AndrewIngram

🦊
Being foxy
View GitHub Profile
@AndrewIngram
AndrewIngram / example.sql
Last active October 28, 2021 19:26
Batch pagination
-- This is one technique for batching up the *first* page of results when doing pagination.
-- Potentially useful when using GraphQL and Dataloader.
--
-- In this example, we return the first 10 books whose author_id is 1,2,3,4 or 5 and genre
-- is "biography", ordered by "title"
--
-- For cursor-based (keyset) pagination, this technique won't work for anything after the
-- first "page", because the where clause needs to be the same for every entry in the
-- batch, which means you can't use different cursors. In practice, there isn't usually a
-- need to batch up subsequent pages, because your "next page" GQL queries will typically
import * as React from "react";
import { graphql, usePreloadedQuery } from "react-relay";
import { Button, Heading, Stack, Text } from "native-base";
import { ProfileScreenQuery } from "~/__generated__/ProfileScreenQuery.graphql";
import useLogout from "~/hooks/useLogout";
import { TabParamListProps } from "~/navigation-types";
import withData, { IScreenContainerProps } from "~/utils/withData";
@AndrewIngram
AndrewIngram / 01_connections.py
Last active December 10, 2021 23:47
Strawberry Generic Connections (Python 3.10 syntax)
from typing import Generic, TypeVar
import strawberry
ConnectionNode = TypeVar("ConnectionNode")
@strawberry.type
class Edge(Generic[ConnectionNode]):
node: ConnectionNode
@AndrewIngram
AndrewIngram / facets.py
Last active May 28, 2021 12:47
Filter Count Mixin for Django's admin
from django.contrib import admin
from django.db.models import Count, F
class FilterCountMixin(admin.SimpleListFilter):
def __init__(self, request, params, model, model_admin):
# Store the request so we can reapply all the filters properly later
self.request = request
super().__init__(request, params, model, model_admin)
def get_filter_counts_parameter_name(self):
@AndrewIngram
AndrewIngram / utils.py
Last active December 12, 2020 09:10
Django one-off on_save callbacks
from django.db import transaction
from django.db import models
from django.db.models.signals import post_save
def on_save(obj: models.Model, callback, on_commit=False):
"""
Attaches a one-off callback that's triggered after a Django model instance is
saved. Optionally makes use of transaction.on_commit to defer until any wrapping
transaction has also committed.
@AndrewIngram
AndrewIngram / PostDetail.jsx
Last active October 23, 2022 01:56
Updated Next.js with Relay (still using getInitialProps)
import React from "react";
import { graphql } from "react-relay";
import createPageContainer from "../relay/createPageContainer";
import PostLayout from "../components/PostLayout";
import Header from "../components/Header";
import Shell from "../layouts/Shell";
@AndrewIngram
AndrewIngram / schema.graphql
Last active January 7, 2021 11:05
Derived Dribbble Scema
# Not everything is defined here, just what was mentioned in the article
type User {
id: ID!
name: String!
avatar: Image!
isPro: Boolean!
viewerHasFollowed: Boolean!
shots(exclude: ID): ShotConnection!
}
@AndrewIngram
AndrewIngram / pagination.py
Last active April 13, 2021 10:35
Proper cursors with Graphene + Django. Graphene-Django's stock connections use limit/offset logic under the hood, making the whole cursor-based connection modelling kinda pointless.
import datetime
import operator
from base64 import b64decode as _unbase64
from base64 import b64encode as _base64
from functools import reduce
from django.db.models import Q
from graphene import relay
from graphql_relay.connection import connectiontypes
@AndrewIngram
AndrewIngram / Homepage.tsx
Created December 31, 2019 00:25
Next.js serverless dynamic routing with centralised routes
// src/pages/Homepage.tsx
import React from "react";
export default function Homepage() {
return <div>Homepage/div>;
}
@AndrewIngram
AndrewIngram / SketchSystems.spec
Created March 8, 2019 16:35
My Awesome Sketch
My Awesome Sketch
First State
some event -> Second State
Second State