Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
perf: add deault fetch policy
Browse files Browse the repository at this point in the history
  • Loading branch information
Arenukvern committed Mar 13, 2021
1 parent 341e120 commit 65e839f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
16 changes: 13 additions & 3 deletions examples/vue-client/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<HelloWorld msg="Hello Vue 3 + TypeScript + Vite" />
<button @click="addTodo">Add to original state</button>
<button @click="addTodoModel">Add via standalone model</button>
<button @click="find">find</button>
<button @click="findMars">findMars</button>
<div>
<span>List Original</span>
<div v-for="todo in todoList" :key="todo.id">
Expand Down Expand Up @@ -36,17 +38,23 @@
const todoState = useTodoState()
const todoDubplicateState = useTodoState()
const counter = ref(1)
const find = () => todoState.find()
const findMars = () =>
todoState.find({ filter: { title: { contains: 'Mars' } } })
onMounted(async () => {
const isReplicating = await todoModel.startReplication()
console.log({ isReplicating })
todoState.find()
})
const addTodo = async (arg?: { standalone?: boolean }) => {
const addTodo = async (arg?: {
standalone?: boolean
title?: string
}) => {
const todo: CreateTodoInput = {
_version: 1,
_lastUpdatedAt: Date.now().toString(),
title: `New todo ${counter.value}`,
title: `${arg?.title ?? 'New todo'} ${counter.value}`,
}
counter.value++
if (arg?.standalone) {
Expand All @@ -59,7 +67,7 @@
})
}
}
const addTodoModel = () => addTodo({ standalone: true })
const addTodoModel = () => addTodo({ standalone: true, title: 'Mars' })
const removeTodo = async (todo: Todo) => {
await todoState.remove({
input: {
Expand All @@ -72,6 +80,8 @@
addTodo,
addTodoModel,
removeTodo,
find,
findMars,
todoList: todoState.list,
todoDuplicateList: todoDubplicateState.list,
}
Expand Down
12 changes: 11 additions & 1 deletion packages/cactus-sync-client/lib/src/abstract/ApolloRunner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ApolloClient,
ApolloClientOptions,
FetchPolicy,
FetchResult,
ObservableSubscription,
Observer,
Expand Down Expand Up @@ -33,11 +34,13 @@ interface ApolloRunnerSubscribes extends ModelNameReplicationI {
interface ApolloRunnerI<TCacheShape> {
apollo: ApolloClient<TCacheShape>
schema: GraphQLSchema
defaultFetchPolicy?: Maybe<FetchPolicy>
}
interface ApolloRunnerInitI<TCacheShape> {
db: Dexie
options: ApolloClientOptions<TCacheShape>
schema: GraphQLSchema
defaultFetchPolicy?: Maybe<FetchPolicy>
}

export type ApolloSubscription<TModel = any> = {
Expand All @@ -62,9 +65,15 @@ export type ApolloSubscription<TModel = any> = {
export class ApolloRunner<TCacheShape> {
schema: GraphQLSchema
apollo: ApolloClient<TCacheShape>
constructor({ schema, apollo }: ApolloRunnerI<TCacheShape>) {
defaultFetchPolicy: FetchPolicy
constructor({
schema,
apollo,
defaultFetchPolicy,
}: ApolloRunnerI<TCacheShape>) {
this.apollo = apollo
this.schema = schema
this.defaultFetchPolicy = defaultFetchPolicy ?? 'network-only'
}
static init<TCacheShape>({
options,
Expand Down Expand Up @@ -93,6 +102,7 @@ export class ApolloRunner<TCacheShape> {
return await this.apollo.query<TResult, TVariables>({
query: parse(query),
variables: variableValues,
fetchPolicy: this.defaultFetchPolicy,
})
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/cactus-sync-client/lib/src/abstract/CactusSync.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
ApolloClientOptions,
ApolloQueryResult,
FetchPolicy,
FetchResult,
OperationVariables,
} from '@apollo/client/core'
Expand All @@ -25,6 +26,7 @@ interface CactusSyncI {
interface CactusSyncInitI<TCacheShape> extends CactusSyncI {
schema: GraphQLSchema
apolloOptions: ApolloClientOptions<TCacheShape>
defaultFetchPolicy?: Maybe<FetchPolicy>
}
/**
* Function type to run after CactusSync(Dexie) change
Expand Down Expand Up @@ -79,6 +81,7 @@ export class CactusSync<TCacheShape = any> extends Dexie {
dbVersion,
apolloOptions,
schema,
defaultFetchPolicy,
}: CactusSyncInitI<TCacheShape>) {
super(dbName ?? 'cactusSyncDb', dexieOptions ?? undefined)
const db = this
Expand All @@ -89,6 +92,7 @@ export class CactusSync<TCacheShape = any> extends Dexie {
db: db,
options: apolloOptions,
schema,
defaultFetchPolicy,
})
}
/**
Expand Down

0 comments on commit 65e839f

Please sign in to comment.