Skip to content

Instantly share code, notes, and snippets.

View lorensr's full-sized avatar

Loren ☺️ lorensr

View GitHub Profile
const addItem = defineSignal('addItem')
const checkout = defineSignal('checkout')
function cart(userId) {
const items = []
setHandler(addItem, (item) => items.push(item))
setHandler(checkout, () => {
const userInfo = await getUser(userId) // activity that gets a handle to the User workflow with Workflow Id = userId and does a 'getUserInfo' Query
const { poll } = proxyActivities<typeof activities>({
startToCloseTimeout: '10m',
retryPolicy: {
initialInterval: '1h',
backoffCoefficient: 1
}
});
export const stopPolling = defineSignal('stopPolling');
import { DataSource } from 'apollo-datasource'
import { InMemoryLRUCache } from 'apollo-server-caching'
import DataLoader from 'dataloader'
class FooDataSource extends DataSource {
constructor(dbClient) { ... }
initialize({ context, cache } = {}) { ... }
didEncounterError(error) { ... }
const resolvers = {
Query: {
getFoo: (_, { id }, context) =>
context.dataSources.myFoos.get(id, { ttlInSeconds: 60 })
},
Mutation: {
updateFoo: async (_, { id, fields }, context) => {
if (context.isAdmin) {
context.dataSources.myFoos.updateFields(id, fields)
}
import FooClient from 'imaginary-foo-library'
import MyFooDB from './MyFooDB'
const fooClient = new FooClient({ uri: 'https://foo.graphql.guide:9001' })
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({
import FooDataSource from './FooDataSource'
import { reportError } from './utils'
export default class MyFooDB extends FooDataSource {
async updateFields(id, fields) {
const doc = await this.get(id)
return this.update(id, {
...doc,
...fields
})
async update(id, newDoc) {
try {
await this.db.update(id, newDoc)
this.cache.delete(this.cacheKey(id))
} catch (error) {
this.didEncounterError(error)
}
}
async get(id, { ttlInSeconds } = {}) {
const cacheDoc = await cache.get(this.cacheKey(id))
if (cacheDoc) {
return JSON.parse(cacheDoc)
}
const doc = await this.loader.load(id)
if (ttlInSeconds) {
cache.set(this.cacheKey(id), JSON.stringify(doc), { ttl: ttlInSeconds })
cacheKey(id) {
return `foo-${this.db.connectionURI}-${id}`
}
didEncounterError(error) {
throw error
}