2020-01-21 15:51:51 +01:00
|
|
|
import { ApolloClient } from 'apollo-client'
|
|
|
|
|
import { createHttpLink } from 'apollo-link-http'
|
2020-01-25 14:59:14 +01:00
|
|
|
import { InMemoryCache } from 'apollo-cache-inmemory'
|
2020-01-21 15:51:51 +01:00
|
|
|
import { setContext } from 'apollo-link-context'
|
|
|
|
|
import { reactive, toRefs } from '@vue/composition-api'
|
2020-01-25 14:59:14 +01:00
|
|
|
import useAuth from './auth'
|
2020-01-21 15:51:51 +01:00
|
|
|
|
|
|
|
|
let instance = null
|
|
|
|
|
|
|
|
|
|
const apiUrl = process.env.graphqlApi || '/query'
|
|
|
|
|
|
|
|
|
|
const httpLink = createHttpLink({
|
|
|
|
|
uri: apiUrl
|
|
|
|
|
})
|
|
|
|
|
|
2020-01-25 14:59:14 +01:00
|
|
|
const getToken = async options => {
|
2020-01-21 15:51:51 +01:00
|
|
|
const { getTokenSilently, isAuthenticated } = useAuth()
|
|
|
|
|
if (isAuthenticated.value) {
|
2020-01-25 14:59:14 +01:00
|
|
|
return getTokenSilently.value(options)
|
2020-01-21 15:51:51 +01:00
|
|
|
}
|
2020-01-25 14:59:14 +01:00
|
|
|
return options
|
|
|
|
|
}
|
2020-01-21 15:51:51 +01:00
|
|
|
|
|
|
|
|
const authLink = setContext(async (_, { headers }) => {
|
|
|
|
|
const token = await getToken()
|
|
|
|
|
return {
|
|
|
|
|
headers: {
|
|
|
|
|
...headers,
|
2020-01-25 14:59:14 +01:00
|
|
|
authorization: token ? `Bearer ${token}` : ''
|
2020-01-21 15:51:51 +01:00
|
|
|
}
|
2020-01-25 14:59:14 +01:00
|
|
|
}
|
2020-01-21 15:51:51 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const client = new ApolloClient({
|
|
|
|
|
link: authLink.concat(httpLink),
|
|
|
|
|
cache: new InMemoryCache(),
|
|
|
|
|
defaultOptions: {
|
|
|
|
|
watchQuery: {
|
2020-01-25 14:59:14 +01:00
|
|
|
fetchPolicy: 'cache-and-network'
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-21 15:51:51 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
instance = client
|
|
|
|
|
|
|
|
|
|
export const useMutation = (mutation, options) => {
|
|
|
|
|
const out = reactive({
|
|
|
|
|
data: {},
|
|
|
|
|
error: null,
|
2020-01-25 14:59:14 +01:00
|
|
|
loading: false
|
2020-01-21 15:51:51 +01:00
|
|
|
})
|
2020-01-25 14:59:14 +01:00
|
|
|
const doMutate = mutateOptions =>
|
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
|
out.loading = true
|
|
|
|
|
instance
|
|
|
|
|
.mutate({
|
|
|
|
|
mutation,
|
|
|
|
|
...options,
|
|
|
|
|
...mutateOptions
|
|
|
|
|
})
|
|
|
|
|
.then(result => {
|
|
|
|
|
out.loading = false
|
|
|
|
|
out.data = result.data
|
|
|
|
|
resolve(result)
|
|
|
|
|
})
|
|
|
|
|
.catch(e => {
|
|
|
|
|
out.loading = false
|
|
|
|
|
out.error = e
|
|
|
|
|
reject(e)
|
|
|
|
|
})
|
|
|
|
|
})
|
2020-01-21 15:51:51 +01:00
|
|
|
return [doMutate, toRefs(out)]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useLazyQuery = (query, options) => {
|
|
|
|
|
let watchedQuery = null
|
2020-01-25 14:59:14 +01:00
|
|
|
const out = reactive({
|
|
|
|
|
data: {},
|
|
|
|
|
error: null,
|
|
|
|
|
loading: false
|
2020-01-21 15:51:51 +01:00
|
|
|
})
|
2020-01-25 14:59:14 +01:00
|
|
|
const doQuery = queryOptions =>
|
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
|
out.loading = true
|
|
|
|
|
const effectiveOptions = {
|
|
|
|
|
query,
|
|
|
|
|
...(options || {}),
|
|
|
|
|
...(queryOptions || {})
|
|
|
|
|
}
|
|
|
|
|
watchedQuery = instance.watchQuery(effectiveOptions)
|
|
|
|
|
watchedQuery.subscribe(
|
|
|
|
|
({ loading, data }) => {
|
|
|
|
|
out.loading = loading
|
|
|
|
|
out.data = data || {}
|
|
|
|
|
out.error = null
|
|
|
|
|
resolve(data)
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
out.loading = false
|
|
|
|
|
out.error = error
|
|
|
|
|
reject(error)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
out.refetch = variables => {
|
2020-01-21 15:51:51 +01:00
|
|
|
const opts = {}
|
2020-01-25 14:59:14 +01:00
|
|
|
if (variables) opts.variables = variables
|
2020-01-21 15:51:51 +01:00
|
|
|
doQuery(opts)
|
|
|
|
|
}
|
2020-01-25 14:59:14 +01:00
|
|
|
out.startPolling = interval => doQuery({ pollInterval: interval })
|
|
|
|
|
out.stopPolling = () => {
|
2020-01-21 15:51:51 +01:00
|
|
|
if (watchedQuery) {
|
|
|
|
|
watchedQuery.stopPolling()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return [doQuery, toRefs(out)]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useQuery = (query, options) => {
|
|
|
|
|
const [doQuery, out] = useLazyQuery(query, options)
|
|
|
|
|
doQuery()
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// import { execute, makePromise, ApolloLink, Observable } from 'apollo-link';
|
|
|
|
|
// import { HttpLink } from 'apollo-link-http';
|
|
|
|
|
// const { includeCredentials } = require('./middleware');
|
|
|
|
|
// import { onError } from 'apollo-link-error';
|
|
|
|
|
//
|
|
|
|
|
// const defaultGraphUri = process.env.graphqlApi || 'https://shiny-gateway.unbound.se';
|
|
|
|
|
// const httpLink = new HttpLink({ uri: defaultGraphUri, fetch: includeCredentials, credentials: 'same-origin' });
|
|
|
|
|
// const errorLink = onError(({ graphQLErrors, networkError, operation, forward }) => {
|
|
|
|
|
// if (graphQLErrors) {
|
|
|
|
|
// console.log('GraphQL errors:', graphQLErrors);
|
|
|
|
|
// // for (let err of graphQLErrors) {
|
|
|
|
|
// // switch (err.extensions.code) {
|
|
|
|
|
// // case 'UNAUTHENTICATED':
|
|
|
|
|
// // // error code is set to UNAUTHENTICATED
|
|
|
|
|
// // // when AuthenticationError thrown in resolver
|
|
|
|
|
// //
|
|
|
|
|
// // // modify the operation context with a new token
|
|
|
|
|
// // }
|
|
|
|
|
// // }
|
|
|
|
|
// }
|
|
|
|
|
// if (networkError) {
|
|
|
|
|
// if (networkError.statusCode === 401) {
|
|
|
|
|
// return new Observable(observer => {
|
|
|
|
|
// // webAuth.checkSession(() => {
|
|
|
|
|
// const subscriber = {
|
|
|
|
|
// next: observer.next.bind(observer),
|
|
|
|
|
// error: observer.error.bind(observer),
|
|
|
|
|
// complete: observer.complete.bind(observer)
|
|
|
|
|
// };
|
|
|
|
|
//
|
|
|
|
|
// // Retry last failed request
|
|
|
|
|
// forward(operation).subscribe(subscriber);
|
|
|
|
|
// // }, (err) => {
|
|
|
|
|
// // console.log(err);
|
|
|
|
|
// // observer.error(err)
|
|
|
|
|
// // });
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// );
|