chore: add eslint and fix lint errors
This commit is contained in:
+63
-65
@@ -1,9 +1,9 @@
|
||||
import { ApolloClient } from 'apollo-client'
|
||||
import { createHttpLink } from 'apollo-link-http'
|
||||
import { InMemoryCache } from 'apollo-cache-inmemory';
|
||||
import { InMemoryCache } from 'apollo-cache-inmemory'
|
||||
import { setContext } from 'apollo-link-context'
|
||||
import { useAuth } from './auth'
|
||||
import { reactive, toRefs } from '@vue/composition-api'
|
||||
import useAuth from './auth'
|
||||
|
||||
let instance = null
|
||||
|
||||
@@ -13,23 +13,22 @@ const httpLink = createHttpLink({
|
||||
uri: apiUrl
|
||||
})
|
||||
|
||||
const getToken = async (options) => {
|
||||
const getToken = async options => {
|
||||
const { getTokenSilently, isAuthenticated } = useAuth()
|
||||
if (isAuthenticated.value) {
|
||||
return await getTokenSilently.value(options)
|
||||
} else {
|
||||
return options
|
||||
return getTokenSilently.value(options)
|
||||
}
|
||||
};
|
||||
return options
|
||||
}
|
||||
|
||||
const authLink = setContext(async (_, { headers }) => {
|
||||
const token = await getToken()
|
||||
return {
|
||||
headers: {
|
||||
...headers,
|
||||
authorization: token ? `Bearer ${token}` : '',
|
||||
authorization: token ? `Bearer ${token}` : ''
|
||||
}
|
||||
};
|
||||
}
|
||||
})
|
||||
|
||||
const client = new ApolloClient({
|
||||
@@ -37,84 +36,83 @@ const client = new ApolloClient({
|
||||
cache: new InMemoryCache(),
|
||||
defaultOptions: {
|
||||
watchQuery: {
|
||||
fetchPolicy: 'cache-and-network',
|
||||
},
|
||||
},
|
||||
fetchPolicy: 'cache-and-network'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
instance = client
|
||||
|
||||
export const useMutation = (mutation, options) => {
|
||||
const opts = options;
|
||||
const doMutate = options => new Promise((resolve, reject) => {
|
||||
out.loading = true
|
||||
instance.mutate({
|
||||
mutation,
|
||||
...opts,
|
||||
...options
|
||||
})
|
||||
.then(result => {
|
||||
out.loading = false
|
||||
out.data = result.data
|
||||
resolve(result)
|
||||
})
|
||||
.catch(e => {
|
||||
out.loading = false
|
||||
out.error = e
|
||||
reject(e)
|
||||
})
|
||||
})
|
||||
const out = reactive({
|
||||
data: {},
|
||||
error: null,
|
||||
loading: false,
|
||||
loading: false
|
||||
})
|
||||
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)
|
||||
})
|
||||
})
|
||||
return [doMutate, toRefs(out)]
|
||||
}
|
||||
|
||||
export const useLazyQuery = (query, options) => {
|
||||
const opts = options
|
||||
let watchedQuery = null
|
||||
const doQuery = options => new Promise((resolve, reject) => {
|
||||
out.loading = true
|
||||
let effectiveOptions = {
|
||||
query,
|
||||
...(opts || {}),
|
||||
...(options || {})
|
||||
}
|
||||
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)
|
||||
})
|
||||
const out = reactive({
|
||||
data: {},
|
||||
error: null,
|
||||
loading: false
|
||||
})
|
||||
const refetch = variables => {
|
||||
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 => {
|
||||
const opts = {}
|
||||
if (variables) (
|
||||
opts.variables = variables
|
||||
)
|
||||
if (variables) opts.variables = variables
|
||||
doQuery(opts)
|
||||
}
|
||||
const startPolling = interval => doQuery({pollInterval: interval})
|
||||
const stopPolling = () => {
|
||||
out.startPolling = interval => doQuery({ pollInterval: interval })
|
||||
out.stopPolling = () => {
|
||||
if (watchedQuery) {
|
||||
watchedQuery.stopPolling()
|
||||
}
|
||||
}
|
||||
const out = reactive({
|
||||
data: {},
|
||||
error: null,
|
||||
loading: false,
|
||||
refetch,
|
||||
startPolling,
|
||||
stopPolling
|
||||
})
|
||||
return [doQuery, toRefs(out)]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user