fix: eslint errors

This commit is contained in:
2020-04-06 10:52:55 +02:00
parent cc9968bd06
commit b4191d1416
9 changed files with 145 additions and 118 deletions
+69 -56
View File
@@ -1,9 +1,13 @@
/* eslint no-shadow: ["error", { "allow": ["options"] }] */
import { ApolloClient } from 'apollo-client'
import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory'
import {
InMemoryCache,
IntrospectionFragmentMatcher
} from 'apollo-cache-inmemory'
import { createHttpLink } from 'apollo-link-http'
import { setContext } from 'apollo-link-context'
import { useAuth } from './auth'
import { computed, reactive, toRefs } from '@vue/composition-api'
import useAuth from './auth'
import introspectionQueryResultData from '../fragmentTypes.json'
const fragmentMatcher = new IntrospectionFragmentMatcher({
@@ -20,7 +24,7 @@ const httpLink = createHttpLink({
uri: apiUrl
})
const getToken = async (options) => {
const getToken = async options => {
const { getTokenSilently } = useAuth()
return getTokenSilently.value(options)
}
@@ -47,60 +51,72 @@ instance = new ApolloClient({
const merge = (target, ...source) => {
const sources = source instanceof Array ? source : [source]
return sources.reduce((acc, source) => {
for (const key of Object.keys(source)) {
if (source[key] instanceof Object) Object.assign(source[key], merge(acc[key], source[key]))
}
return Object.assign(acc, source)
return sources.reduce((acc, src) => {
Object.keys(src).forEach(key => {
if (src[key] instanceof Object)
Object.assign(src[key], merge(acc[key], src[key]))
})
return Object.assign(acc, src)
}, target || {})
}
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
})
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)
})
})
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
const effectiveOptions = merge({ 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 doQuery = options =>
new Promise((resolve, reject) => {
out.loading = true
const effectiveOptions = merge({ 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 refetch = variables => {
doQuery({
variables: {
@@ -114,17 +130,15 @@ export const useLazyQuery = (query, options) => {
watchedQuery.stopPolling()
}
}
const out = reactive({
data: {},
error: null,
loading: false
})
return [doQuery, {
...toRefs(out),
refetch,
startPolling,
stopPolling
}]
return [
doQuery,
{
...toRefs(out),
refetch,
startPolling,
stopPolling
}
]
}
export const useQuery = (query, options) => {
@@ -135,24 +149,23 @@ export const useQuery = (query, options) => {
export const useResult = (result, defaultValue, pick) => {
return computed(() => {
const value = result.value
const { value } = result
if (value) {
if (pick) {
try {
return pick(value)
} catch (e) {
// Silent error
return defaultValue
}
} else {
const keys = Object.keys(value)
if (keys.length === 1) {
// Automatically take the only key in result data
return value[keys[0]]
} else {
// Return entire result data
return value
}
// Return entire result data
return value
}
} else {
return defaultValue