Skip to content

Instantly share code, notes, and snippets.

GraphQL

We use GraphQL Code Generator with the client preset to make it simple to write queries, mutations, and fragments in a way which is typesafe and extensible. This article is a good place to start if you're new to GraphQL and React, or are more familiar with the old way of using plugins to generate separate React hooks for each query.

To encourage consistancy in what and how we display our oft complex data models, we provide a handful of global queries. However, if you do need a query or mutation that is only going to be used in a single component, it's advisable to use graphql inline with useQuery, rather than cluttering up the global generated queries. See usage below.

For the global scope - to ensure we're not loading more data than is nessesary - we build up our queries as such

@andflett
andflett / docgen.js
Created June 7, 2024 11:12
docgen.js
// We generate a JSON file containing all components' metadata from comments in
// JSDoc format within the source code itself, and publish this (versioned)
// along side the design system for use in our docs site.
var fs = require("fs");
// eslint-disable-next-line no-unused-vars
const colors = require("colors");
var read = require("fs-readdir-recursive");
const docgen = require("react-docgen-typescript");
@andflett
andflett / EAPI.class.rb
Created July 27, 2016 17:00
Erply API class with added Bulk requests
require 'json'
require 'net/http'
require 'uri'
class MissingArgumentsException < Exception
end
class VerifyUserException < Exception
end
class EAPI
/**
* Mixcloud oEmbed discovery
*/
SirTrevor.Blocks.Mixcloud = (function(){
return SirTrevor.Block.extend({
type: 'Mixcloud',
title: function() { return 'Mixcloud' },
/**
* Soundcloud oEmbed discovery
*/
SirTrevor.Blocks.Soundcloud = (function(){
return SirTrevor.Block.extend({
type: 'Soundcloud',
title: function() { return 'Soundcloud' },
@andflett
andflett / timezoneoverlap.rb
Last active August 29, 2015 14:04
Time zone overlap
host = user = User.new
# Store in real datetime models with timezone data, convert to UTC, and base overlap on current week
# to mitigate the risk of leap years, timezone shifts (summer time) etc.
host.time_zone = "Europe/London"
host.availability = [
ActiveSupport::TimeZone.new(host.time_zone).parse('Wednesday 8am').utc..ActiveSupport::TimeZone.new(host.time_zone).parse('Wednesday 6pm').utc,
ActiveSupport::TimeZone.new(host.time_zone).parse('Friday 8am').utc..ActiveSupport::TimeZone.new(host.time_zone).parse('Friday 6pm').utc,
]
@andflett
andflett / is_json.rb
Created October 4, 2012 15:15
Is JSON for Sir Trevor
require 'json'
class String
def is_json?
begin
!!JSON.parse(self)
rescue
false
end
end
@andflett
andflett / sir-trevor-active-admin.css
Created October 4, 2012 12:36
Make Sir Trevor more presentable in ActiveAdmin
body {
margin: 0;
line-height: 150%;
font-size: 72%;
}
form .sir-trevor {
width: 860px;
margin: 30px auto;
background: #ffffff;
@andflett
andflett / cartography.rb
Created June 23, 2012 00:52
Convert OSGB36 Eastings/Northings Coords to WGS84 Latitude/Longitude
module Cartography
extend self
# Takes OSGB36 Easting/Northing coords
# and returns WGS84 Latitude and Longitude
def en_to_ll(easting, northing)
@OSGB36_ll = to_OSGB36(easting, northing)
to_WGS84(@OSGB36_ll[:latitude], @OSGB36_ll[:longitude])
end
@andflett
andflett / bookmarks.rake
Created May 8, 2012 22:26
Rake task to import bookmarks and tags (parent folders) from Netscape Bookmark formatted files.
require 'nokogiri'
require 'open-uri'
namespace :bookmarks do
desc '-- Import bookmarks in Netscape Bookmark format'
task :import => :environment do
doc = Nokogiri::HTML(open(File.join(Rails.root, "lib/dump/bookmarks_5_8_12.html")))
# Ensure we're dealing with the correct format