Skip to content

Commit

Permalink
chore: apply changes from v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Nov 28, 2021
1 parent 52de9ef commit 54279f7
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 41 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ module.exports = {
'array-callback-return': 'warn',
},
},
{
files: [
'packages/**/*.js',
],
rules: {
'@typescript-eslint/explicit-module-boundary-types': 'off',
},
},
],
ignorePatterns: [
'node_modules/',
Expand Down
8 changes: 7 additions & 1 deletion packages/vue-apollo-components/src/ApolloMutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export default {
type: String,
default: 'div',
},

context: {
type: Object,
default: undefined,
},
},

data () {
Expand All @@ -63,13 +68,14 @@ export default {
mutation = mutation(gql)
}

this.$apollo.mutate({
return this.$apollo.mutate({
mutation,
client: this.clientId,
variables: this.variables,
optimisticResponse: this.optimisticResponse,
update: this.update,
refetchQueries: this.refetchQueries,
context: this.context,
...options,
}).then(result => {
this.$emit('done', result)
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-apollo-components/src/ApolloQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import gql from 'graphql-tag'
import { h } from 'vue'

function isDataFilled (data) {
return Object.keys(data).length > 0
return data && Object.keys(data).length > 0
}

export default {
Expand Down
2 changes: 2 additions & 0 deletions packages/vue-apollo-option/src/dollar-apollo.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,14 @@ export class DollarApollo {
}

set skipAllQueries (value) {
this._skipAllQueries = value
for (const key in this.queries) {
this.queries[key].skip = value
}
}

set skipAllSubscriptions (value) {
this._skipAllSubscriptions = value
for (const key in this.subscriptions) {
this.subscriptions[key].skip = value
}
Expand Down
7 changes: 6 additions & 1 deletion packages/vue-apollo-option/src/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,12 @@ export function installMixin (app, provider) {

serverPrefetch () {
if (this.$_apolloPromises) {
return Promise.all(this.$_apolloPromises)
return Promise.all(this.$_apolloPromises).then(() => {
destroy.call(this)
}).catch(e => {
destroy.call(this)
return Promise.reject(e)
})
}
},

Expand Down
19 changes: 12 additions & 7 deletions packages/vue-apollo-option/src/smart-apollo.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { throttle, debounce, omit, addGqlError } from '../lib/utils'
import { isServer } from './env'

const skippAllKeys = {
query: '_skipAllQueries',
subscription: '_skipAllSubscriptions',
}

export default class SmartApollo {
type = null
vueApolloSpecialKeys = []

constructor (vm, key, options, autostart = true) {
constructor (vm, key, options) {
this.vm = vm
this.key = key
this.initialOptions = options
Expand All @@ -15,10 +20,6 @@ export default class SmartApollo {
this._watchers = []
this._destroyed = false
this.lastApolloOptions = null

if (autostart) {
this.autostart()
}
}

autostart () {
Expand All @@ -27,7 +28,7 @@ export default class SmartApollo {
immediate: true,
deep: this.options.deep,
})
} else if (!this.options.skip) {
} else if (!this.options.skip && !this.allSkip) {
this.start()
} else {
this._skip = true
Expand Down Expand Up @@ -69,14 +70,18 @@ export default class SmartApollo {
}

set skip (value) {
if (value) {
if (value || this.allSkip) {
this.stop()
} else {
this.start()
}
this._skip = value
}

get allSkip () {
return this.vm.$apollo[skippAllKeys[this.type]]
}

refresh () {
if (!this._skip) {
this.stop()
Expand Down
48 changes: 33 additions & 15 deletions packages/vue-apollo-option/src/smart-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class SmartQuery extends SmartApollo {
}
}

super(vm, key, options, false)
super(vm, key, options)

if (isServer) {
this.firstRun = new Promise((resolve, reject) => {
Expand Down Expand Up @@ -82,6 +82,17 @@ export default class SmartQuery extends SmartApollo {
}
}

generateApolloOptions (variables) {
const apolloOptions = super.generateApolloOptions(variables)

if (this.vm.$isServer) {
// Don't poll on the server, that would run indefinitely
delete apolloOptions.pollInterval
}

return apolloOptions
}

executeApollo (variables) {
const variablesJson = JSON.stringify(variables)

Expand All @@ -105,9 +116,11 @@ export default class SmartQuery extends SmartApollo {
this.startQuerySubscription()

if (this.options.fetchPolicy !== 'no-cache' || this.options.notifyOnNetworkStatusChange) {
const currentResult = this.maySetLoading()
const currentResult = this.retrieveCurrentResult()

if (!currentResult.loading || this.options.notifyOnNetworkStatusChange) {
if (this.options.notifyOnNetworkStatusChange ||
// Initial call of next result when it's not loading (for Apollo Client 3)
(this.observer.getCurrentResult && !currentResult.loading)) {
this.nextResult(currentResult)
}
}
Expand All @@ -130,8 +143,11 @@ export default class SmartQuery extends SmartApollo {
})
}

maySetLoading (force = false) {
const currentResult = this.observer.getCurrentResult()
/**
* May update loading state
*/
retrieveCurrentResult (force = false) {
const currentResult = this.observer.getCurrentResult ? this.observer.getCurrentResult() : this.observer.currentResult()
if (force || currentResult.loading) {
if (!this.loading) {
this.applyLoadingModifier(1)
Expand All @@ -146,8 +162,10 @@ export default class SmartQuery extends SmartApollo {

const { data, loading, error, errors } = result

if (error || errors) {
this.firstRunReject()
const anyErrors = errors && errors.length

if (error || anyErrors) {
this.firstRunReject(error)
}

if (!loading) {
Expand All @@ -157,7 +175,7 @@ export default class SmartQuery extends SmartApollo {
// If `errorPolicy` is set to `all`, an error won't be thrown
// Instead result will have an `errors` array of GraphQL Errors
// so we need to reconstruct an error object similar to the normal one
if (errors && errors.length) {
if (anyErrors) {
const e = new Error(`GraphQL error: ${errors.map(e => e.message).join(' | ')}`)
Object.assign(e, {
graphQLErrors: errors,
Expand All @@ -168,7 +186,7 @@ export default class SmartQuery extends SmartApollo {
super.catchError(e)
}

if (this.observer.options.errorPolicy === 'none' && (error || errors)) {
if (this.observer.options.errorPolicy === 'none' && (error || anyErrors)) {
// Don't apply result
return
}
Expand Down Expand Up @@ -201,9 +219,9 @@ export default class SmartQuery extends SmartApollo {

catchError (error) {
super.catchError(error)
this.firstRunReject()
this.firstRunReject(error)
this.loadingDone(error)
this.nextResult(this.observer.getCurrentResult())
this.nextResult(this.observer.getCurrentResult ? this.observer.getCurrentResult() : this.observer.currentResult())
// The observable closes the sub if an error occurs
this.resubscribeToQuery()
}
Expand Down Expand Up @@ -250,7 +268,7 @@ export default class SmartQuery extends SmartApollo {

fetchMore (...args) {
if (this.observer) {
this.maySetLoading(true)
this.retrieveCurrentResult(true)
return this.observer.fetchMore(...args).then(result => {
if (!result.loading) {
this.loadingDone()
Expand All @@ -277,7 +295,7 @@ export default class SmartQuery extends SmartApollo {
}
return result
})
this.maySetLoading()
this.retrieveCurrentResult()
return result
}
}
Expand All @@ -286,7 +304,7 @@ export default class SmartQuery extends SmartApollo {
this.options.variables = variables
if (this.observer) {
const result = this.observer.setVariables(variables, tryFetch)
this.maySetLoading()
this.retrieveCurrentResult()
return result
}
}
Expand All @@ -295,7 +313,7 @@ export default class SmartQuery extends SmartApollo {
Object.assign(this.options, options)
if (this.observer) {
const result = this.observer.setOptions(options)
this.maySetLoading()
this.retrieveCurrentResult()
return result
}
}
Expand Down
25 changes: 25 additions & 0 deletions packages/vue-apollo-option/src/smart-subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ export default class SmartSubscription extends SmartApollo {
'linkedQuery',
]

constructor (vm, key, options, autostart = true) {
super(vm, key, options)

if (autostart) {
this.autostart()
}
}

generateApolloOptions (variables) {
const apolloOptions = super.generateApolloOptions(variables)

apolloOptions.onError = this.catchError.bind(this)

return apolloOptions
}

executeApollo (variables) {
const variablesJson = JSON.stringify(variables)
if (this.sub) {
Expand Down Expand Up @@ -59,4 +75,13 @@ export default class SmartSubscription extends SmartApollo {
this.options.result.call(this.vm, data, this.key)
}
}

catchError (error) {
super.catchError(error)
// Restart the subscription
if (!this.skip) {
this.stop()
this.start()
}
}
}
15 changes: 0 additions & 15 deletions packages/vue-apollo-option/types/gql.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/vue-apollo-option/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import './vue'
import './gql'
import { ApolloProvider, ApolloProviderOptions } from './apollo-provider'

export { ApolloProvider }
Expand Down

0 comments on commit 54279f7

Please sign in to comment.