From 11806792363cd754e98157d8a215ab0dabce913d Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Mon, 20 Sep 2021 18:50:13 +0200 Subject: [PATCH] fix: remove $isServer, closes #1241 --- packages/vue-apollo-composable/src/useQuery.ts | 2 +- packages/vue-apollo-composable/src/useSubscription.ts | 3 +-- packages/vue-apollo-composable/src/util/env.ts | 1 + packages/vue-apollo-composable/src/util/types.ts | 1 - packages/vue-apollo-option/src/dollar-apollo.js | 10 +++++----- packages/vue-apollo-option/src/env.js | 1 + packages/vue-apollo-option/src/mixin.js | 5 +++-- packages/vue-apollo-option/src/smart-apollo.js | 5 +++-- packages/vue-apollo-option/src/smart-query.js | 5 +++-- 9 files changed, 18 insertions(+), 15 deletions(-) create mode 100644 packages/vue-apollo-composable/src/util/env.ts create mode 100644 packages/vue-apollo-option/src/env.js diff --git a/packages/vue-apollo-composable/src/useQuery.ts b/packages/vue-apollo-composable/src/useQuery.ts index 90ea007..ef6a096 100644 --- a/packages/vue-apollo-composable/src/useQuery.ts +++ b/packages/vue-apollo-composable/src/useQuery.ts @@ -30,6 +30,7 @@ import { paramToReactive } from './util/paramToReactive' import { useEventHook } from './util/useEventHook' import { trackQuery } from './util/loadingTracking' import { toApolloError } from './util/toApolloError' +import { isServer } from './util/env' import type { CurrentInstance } from './util/types' @@ -142,7 +143,6 @@ export function useQueryImpl< ): UseQueryReturn { // Is on server? const vm = getCurrentInstance() as CurrentInstance | null - const isServer = vm?.$isServer ?? false const currentOptions = ref>() diff --git a/packages/vue-apollo-composable/src/useSubscription.ts b/packages/vue-apollo-composable/src/useSubscription.ts index 179f1ad..8804282 100644 --- a/packages/vue-apollo-composable/src/useSubscription.ts +++ b/packages/vue-apollo-composable/src/useSubscription.ts @@ -25,9 +25,9 @@ import { paramToReactive } from './util/paramToReactive' import { useApolloClient } from './useApolloClient' import { useEventHook } from './util/useEventHook' import { trackSubscription } from './util/loadingTracking' - import type { CurrentInstance } from './util/types' import { toApolloError } from './util/toApolloError' +import { isServer } from './util/env' export interface UseSubscriptionOptions < // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -113,7 +113,6 @@ export function useSubscription < ): UseSubscriptionReturn { // Is on server? const vm = getCurrentInstance() as CurrentInstance | null - const isServer = vm?.$isServer ?? false const documentRef = paramToRef(document) const variablesRef = paramToRef(variables) diff --git a/packages/vue-apollo-composable/src/util/env.ts b/packages/vue-apollo-composable/src/util/env.ts new file mode 100644 index 0000000..37b9191 --- /dev/null +++ b/packages/vue-apollo-composable/src/util/env.ts @@ -0,0 +1 @@ +export const isServer = typeof window === 'undefined' diff --git a/packages/vue-apollo-composable/src/util/types.ts b/packages/vue-apollo-composable/src/util/types.ts index fff847c..8e3df90 100644 --- a/packages/vue-apollo-composable/src/util/types.ts +++ b/packages/vue-apollo-composable/src/util/types.ts @@ -5,5 +5,4 @@ export interface CurrentInstance extends Omit { @@ -114,11 +114,11 @@ export class DollarApollo { } const smart = this.queries[key] = new SmartQuery(this.vm, key, finalOptions, false) - if (!this.vm.$isServer || finalOptions.prefetch !== false) { + if (!isServer || finalOptions.prefetch !== false) { smart.autostart() } - if (!this.vm.$isServer) { + if (!isServer) { const subs = finalOptions.subscribeToMore if (subs) { if (Array.isArray(subs)) { @@ -141,7 +141,7 @@ export class DollarApollo { } addSmartSubscription (key, options) { - if (!this.vm.$isServer) { + if (!isServer) { options = reapply(options, this.vm) const smart = this.subscriptions[key] = new SmartSubscription(this.vm, key, options, false) diff --git a/packages/vue-apollo-option/src/env.js b/packages/vue-apollo-option/src/env.js new file mode 100644 index 0000000..37b9191 --- /dev/null +++ b/packages/vue-apollo-option/src/env.js @@ -0,0 +1 @@ +export const isServer = typeof window === 'undefined' diff --git a/packages/vue-apollo-option/src/mixin.js b/packages/vue-apollo-option/src/mixin.js index be1fa22..6e4cc86 100644 --- a/packages/vue-apollo-option/src/mixin.js +++ b/packages/vue-apollo-option/src/mixin.js @@ -1,5 +1,6 @@ import { reapply } from '../lib/utils' import { DollarApollo } from './dollar-apollo' +import { isServer } from './env' function hasProperty (holder, key) { return typeof holder !== 'undefined' && Object.prototype.hasOwnProperty.call(holder, key) @@ -70,7 +71,7 @@ function launch () { if (key.charAt(0) !== '$') { let options = apollo[key] const smart = this.$apollo.addSmartQuery(key, options) - if (this.$isServer) { + if (isServer) { options = reapply(options, this) if (apolloProvider.prefetch !== false && options.prefetch !== false && apollo.$prefetch !== false && !smart.skip) { this.$_apolloPromises.push(smart.firstRun) @@ -123,7 +124,7 @@ export function installMixin (app, provider) { beforeCreate () { this.$apollo = new DollarApollo(this, provider) proxyData.call(this) - if (this.$isServer) { + if (isServer) { // Patch render function to cleanup apollo const render = this.$options.render this.$options.render = (h) => { diff --git a/packages/vue-apollo-option/src/smart-apollo.js b/packages/vue-apollo-option/src/smart-apollo.js index e62ebe0..b79aa21 100644 --- a/packages/vue-apollo-option/src/smart-apollo.js +++ b/packages/vue-apollo-option/src/smart-apollo.js @@ -1,4 +1,5 @@ import { throttle, debounce, omit, addGqlError } from '../lib/utils' +import { isServer } from './env' export default class SmartApollo { type = null @@ -95,7 +96,7 @@ export default class SmartApollo { this.options[prop] = query this.refresh() } - if (!this.vm.$isServer) { + if (!isServer) { cb = this.options.throttle ? throttle(cb, this.options.throttle) : cb cb = this.options.debounce ? debounce(cb, this.options.debounce) : cb } @@ -108,7 +109,7 @@ export default class SmartApollo { // GraphQL Variables if (typeof this.options.variables === 'function') { let cb = this.executeApollo.bind(this) - if (!this.vm.$isServer) { + if (!isServer) { cb = this.options.throttle ? throttle(cb, this.options.throttle) : cb cb = this.options.debounce ? debounce(cb, this.options.debounce) : cb } diff --git a/packages/vue-apollo-option/src/smart-query.js b/packages/vue-apollo-option/src/smart-query.js index 4a15aae..a4c1fcd 100644 --- a/packages/vue-apollo-option/src/smart-query.js +++ b/packages/vue-apollo-option/src/smart-query.js @@ -1,5 +1,6 @@ import SmartApollo from './smart-apollo' import { VUE_APOLLO_QUERY_KEYWORDS } from '../lib/consts' +import { isServer } from './env' export default class SmartQuery extends SmartApollo { type = 'query' @@ -19,14 +20,14 @@ export default class SmartQuery extends SmartApollo { super(vm, key, options, false) - if (vm.$isServer) { + if (isServer) { this.firstRun = new Promise((resolve, reject) => { this._firstRunResolve = resolve this._firstRunReject = reject }) } - if (this.vm.$isServer) { + if (isServer) { this.options.fetchPolicy = 'network-only' }