Last active
July 10, 2019 20:35
-
-
Save betweenbrain/82491cd510acc5f05a6eed1d9d6ee743 to your computer and use it in GitHub Desktop.
Vue Apollo query from within Vue router route guard
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
router.beforeEach((to, from, next) => { | |
function delay(time) { | |
return new Promise(resolve => { | |
setTimeout(function() { | |
resolve(); | |
}, time); | |
}); | |
} | |
setTimeout(async function() { | |
const app = router.app.$apolloProvider.defaultClient; | |
while (!app) await delay(10); | |
app | |
.query({ | |
query: gql` | |
{ | |
posts(first: 100) { | |
nodes { | |
title | |
} | |
} | |
} | |
` | |
}) | |
.then(data => { | |
console.log('<== Pre-route GraphQL request completed ==>'); | |
next(); | |
}) | |
.catch(err => { | |
console.log(err); | |
}); | |
}, 1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment