Skip to content

Instantly share code, notes, and snippets.

View jonnysamps's full-sized avatar

Jonathan Samples jonnysamps

View GitHub Profile
@jonnysamps
jonnysamps / watchQuery-refetch.ts
Last active February 25, 2021 16:17
Apollo Client Best Practices - 6
const query = gql`...`;
const watchQuery = apollo.watchQuery({ query });
watchQuery.valueChanges
.pipe(
map(result => result.data)
)
.subscribe(() => {
// ...
});
@jonnysamps
jonnysamps / watchQuery-pollInterval.ts
Last active February 25, 2021 15:29
Apollo Client Best Practices - 5
const query = gql`...`;
apollo.watchQuery({
query,
pollInterval: REFRESH_INTERVAL
}).valueChanges
.pipe(
map(result => result.data)
)
.subscribe(() => {
// ...
@jonnysamps
jonnysamps / watchQuery-poller-pollInterval.ts
Last active February 25, 2021 00:08
Apollo Client Best Practices
const query = gql`...`;
timer(0, REFRESH_INTERVAL)
.pipe(
mergeMap(() => apollo.watchQuery({ query }).valueChanges),
map(result => result.data)
)
.subscribe(() => {
// ...
});
@jonnysamps
jonnysamps / .ts
Last active February 25, 2021 00:03
Apollo Client Best Practices - 3
// Fires immediately and then on an interval
timer(0, REFRESH_INTERVAL)
.pipe(
mergeMap(() => apollo.query({ query: gql`...` })),
map(result => result.data)
)
.subscribe(() => {
// fires once per http request
});
@jonnysamps
jonnysamps / .ts
Last active February 24, 2021 23:56
Apollo Client Best Practices - 2
export class WidgetService {
constructor(
private auth: AuthService,
private apollo: Apollo,
) {}
// 1. We still have a memoized member variable
// but this time it is an Observable (not a subject)
// and it gets created from piplining from our
@jonnysamps
jonnysamps / .ts
Last active February 24, 2021 23:47
Apollo Client Best Practices - 1
class WidgetService {
constructor(private auth: AuthService, private apollo: Apollo){}
// 1. BAD PRACTICE: keeps a separate member variable as the
// subject that will get exposed to users
// of this class. This creates opportunity to simply
// call `next` whenever it is convenient instead of
// pipelining from the _source of truth_ observable to
// create the intended behavior. This creates
// misdirection away from the source of truth