How to update headers of apolloProvider?
Please check out nativescript-vue app repo:
https://github.com/kaanguru/vue-apollo-login
I can not explain properly so please check out the app. I don't know how to update appolloClient headers.
App repo has it's own comments and directives. It's easy to install and see by your self.
Current Structure of code:
Post request submits the user's identifier and password credentials for authentication and gets token in login page.
Apollo needs to place the jwt token into an Authorization header.
Main.js: Start apollo client if there is JWT start with headers
Goto login if there is no JWT
Goto birds list if there is JWT
Login : get jwt from server and write it to local storage
- Go to birds list (does not show data because apollo initilised in main js)
import ApolloClient from 'apollo-boost'
import VueApollo from 'vue-apollo'
Vue.use(VueApollo)
const apolloClient = new ApolloClient({
uri: 'http://sebapi.com/graphql',
// HEADERS WORK FINE IF TOKEN WAS IN MAIN
// headers: {
// authorization: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiaWF0IjoxNTg2MzU2NzM2LCJleHAiOjE1ODg5NDg3MzZ9.wpyhPTWuqxrDgezDXJqIOaAIaocpM8Ehd3BhQUWKK5Q`,
// }
})
const apolloProvider = new VueApollo({
defaultClient: apolloClient,
})
LOGIN.VUE
.then(
(response) => {
const result = response.content.toJSON();
console.log("Result from Server: ", result);
const token = result.jwt;
// HOW TO ADD HEADERS TO APOLLOCLIENT this.$apollo.provider.defaultClient
// this.$apollo.provider.defaultClient({
// request: (operation) => {
// operation.setContext({
// headers: {
// authorization: `Bearer ${result.jwt}` ,
// },
// });
// },
// });
},
Thank you for your interest.
NOTE: Please comment for more details. sebapi.com backend is a strapi graphql server.
Related Docs:
