From 54279f7fc122f553a7f14b83ced604b3dd9b3548 Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Sun, 28 Nov 2021 17:27:47 +0100 Subject: [PATCH] chore: apply changes from v3 --- .eslintrc.js | 8 ++++ .../src/ApolloMutation.js | 8 +++- .../vue-apollo-components/src/ApolloQuery.js | 2 +- .../vue-apollo-option/src/dollar-apollo.js | 2 + packages/vue-apollo-option/src/mixin.js | 7 ++- .../vue-apollo-option/src/smart-apollo.js | 19 +++++--- packages/vue-apollo-option/src/smart-query.js | 48 +++++++++++++------ .../src/smart-subscription.js | 25 ++++++++++ packages/vue-apollo-option/types/gql.d.ts | 15 ------ packages/vue-apollo-option/types/index.d.ts | 1 - 10 files changed, 94 insertions(+), 41 deletions(-) delete mode 100644 packages/vue-apollo-option/types/gql.d.ts diff --git a/.eslintrc.js b/.eslintrc.js index 68a472830..8e74fcb84 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -78,6 +78,14 @@ module.exports = { 'array-callback-return': 'warn', }, }, + { + files: [ + 'packages/**/*.js', + ], + rules: { + '@typescript-eslint/explicit-module-boundary-types': 'off', + }, + }, ], ignorePatterns: [ 'node_modules/', diff --git a/packages/vue-apollo-components/src/ApolloMutation.js b/packages/vue-apollo-components/src/ApolloMutation.js index 51ef4054b..bd79f54f9 100644 --- a/packages/vue-apollo-components/src/ApolloMutation.js +++ b/packages/vue-apollo-components/src/ApolloMutation.js @@ -38,6 +38,11 @@ export default { type: String, default: 'div', }, + + context: { + type: Object, + default: undefined, + }, }, data () { @@ -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) diff --git a/packages/vue-apollo-components/src/ApolloQuery.js b/packages/vue-apollo-components/src/ApolloQuery.js index c61cca164..d55554cf1 100644 --- a/packages/vue-apollo-components/src/ApolloQuery.js +++ b/packages/vue-apollo-components/src/ApolloQuery.js @@ -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 { diff --git a/packages/vue-apollo-option/src/dollar-apollo.js b/packages/vue-apollo-option/src/dollar-apollo.js index b7ccb7a95..e6c1e592f 100644 --- a/packages/vue-apollo-option/src/dollar-apollo.js +++ b/packages/vue-apollo-option/src/dollar-apollo.js @@ -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 } diff --git a/packages/vue-apollo-option/src/mixin.js b/packages/vue-apollo-option/src/mixin.js index db08b566e..e4577f6b3 100644 --- a/packages/vue-apollo-option/src/mixin.js +++ b/packages/vue-apollo-option/src/mixin.js @@ -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) + }) } }, diff --git a/packages/vue-apollo-option/src/smart-apollo.js b/packages/vue-apollo-option/src/smart-apollo.js index b79aa21fc..daca81fc0 100644 --- a/packages/vue-apollo-option/src/smart-apollo.js +++ b/packages/vue-apollo-option/src/smart-apollo.js @@ -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 @@ -15,10 +20,6 @@ export default class SmartApollo { this._watchers = [] this._destroyed = false this.lastApolloOptions = null - - if (autostart) { - this.autostart() - } } autostart () { @@ -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 @@ -69,7 +70,7 @@ export default class SmartApollo { } set skip (value) { - if (value) { + if (value || this.allSkip) { this.stop() } else { this.start() @@ -77,6 +78,10 @@ export default class SmartApollo { this._skip = value } + get allSkip () { + return this.vm.$apollo[skippAllKeys[this.type]] + } + refresh () { if (!this._skip) { this.stop() diff --git a/packages/vue-apollo-option/src/smart-query.js b/packages/vue-apollo-option/src/smart-query.js index a4c1fcd75..bf3715c3b 100644 --- a/packages/vue-apollo-option/src/smart-query.js +++ b/packages/vue-apollo-option/src/smart-query.js @@ -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) => { @@ -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) @@ -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) } } @@ -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) @@ -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) { @@ -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, @@ -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 } @@ -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() } @@ -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() @@ -277,7 +295,7 @@ export default class SmartQuery extends SmartApollo { } return result }) - this.maySetLoading() + this.retrieveCurrentResult() return result } } @@ -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 } } @@ -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 } } diff --git a/packages/vue-apollo-option/src/smart-subscription.js b/packages/vue-apollo-option/src/smart-subscription.js index 67ff54bab..474feedf2 100644 --- a/packages/vue-apollo-option/src/smart-subscription.js +++ b/packages/vue-apollo-option/src/smart-subscription.js @@ -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) { @@ -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() + } + } } diff --git a/packages/vue-apollo-option/types/gql.d.ts b/packages/vue-apollo-option/types/gql.d.ts deleted file mode 100644 index 1b1980236..000000000 --- a/packages/vue-apollo-option/types/gql.d.ts +++ /dev/null @@ -1,15 +0,0 @@ -/* eslint-disable import/no-duplicates */ - -declare module '*.gql' { - import { DocumentNode } from 'graphql' - - const content: DocumentNode - export default content -} - -declare module '*.graphql' { - import { DocumentNode } from 'graphql' - - const content: DocumentNode - export default content -} diff --git a/packages/vue-apollo-option/types/index.d.ts b/packages/vue-apollo-option/types/index.d.ts index eeb4ad021..2f3d7f704 100644 --- a/packages/vue-apollo-option/types/index.d.ts +++ b/packages/vue-apollo-option/types/index.d.ts @@ -1,5 +1,4 @@ import './vue' -import './gql' import { ApolloProvider, ApolloProviderOptions } from './apollo-provider' export { ApolloProvider }