This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { poll } = proxyActivities<typeof activities>({ | |
startToCloseTimeout: '10m', | |
retryPolicy: { | |
initialInterval: '1h', | |
backoffCoefficient: 1 | |
} | |
}); | |
export const stopPolling = defineSignal('stopPolling'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { ... } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: () => ({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async update(id, newDoc) { | |
try { | |
await this.db.update(id, newDoc) | |
this.cache.delete(this.cacheKey(id)) | |
} catch (error) { | |
this.didEncounterError(error) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cacheKey(id) { | |
return `foo-${this.db.connectionURI}-${id}` | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
didEncounterError(error) { | |
throw error | |
} |
NewerOlder