Skip to content

Instantly share code, notes, and snippets.

View mlg87's full-sized avatar
πŸ‡ΊπŸ‡¦
Π‘Π»Π°Π²Π° Π£ΠΊΡ€Π°Ρ—Π½Ρ–

Mason Goetz mlg87

πŸ‡ΊπŸ‡¦
Π‘Π»Π°Π²Π° Π£ΠΊΡ€Π°Ρ—Π½Ρ–
View GitHub Profile
@mlg87
mlg87 / express_postgress_knex.md
Created January 17, 2019 18:07 — forked from laurenfazah/express_postgress_knex.md
Cheat Sheet: Setting up Express with Postgres via Knex

Express & Postgres via Knex

Note: <example> is meant to denote text replaced by you (including brackets).

Setup

// global dependencies
npm install -g knex
async function doSomething(data) {
if(data.ok) {
return await db.insert(data)
}
if(typeof data === 'string') {
return new Alert(data)
}
if(data.type === 'result') {
@mlg87
mlg87 / example.ts
Created February 4, 2019 21:56
mock a static class method in jest
// ./SomeClass.ts
export default class SomeClass {
public static myMethod(params: any) {
... do something
}
}
// ./SomeComponent.tsx
@mlg87
mlg87 / findMatchingSimulator.js
Created July 16, 2019 22:28
Fixes a bug in `react-native/local-cli/runIOS/findMatchingSimulator.js`
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
'use strict';

Using Apollo's local cache for global state management with Typescript, codegen and remote resolvers

If your project is using the apollo-client to communicate with your GraphQL backend, there is no need to include redux or mobx for the global state management; you can store this state in the same Apollo client you are already using. This will be a contrived example, but this post will cover:

  • Setting up local queries and mutations to take advantage of the cache
  • Integrating the apollo-client you are already using with the local graphql
  • Setting up a GraphQL schema locally to use for typeDefs and codegen
  • Using @graphql-codegen to create custom hooks for your queries and mutations

Why did I want to do this?

@mlg87
mlg87 / src_Balloon_schema.js
Last active July 21, 2020 20:57
Using Apollo Client for global state management code samples - src_Balloon_schema.js
// src/Balloon/schema.js
const gql = require("graphql-tag");
module.exports = gql`
input BalloonInput {
color: String!
id: ID!
}
type Balloon {
@mlg87
mlg87 / src_Balloon_queries.ts
Created July 21, 2020 20:58
Using Apollo Client for global state management code samples - src_Balloon_queries.ts
// src/Balloon/queries.ts
import gql from "graphql-tag";
//
// ─── GQL TAGS ───────────────────────────────────────────────────────────────────
//
export const GetSelectedBalloonLocal = gql`
query GetSelectedBalloonLocal {
selectedBalloon @client {
@mlg87
mlg87 / src_Balloon_mutations.ts
Created July 21, 2020 21:00
Using Apollo Client for global state management code samples - src/Balloon/mutations.ts
// src/Balloon/mutations.ts
import { Resolver } from "apollo-client";
import gql from "graphql-tag";
//
// ─── GQL TAGS ───────────────────────────────────────────────────────────────────
//
export const SetSelectedBalloonLocal = gql`
mutation SetSelectedBalloonLocal($selectedBalloon: BalloonInput!) {
@mlg87
mlg87 / src_Balloon_defaults.ts
Created July 21, 2020 21:03
Using Apollo Client for global state management code samples - src/Balloon/defaults.ts
// src/Balloon/defaults.ts
import { IBalloon } from "../types/graphql"; // <-- we will auto-generate this file a bit later in the post
const defaultSelectedBalloon: IBalloon = {
color: "",
id: "",
};
export default {
selectedBalloon: { ...defaultSelectedBalloon, __typename: "Balloon" },
@mlg87
mlg87 / src_client.ts
Created July 21, 2020 21:05
Using Apollo Client for global state management code samples - src/client.ts
// src/client.ts
import { InMemoryCache } from 'apollo-cache-inmemory';
import balloonDefaultCacheValues from './Balloon/defaults';
import { setSelectedBalloonMutationResolver } from './Balloon/mutations';
import balloonSchema from './Balloon
const cache = new InMemoryCache();
export const client = new ApolloClient({