Skip to content

Commit

Permalink
Fabo/force network loading for refetch queries (#3111)
Browse files Browse the repository at this point in the history
* some fixes

* fixed fetching and caching issues

* fixed tests

* better failing of e2e tests

* renaming
  • Loading branch information
faboweb authored and colw committed Nov 11, 2019
1 parent 9eb9187 commit 557399a
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 26 deletions.
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export default {

graphqlHost: graphql,

e2e: process.env.VUE_APP_E2E
e2e: process.env.VUE_APP_E2E || false
}
28 changes: 28 additions & 0 deletions src/ActionModal/components/DelegationModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:transaction-data="transactionData"
:notify-message="notifyMessage"
@close="clear"
@txIncluded="onSuccess"
>
<TmFormGroup class="action-modal-form-group">
<div class="form-message notice">
Expand Down Expand Up @@ -143,6 +144,7 @@ import ActionModal from "./ActionModal"
import transaction from "../utils/transactionTypes"
import { toMicroDenom } from "src/scripts/common"
import { formatBech32 } from "src/filters"
import { UserTransactionAdded } from "src/gql"
export default {
name: `delegation-modal`,
Expand Down Expand Up @@ -253,6 +255,10 @@ export default {
return this.fromOptions[this.selectedIndex].maximum
}
},
mounted() {
this.$apollo.queries.balance.refetch()
this.$apollo.queries.delegations.refetch()
},
methods: {
open(options) {
if (options && options.redelegation && this.fromOptions.length > 1) {
Expand All @@ -279,6 +285,9 @@ export default {
},
enterPressed() {
this.$refs.actionModal.validateChangeStep()
},
onSuccess(event) {
this.$emit(`success`, event)
}
},
validations() {
Expand Down Expand Up @@ -366,6 +375,25 @@ export default {
return data.network.stakingDenom
}
}
},
$subscribe: {
userTransactionAdded: {
variables() {
return {
networkId: this.network,
address: this.address
}
},
skip() {
return !this.address
},
query: UserTransactionAdded,
result({ data }) {
if (data.userTransactionAdded.success) {
this.$apollo.queries.delegations.refetch()
}
}
}
}
}
</script>
4 changes: 4 additions & 0 deletions src/ActionModal/components/SendModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
:transaction-data="transactionData"
:notify-message="notifyMessage"
@close="clear"
@txIncluded="onSuccess"
>
<TmFormGroup
:error="$v.address.$error && $v.address.$invalid"
Expand Down Expand Up @@ -194,6 +195,9 @@ export default {
open() {
this.$refs.actionModal.open()
},
onSuccess(event) {
this.$emit(`success`, event)
},
validateForm() {
this.$v.$touch()
Expand Down
4 changes: 4 additions & 0 deletions src/ActionModal/components/UndelegationModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
:transaction-data="transactionData"
:notify-message="notifyMessage"
@close="clear"
@txIncluded="onSuccess"
>
<TmFormGroup class="action-modal-form-group">
<div class="form-message notice">
Expand Down Expand Up @@ -174,6 +175,9 @@ export default {
switchToRedelegation() {
this.$refs.actionModal.close()
this.$emit("switchToRedelegation")
},
onSuccess(event) {
this.$emit(`success`, event)
}
},
apollo: {
Expand Down
9 changes: 2 additions & 7 deletions src/components/common/TmBalance.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
</template>
<script>
import { shortDecimals } from "scripts/num"
import refetchNetworkOnly from "scripts/refetch-network-only"
import { noBlanks } from "src/filters"
import TmBtn from "common/TmBtn"
import SendModal from "src/ActionModal/components/SendModal"
Expand Down Expand Up @@ -74,12 +75,6 @@ export default {
return this.overview.totalRewards > 0
}
},
mounted() {
// We need to account or a small delay in the node processing of balances.
// TODO: Find a cleaner solution.
const refetch = () => this.$apollo.queries.overview.refetch()
setTimeout(refetch, 2000)
},
methods: {
onWithdrawal() {
this.$refs.ModalWithdrawRewards.open()
Expand Down Expand Up @@ -150,7 +145,7 @@ export default {
query: UserTransactionAdded,
result() {
// query if successful or not as even an unsuccessful tx costs fees
this.$apollo.queries.overview.refetch()
refetchNetworkOnly(this.$apollo.query.overview)
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/governance/PageProposal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export default {
proposalId: this.proposal.id,
address: this.address
})
this.$store.commit("invalidateCache", [`overview`])
},
onDeposit() {
this.$refs.modalDeposit.open()
Expand All @@ -248,6 +249,7 @@ export default {
this.$apollo.queries.proposal.refetch({
id: this.proposal.id
})
this.$store.commit("invalidateCache", [`overview`])
}
},
apollo: {
Expand Down
1 change: 1 addition & 0 deletions src/components/governance/PageProposals.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default {
},
afterPropose() {
this.$apollo.queries.proposals.refetch()
this.$store.commit("invalidateCache", [`overview`])
}
},
apollo: {
Expand Down
3 changes: 2 additions & 1 deletion src/components/staking/DelegationsOverview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { mapGetters } from "vuex"
import TmDataMsg from "common/TmDataMsg"
import TableValidators from "staking/TableValidators"
import { DelegationsForDelegator, UserTransactionAdded } from "src/gql"
import refetchNetworkOnly from "scripts/refetch-network-only"
export default {
name: `delegations-overview`,
Expand Down Expand Up @@ -72,7 +73,7 @@ export default {
query: UserTransactionAdded,
result({ data }) {
if (data.userTransactionAdded.success) {
this.$apollo.queries.delegations.refetch()
refetchNetworkOnly(this.$apollo.query.delegations)
}
}
}
Expand Down
21 changes: 17 additions & 4 deletions src/components/staking/PageValidator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,16 @@
</li>
</ul>

<DelegationModal ref="delegationModal" :target-validator="validator" />
<DelegationModal
ref="delegationModal"
:target-validator="validator"
@success="clearDelegationCache"
/>
<UndelegationModal
ref="undelegationModal"
:source-validator="validator"
@switchToRedelegation="onDelegation({ redelegation: true })"
@success="clearUnelegationCache"
/>
</template>
<template v-else>
Expand Down Expand Up @@ -212,6 +217,10 @@ export default {
...mapGetters([`network`]),
...mapGetters({ userAddress: `address` })
},
mounted() {
this.$apollo.queries.rewards.refetch()
this.$apollo.queries.delegation.refetch()
},
methods: {
shortDecimals,
atoms,
Expand All @@ -222,6 +231,12 @@ export default {
},
onUndelegation() {
this.$refs.undelegationModal.open()
},
clearDelegationCache() {
this.$store.commit("invalidateCache", [`overview`, `delegations`]) // TODO use more finegrained query string (network and address)
},
clearUndelegationCache() {
this.$store.commit("invalidateCache", [`overview`, `delegations`]) // TODO use more finegrained query string (network and address)
}
},
apollo: {
Expand Down Expand Up @@ -285,9 +300,7 @@ export default {
}
},
update: result => {
const r = result.rewards.length > 0 ? result.rewards[0] : { amount: 0 }
console.log(r)
return r
return result.rewards.length > 0 ? result.rewards[0] : { amount: 0 }
}
},
validator: {
Expand Down
3 changes: 2 additions & 1 deletion src/components/staking/Undelegations.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<script>
import { mapGetters } from "vuex"
import TableUndelegations from "staking/TableUndelegations"
import refetchNetworkOnly from "scripts/refetch-network-only"
import { UndelegationsForDelegator, UserTransactionAdded } from "src/gql"
export default {
Expand Down Expand Up @@ -51,7 +52,7 @@ export default {
query: UserTransactionAdded,
result({ data }) {
if (data.userTransactionAdded.success) {
this.$apollo.queries.undelegations.refetch()
refetchNetworkOnly(this.$apollo.query.undelegations)
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/scripts/refetch-network-only.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// when refetching always force to load from the network
export default apolloQuery => {
apolloQuery.setOptions({
fetchPolicy: "network-only"
})
apolloQuery.refetch()
apolloQuery.setOptions({
fetchPolicy: "cache-and-network"
})
}
19 changes: 16 additions & 3 deletions src/vuex/modules/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const windowsWarning = `If you’re using Windows 10 (May 2019 update), signing
transactions with your Ledger Nano S will not work. Please use another
operating system, or version of Windows.`

export default () => {
export default ({ apollo }) => {
const USER_PREFERENCES_KEY = `lunie_user_preferences`

const state = {
Expand Down Expand Up @@ -63,8 +63,8 @@ export default () => {
setExperimentalMode(state) {
state.experimentalMode = true
},
setInsecureMode(state, insecureMode) {
state.insecureMode = insecureMode
setInsecureMode(state) {
state.insecureMode = true
},
addHistory(state, path) {
state.history.push(path)
Expand All @@ -80,6 +80,19 @@ export default () => {
},
setCurrrentModalOpen(state, modal) {
state.currrentModalOpen = modal
},

// TODO to own store module?
// clear the cache manually so we can force reloading of stale data in not mounted components
invalidateCache(state, queries) {
let rootQuery = apollo.cache.data.data.ROOT_QUERY
Object.keys(rootQuery)
.filter(query =>
queries.find(queryToClear => query.startsWith(queryToClear))
)
.forEach(query => {
delete rootQuery[query]
})
}
}

Expand Down
12 changes: 6 additions & 6 deletions tests/e2e/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ async function waitFor(check, iterations = 10, timeout = 1000) {
}
}

throw new Error("Condition was not meet in time")
console.error("Condition was not meet in time")
process.exit(2) // exiting here so the e2e tests actually fail. if not they pass
}
async function waitForText(
browser,
Expand All @@ -50,10 +51,7 @@ async function waitForText(
},
iterations,
timeout
).catch(err => {
console.log(err.message)
process.exit(2)
})
)
}

// performs some details actions and handles checking of the invoice step + signing
Expand All @@ -67,7 +65,7 @@ async function actionModalCheckout(
) {
// grab page we came from as we want to go to another page and come back
let sourcePage
browser.url(function(result) {
browser.url(function (result) {
sourcePage = result.value
})

Expand Down Expand Up @@ -140,12 +138,14 @@ async function actionModalCheckout(

// check if balance header updates as expected
// TODO find a way to know the rewards on an undelegation to know the final balance 100%
console.log("Wait for total balance to update")
await waitFor(async () => {
const approximatedBalanceAfter = balanceBefore - expectedTotalChange - fees
expect(
Math.abs(approximatedBalanceAfter - (await getBalance(browser)))
).to.be.lessThan(2) // acounting for rewards being withdrawn on an undelegation
})
console.log("Wait for liquid balance to update")
await waitFor(async () => {
const approximatedAvailableBalanceAfter =
availableTokensBefore - expectedAvailableTokensChange - fees
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe(`DelegationModal`, () => {
let wrapper
const localVue = createLocalVue()
localVue.use(Vuelidate)
localVue.directive("focus", () => {})
localVue.directive("focus", () => { })

const state = {
session: {
Expand All @@ -46,7 +46,13 @@ describe(`DelegationModal`, () => {
wrapper = shallowMount(DelegationModal, {
localVue,
mocks: {
$store: { getters, state }
$store: { getters, state },
$apollo: {
queries: {
balance: { refetch: () => { } },
delegations: { refetch: () => { } }
}
}
},
propsData: {
targetValidator: validators[0]
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/specs/components/staking/PageValidator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,16 @@ describe(`PageValidator`, () => {
queries: {
validator: {
loading: false,
error: false
error: false,
refetch: () => {}
},
rewards: {
loading: false,
error: false,
refetch: () => {}
},
delegation: {
refetch: () => {}
}
}
}
Expand Down

0 comments on commit 557399a

Please sign in to comment.