Skip to content

Commit 6d2a9bf

Browse files
Fix caching (#413)
* try to fix caching test (?) * also fix typo in test * cache tweeks, better test, trying to find cause of error * fix another query * version number 0.2.60
1 parent 5f46bd3 commit 6d2a9bf

File tree

6 files changed

+88
-40
lines changed

6 files changed

+88
-40
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@daostack/client",
3-
"version": "0.2.59",
3+
"version": "0.2.60",
44
"description": "",
55
"keywords": [],
66
"main": "dist/lib/index.js",
@@ -102,8 +102,8 @@
102102
"u": "^0.1.0"
103103
},
104104
"dependencies": {
105-
"apollo-cache-inmemory": "^1.6.2",
106-
"apollo-client": "^2.6.3",
105+
"apollo-cache-inmemory": "^1.6.5",
106+
"apollo-client": "^2.6.8",
107107
"apollo-client-ws": "^2.5.0",
108108
"apollo-link-error": "^1.1.12",
109109
"apollo-link-http": "^1.5.15",

src/graphnode.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,15 @@ export function createApolloClient(options: {
9898
const cache = new InMemoryCache({
9999
cacheRedirects: {
100100
Query: {
101-
// competition: (_, args, { getCacheKey }) => {
102-
// return getCacheKey({ __typename: 'CompetitionProposal', id: args.id })
103-
// },
104101
competitionProposal: (_, args, { getCacheKey }) => {
105102
return getCacheKey({ __typename: 'CompetitionProposal', id: args.id })
106103
},
107104
competitionSuggestion: (_, args, { getCacheKey }) => {
108105
return getCacheKey({ __typename: 'CompetitionSuggestion', id: args.id })
109106
},
107+
// suggestion: (_, args, { getCacheKey }) => {
108+
// return getCacheKey({ __typename: 'CompetitionSuggestion', id: args.id })
109+
// },
110110
competitionVote: (_, args, { getCacheKey }) => {
111111
return getCacheKey({ __typename: 'CompetitionVote', id: args.id })
112112
},
@@ -134,8 +134,7 @@ export function createApolloClient(options: {
134134
switch (object.__typename) {
135135
case 'ProposalVote': return undefined
136136
case 'ProposalStake': return undefined
137-
case 'CompetitionSuggestion': return undefined
138-
case 'CompetitionVote': return undefined
137+
// case 'CompetitionVote': return undefined
139138
default: return defaultDataIdFromObject(object) // fall back to default handling
140139
}
141140
}

src/scheme.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ export class Scheme extends SchemeBase {
303303
}
304304

305305
public state(apolloQueryOptions: IApolloQueryOptions = {}): Observable<ISchemeState> {
306-
const query = gql`query SchemeState
306+
const query = gql`query SchemeStateById
307307
{
308308
controllerScheme (id: "${this.id}") {
309309
...SchemeFields

src/schemes/competition.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export interface ICompetitionVoteState {
8383

8484
export class CompetitionScheme extends SchemeBase {
8585
public state(apolloQueryOptions: IApolloQueryOptions = {}): Observable<ISchemeState> {
86-
const query = gql`query SchemeState
86+
const query = gql`query SchemeStateById
8787
{
8888
controllerScheme (id: "${this.id}") {
8989
...SchemeFields
@@ -486,6 +486,9 @@ export class Competition { // extends Proposal {
486486
const observable = proposal.state().pipe(
487487
first(),
488488
concatMap((competitionState: IProposalState) => {
489+
if (competitionState === null) {
490+
throw Error(`Cannot vote on this suggestion, because the competition with id ${this.id} could not be foound`)
491+
}
489492
const scheme = new CompetitionScheme(competitionState.scheme, this.context)
490493
return scheme.voteSuggestion({ suggestionId })
491494
})
@@ -541,7 +544,7 @@ export class CompetitionSuggestion implements IStateful<ICompetitionSuggestionSt
541544
id
542545
suggestionId
543546
proposal {
544-
id
547+
id
545548
}
546549
descriptionHash
547550
title
@@ -689,7 +692,7 @@ constructor(
689692
}
690693

691694
public state(apolloQueryOptions: IApolloQueryOptions = {}): Observable < ICompetitionSuggestionState > {
692-
const query = gql`query SchemeState
695+
const query = gql`query CompetitionSuggestionById
693696
{
694697
competitionSuggestion (id: "${this.id}") {
695698
...CompetitionSuggestionFields
@@ -783,6 +786,7 @@ export class CompetitionVote implements IStateful<ICompetitionVoteState> {
783786
query = gql`query CompetitionVoteSearchBySuggestion
784787
{
785788
competitionSuggestion (id: "${options.where.suggestion}") {
789+
id
786790
votes ${createGraphQlQuery({ where: { ...options.where, suggestion: undefined}})} {
787791
...CompetitionVoteFields
788792
}

test/proposal-competition.spec.ts

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -710,41 +710,90 @@ describe('Competition Proposal', () => {
710710
arc.setAccount(address0)
711711
})
712712

713-
// TODO!! unskip this test. It passes locally but fails often (but not always) on travis :-(
714-
it.skip('pre-fetching competition.suggestions works', async () => {
713+
it('pre-fetching competition.suggestions works', async () => {
715714
// find a proposal in a scheme that has > 1 votes
716715
const competition = await createCompetition()
717716
// check if the competition has indeed some suggestions
718717

719718
const suggestions = await competition.suggestions().pipe(first()).toPromise()
720719
expect(suggestions.length).toBeGreaterThan(0)
721720

722-
// now we have our objects, reset the cache
721+
// now we have our objects, reset the cache
723722
await (arc.apolloClient as any).cache.reset()
724723
expect((arc.apolloClient as any).cache.data.data).toEqual({})
725724

726-
// // construct our superquery that will fill the cache
725+
// // construct our superquery that will fill the cache
727726
const query = gql`query {
728-
proposals (where: { id: "${competition.id}"}) {
729-
competition {
727+
proposals (where: { id: "${competition.id}"}) {
728+
...ProposalFields
730729
id
731-
suggestions { ...CompetitionSuggestionFields }
730+
competition {
731+
id
732+
suggestions {
733+
...CompetitionSuggestionFields
734+
}
735+
}
732736
}
733-
...ProposalFields
734737
}
735-
}
736-
${Proposal.fragments.ProposalFields}
737-
${Scheme.fragments.SchemeFields}
738-
${CompetitionSuggestion.fragments.CompetitionSuggestionFields}
739-
`
738+
${Proposal.fragments.ProposalFields}
739+
${Scheme.fragments.SchemeFields}
740+
${CompetitionSuggestion.fragments.CompetitionSuggestionFields}
741+
`
740742

741743
await arc.sendQuery(query)
742744

743-
// now see if we can get our informatino directly from the cache
744-
const cachedSugestions = await competition.suggestions({}, { fetchPolicy: 'cache-only'})
745-
.pipe(first()).toPromise()
746-
expect(cachedSugestions.map((v: CompetitionSuggestion) => v.id))
747-
.toEqual(suggestions.map((v: CompetitionSuggestion) => v.id))
745+
// now see if we can get our informatino directly from the cache
746+
const cachedSuggestions = await competition.suggestions({}, { fetchPolicy: 'cache-only'})
747+
.pipe(first()).toPromise()
748+
expect(cachedSuggestions.map((v: CompetitionSuggestion) => v.id))
749+
.toEqual(suggestions.map((v: CompetitionSuggestion) => v.id))
750+
751+
const cachedSuggestionState = await cachedSuggestions[0]
752+
.state({ fetchPolicy: 'cache-only'}).pipe(first()).toPromise()
753+
expect(cachedSuggestionState.id).toEqual(cachedSuggestions[0].id)
754+
755+
})
756+
757+
it('pre-fetching competition.suggestions works also without resetting the cache', async () => {
758+
// find a proposal in a scheme that has > 1 votes
759+
const competition = await createCompetition()
760+
// check if the competition has indeed some suggestions
761+
762+
const suggestions = await competition.suggestions().pipe(first()).toPromise()
763+
expect(suggestions.length).toBeGreaterThan(0)
764+
765+
// add some exiting data to the cache to seeif we can mess things up
766+
await arc.proposal(competition.id).state().pipe(first()).toPromise()
767+
768+
// construct our superquery that will fill the cache
769+
const query = gql`query {
770+
proposals (where: { id: "${competition.id}"}) {
771+
# id
772+
...ProposalFields
773+
competition {
774+
id
775+
suggestions {
776+
...CompetitionSuggestionFields
777+
}
778+
}
779+
}
780+
}
781+
${Proposal.fragments.ProposalFields}
782+
${Scheme.fragments.SchemeFields}
783+
${CompetitionSuggestion.fragments.CompetitionSuggestionFields}
784+
`
785+
786+
await arc.sendQuery(query)
787+
788+
// now see if we can get our informatino directly from the cache
789+
const cachedSuggestions = await competition.suggestions({}, { fetchPolicy: 'cache-only'})
790+
.pipe(first()).toPromise()
791+
expect(cachedSuggestions.map((v: CompetitionSuggestion) => v.id))
792+
.toEqual(suggestions.map((v: CompetitionSuggestion) => v.id))
793+
794+
const cachedSuggestionState = await cachedSuggestions[0]
795+
.state({ fetchPolicy: 'cache-only'}).pipe(first()).toPromise()
796+
expect(cachedSuggestionState.id).toEqual(cachedSuggestions[0].id)
748797

749798
})
750799

@@ -771,7 +820,7 @@ describe('Competition Proposal', () => {
771820
// // construct our superquery that will fill the cache
772821
const query = gql`query
773822
{
774-
competitionSuggestion(id: '${suggestion1.id}') {
823+
competitionSuggestion(id: "${suggestion1.id}") {
775824
id
776825
votes {
777826
...CompetitionVoteFields
@@ -784,19 +833,15 @@ describe('Competition Proposal', () => {
784833
${CompetitionVote.fragments.CompetitionVoteFields}
785834
`
786835

787-
let subscribed = false
788-
const results: any[] = []
789-
arc.getObservable(query, { subscribe: true, fetchPolicy: 'no-cache'}).subscribe((x: any) => {
790-
subscribed = true
791-
results.push(x)
792-
})
793-
await waitUntilTrue(() => subscribed)
836+
await arc.sendQuery(query)
794837

795-
// now see if we can get our information directly from the cache
796838
const cachedVotes = await suggestion1.votes({}, { fetchPolicy: 'cache-only'})
797839
.pipe(first()).toPromise()
798840
expect(cachedVotes.map((v: CompetitionVote) => v.id))
799841
.toEqual(votes.map((v: CompetitionVote) => v.id))
800842

843+
const cachedVoteState = await cachedVotes[0].state({ fetchPolicy: 'cache-only'})
844+
.pipe(first()).toPromise()
845+
expect(cachedVoteState.id).toEqual(cachedVotes[0].id)
801846
})
802847
})

0 commit comments

Comments
 (0)