View pollRace.ts
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'); |
View FooDataSource-structure.js
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) { ... } |
View resolvers.js
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) | |
} |
View dataSources.js
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: () => ({ |
View MyFooDB.js
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 | |
}) |
View FooDataSource-update.js
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) | |
} | |
} |
View FooDataSource-get.js
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 }) |
View FooDataSource-cacheKey.js
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}` | |
} |
View FooDataSource-didEncounterError.js
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 | |
} |
View FooDataSource-initialize.js
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 { InMemoryLRUCache } from 'apollo-server-caching' | |
... | |
initialize({ context, cache } = {}) { | |
this.context = context | |
this.cache = cache || new InMemoryLRUCache() | |
} |
NewerOlder