-
-
Save tomdavies/b88c68110333c5754f81f785ec0f270d to your computer and use it in GitHub Desktop.
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
// Generated (incorrect) file | |
import Vue from 'vue' | |
import VueApollo from 'vue-apollo' | |
import 'isomorphic-fetch' | |
import { createApolloClient, restartWebsockets } from 'vue-cli-plugin-apollo/graphql-client' | |
import jsCookie from 'js-cookie' | |
import cookie from 'cookie' | |
import { InMemoryCache } from 'apollo-cache-inmemory' | |
Vue.use(VueApollo) | |
export default (ctx, inject) => { | |
const providerOptions = { clients: {} } | |
const { app, beforeNuxtRender, req } = ctx | |
const AUTH_TOKEN_NAME = 'apollo-token' | |
const AUTH_TYPE = 'Bearer ' | |
// Config | |
// Create apollo client | |
var currentOptions = { | |
"httpEndpoint": "<ENDPOINT_URL>", | |
"cache": { | |
"optimistic": [], | |
"watches": [], | |
"typenameDocumentCache": {}, | |
"silenceBroadcast": false, | |
"config": { | |
"fragmentMatcher": { | |
"possibleTypesMap": { | |
"EntryInterface": [ | |
"Ads", | |
"ArticlesDefault", | |
"ArticlesRecipe", | |
"ArticlesInterview", | |
"Pages", | |
"Releases" | |
], | |
"CategoryInterface": [ | |
"PostsCategory" | |
], | |
"VolumeInterface": [ | |
"ImagesVolume", | |
"FilesVolume" | |
], | |
"TagInterface": [ | |
"PostsTags" | |
], | |
"ElementInterface": [ | |
"Ads", | |
"ArticlesDefault", | |
"ArticlesRecipe", | |
"ArticlesInterview", | |
"Pages", | |
"Releases" | |
], | |
"BodyUnion": [ | |
"BodyHeading", | |
"BodySubheading", | |
"BodyLede", | |
"BodyText", | |
"BodyTweet", | |
"BodyVideo", | |
"BodyGallery", | |
"BodyReleases", | |
"BodySpotifyEmbed", | |
"BodySoundcloudEmbed", | |
"BodyBreak" | |
] | |
}, | |
"isReady": true | |
}, | |
"addTypename": true | |
}, | |
"addTypename": true, | |
"data": { | |
"data": {} | |
} | |
}, | |
"tokenName": "token", | |
"persisting": false | |
} | |
var tokenName = currentOptions.tokenName || AUTH_TOKEN_NAME | |
var authFunction = eval(() => '') | |
var getAuth = typeof authFunction === 'function' ? () => authFunction.call() : () => { | |
let token | |
if(process.server){ | |
const cookies = cookie.parse(req.headers.cookie || '') | |
token = cookies[tokenName] | |
} else { | |
token = jsCookie.get(tokenName) | |
} | |
return token ? AUTH_TYPE + token : '' | |
} | |
var options = Object.assign({}, currentOptions, { | |
ssr: !!process.server, | |
tokenName, | |
getAuth | |
}) | |
var cache = currentOptions.cache || new InMemoryCache() | |
if(!process.server) { | |
cache.restore(window.__NUXT__ ? window.__NUXT__.apollo.defaultClient : null) | |
} | |
options.cache = cache | |
var {apolloClient, wsClient} = createApolloClient(options) | |
apolloClient.wsClient = wsClient | |
providerOptions.defaultClient = apolloClient | |
const vueApolloOptions = Object.assign(providerOptions, { | |
errorHandler (error) { | |
console.log('%cError', 'background: red; color: white; padding: 2px 4px; border-radius: 3px; font-weight: bold;', error.message) | |
}, | |
}) | |
const apolloProvider = new VueApollo(vueApolloOptions) | |
// Allow access to the provider in the context | |
app.apolloProvider = apolloProvider | |
// Install the provider into the app | |
app.provide = apolloProvider.provide() | |
if (process.server) { | |
beforeNuxtRender(async ({ Components, nuxtState }) => { | |
Components.forEach((Component) => { | |
// Fix https://github.com/nuxt-community/apollo-module/issues/19 | |
if (Component.options && Component.options.apollo && Component.options.apollo.$init) { | |
delete Component.options.apollo.$init | |
} | |
}) | |
await apolloProvider.prefetchAll(ctx, Components) | |
nuxtState.apollo = apolloProvider.getStates() | |
}) | |
} | |
inject('apolloHelpers', { | |
onLogin: async (token, apolloClient = apolloProvider.defaultClient) => { | |
if (token) { | |
jsCookie.set(AUTH_TOKEN_NAME, token) | |
} else { | |
jsCookie.remove(AUTH_TOKEN_NAME) | |
} | |
if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient) | |
try { | |
await apolloClient.resetStore() | |
} catch (e) { | |
// eslint-disable-next-line no-console | |
console.log('%cError on cache reset (setToken)', 'color: orange;', e.message) | |
} | |
}, | |
onLogout: async (apolloClient = apolloProvider.defaultClient) => { | |
jsCookie.remove(AUTH_TOKEN_NAME) | |
if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient) | |
try { | |
await apolloClient.resetStore() | |
} catch (e) { | |
// eslint-disable-next-line no-console | |
console.log('%cError on cache reset (logout)', 'color: orange;', e.message) | |
} | |
}, | |
getToken: (tokenName = AUTH_TOKEN_NAME) => { | |
if(process.server){ | |
const cookies = cookie.parse(req.headers.cookie || '') | |
return cookies && cookies[tokenName] | |
} | |
return jsCookie.get(tokenName) | |
} | |
}) | |
} |
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
const InMemoryCache = require('apollo-cache-inmemory').InMemoryCache; | |
const IntrospectionFragmentMatcher = require('apollo-cache-inmemory').IntrospectionFragmentMatcher; | |
const introspectionQueryResultData = require('./apollo/client-configs/fragmentTypes.json'); | |
const fragmentMatcher = new IntrospectionFragmentMatcher({ | |
introspectionQueryResultData, | |
}) | |
module.exports = { | |
... | |
// Modules | |
modules: ['@nuxtjs/apollo'], | |
apollo: { | |
// tokenName: 'yourApolloTokenName', // optional, default: apollo-token | |
includeNodeModules: true, // optional, default: false (this includes graphql-tag for node_modules folder) | |
authenticationType: 'Bearer', // optional, default: 'Bearer' | |
// required | |
clientConfigs: { | |
default: { | |
httpEndpoint: GRAPHQL_ENDPOINT, | |
cache: new InMemoryCache({ fragmentMatcher }), | |
}, | |
}, | |
}, | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment