The Wayback Machine - https://web.archive.org/web/20201022230049/https://github.com/vuejs/vue-apollo/issues/1011
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Vue 3.0 composition API #1011

Closed
lorensr opened this issue Jul 8, 2020 · 16 comments
Closed

Support Vue 3.0 composition API #1011

lorensr opened this issue Jul 8, 2020 · 16 comments
Labels

Comments

@lorensr
Copy link

@lorensr lorensr commented Jul 8, 2020

According to the Vue roadmap, the API for v3.0 is now frozen.

@lorensr lorensr added the enhancement label Jul 8, 2020
@the-nicolas
Copy link

@the-nicolas the-nicolas commented Jul 21, 2020

This is really a major problem. We had to move many projects to react during the last month, because we were not able to go with vue 3. We did many poc and in general vue3 was doing already great. Just the missing graphql and some typescript problems killed the approach.

@MechJosh0
Copy link

@MechJosh0 MechJosh0 commented Jul 29, 2020

vue-apollo v4 has been available since early December 2019, with v4.0.0-alpha.10 having been released 2 days ago.

@justinwaite
Copy link

@justinwaite justinwaite commented Aug 6, 2020

vue-apollo v4 has been available since early December 2019, with v4.0.0-alpha.10 having been released 2 days ago.

True, but this doesn't support Vue 3, just Vue 2 with the composition api plugin.

@MechJosh0
Copy link

@MechJosh0 MechJosh0 commented Aug 6, 2020

From my understanding it can be used with vue 3. Have you tested, @justinwaite?

I do use it with composition api because storybook is the last thing holding me back from upgrading to vue 3. So I've not tested myself, but I believe it is compatible in the latest version.

Its latest version was released with using a package which allows vue apollo 4 to be used with either vue 2 or vue 3.

@justinwaite
Copy link

@justinwaite justinwaite commented Aug 6, 2020

@MechJosh0 there may be multiple things at play here, but I am unable to get it working with Vue 3 + Vite. See my comment here: #1029 (comment)

@szvest
Copy link

@szvest szvest commented Aug 10, 2020

It does work with Composition API but the thing is that it only does up to [email protected] (see issue here). I had v4.0.0-alpha.8 and [email protected] working fine.

However, it gives this error after upgrading to v4.0.0-alpha.10
[vue-composition-api] must call Vue.use(VueCompositionAPI) before using any function

@lzurbriggen
Copy link

@lzurbriggen lzurbriggen commented Aug 11, 2020

From what I can tell at least @vue/apollo-composable use features of the composition-api/Vue2 that are simply not available in Vue3 (onServerPrefetch/serverPrefetch , vm.$isServer). I can't find much regarding Vue3 SSR so I don't think there's a 1-to-1 replacement for the SSR API.

@frandiox
Copy link

@frandiox frandiox commented Aug 17, 2020

v4.0.0-alpha.10 is supposed to work with Vue 3 since it adds vue-demi to automatically toggle between @vue/composition-api (Vue 2) and Vue 3.

However, I've found a couple of issues with it: #1041 #1040

With those two changes I got it working. Until they are merged or refactored by Guillaume, you can try this patch:

// scripts/vue-apollo-patch.js

const fs = require('fs')
const path = require('path')

const loadTrackingPath = path.resolve(
  __dirname,
  '../node_modules/@vue/apollo-composable/dist/util/loadingTracking.js'
)

fs.writeFileSync(
  loadTrackingPath,
  fs.readFileSync(loadTrackingPath, 'utf8').replace(/\.\$root/m, '.root')
)

const useQueryPath = path.resolve(
  __dirname,
  '../node_modules/@vue/apollo-composable/dist/useQuery.js'
)

fs.writeFileSync(
  useQueryPath,
  fs
    .readFileSync(useQueryPath, 'utf8')
    .replace(/(^.*onServerPrefetch)/m, '$1=()=>{}; $1')
    .replace(/(.* require\("vue"\);)/m, '')
    .replace(/^.*(nextTick)/m, 'vue_demi_1.$1')
)

In package.json:

"scripts": {
  "postinstall": "node scripts/vue-apollo-patch.js"
}

Just make sure the paths in the script point to the right files (in case node_modules is not in the parent directory).

Hope that helps!

@Maxhodges
Copy link

@Maxhodges Maxhodges commented Aug 18, 2020

Thanks @frandiox

@heywhy
Copy link

@heywhy heywhy commented Aug 24, 2020

v4.0.0-alpha.10 is supposed to work with Vue 3 since it adds vue-demi to automatically toggle between @vue/composition-api (Vue 2) and Vue 3.

However, I've found a couple of issues with it: #1041 #1040

With those two changes I got it working. Until they are merged or refactored by Guillaume, you can try this patch:

// scripts/vue-apollo-patch.js

const fs = require('fs')
const path = require('path')

const loadTrackingPath = path.resolve(
  __dirname,
  '../node_modules/@vue/apollo-composable/dist/util/loadingTracking.js'
)

fs.writeFileSync(
  loadTrackingPath,
  fs.readFileSync(loadTrackingPath, 'utf8').replace(/\.\$root/m, '.root')
)

const useQueryPath = path.resolve(
  __dirname,
  '../node_modules/@vue/apollo-composable/dist/useQuery.js'
)

fs.writeFileSync(
  useQueryPath,
  fs
    .readFileSync(useQueryPath, 'utf8')
    .replace(/(^.*onServerPrefetch)/m, '$1=()=>{}; $1')
    .replace(/(.* require\("vue"\);)/m, '')
    .replace(/^.*(nextTick)/m, 'vue_demi_1.$1')
)

In package.json:

"scripts": {
  "postinstall": "node scripts/vue-apollo-patch.js"
}

Just make sure the paths in the script point to the right files (in case node_modules is not in the parent directory).

Hope that helps!

The quoted reply fails because the onServerPrefetch property can't be resigned on a freezed object. The below snippet solves the issue:

const fs = require('fs')
const path = require('path')

const loadTrackingPath = path.resolve(
  __dirname,
  '../node_modules/@vue/apollo-composable/dist/util/loadingTracking.js'
)

fs.rmdirSync(path.resolve(__dirname, '../node_modules/.vite_opt_cache'), {
  recursive: true
})

fs.writeFileSync(
  loadTrackingPath,
  fs.readFileSync(loadTrackingPath, 'utf8').replace(/\.\$root/m, '.root')
)

const useQueryPath = path.resolve(
  __dirname,
  '../node_modules/@vue/apollo-composable/dist/useQuery.js'
)

fs.writeFileSync(
  useQueryPath,
  fs
    .readFileSync(useQueryPath, 'utf8')
    .replace(/(^.*onServerPrefetch)(\.\?\.)?/m, '$1?.')
    .replace(/(.* require\("vue"\);)/m, '')
    .replace(/^.*(nextTick)/m, 'vue_demi_1.$1')
)
@ivansieder
Copy link

@ivansieder ivansieder commented Sep 2, 2020

It does work with Composition API but the thing is that it only does up to [email protected] (see issue here). I had v4.0.0-alpha.8 and [email protected] working fine.

However, it gives this error after upgrading to v4.0.0-alpha.10
[vue-composition-api] must call Vue.use(VueCompositionAPI) before using any function

For me this issue got resolved by putting Vue.use(VueCompositionAPI) into an external file (the same one where I'm setting up Apollo) and then importing it in the main.{ts,js}

@Akryum
Copy link
Member

@Akryum Akryum commented Oct 15, 2020

Full support for Vue 3 is pending the inclusion of onServerPrefetch (currently Vue 3 only has serverPrefetch as a Component option).

@frandiox
Copy link

@frandiox frandiox commented Oct 16, 2020

@Akryum Could you please release v4.0.0-alpha.11 with the latest updates? It should make it work for SPAs at least since all the changes related to this patch are already merged. Thanks!

@Akryum
Copy link
Member

@Akryum Akryum commented Oct 17, 2020

Vue 3 support for @vue/apollo-composable is now included in automatic tests on v4 branch. (More tests to come)

@Akryum Akryum closed this Oct 17, 2020
@folke
Copy link

@folke folke commented Oct 18, 2020

Updated patch for v4.0.0-alpha.12 to make Vite happy:

const fs = require("fs")
const path = require("path")

fs.rmdirSync(path.resolve(__dirname, "../node_modules/.vite_opt_cache"), {
  recursive: true,
})

const useQueryPath = path.resolve(
  __dirname,
  "../node_modules/@vue/apollo-composable/dist/useQuery.js"
)

fs.writeFileSync(
  useQueryPath,
  fs.readFileSync(useQueryPath, "utf8").replace(/^onServerPrefetch, /mu, "")
)
@Stijn98s
Copy link

@Stijn98s Stijn98s commented Oct 22, 2020

Updated patch to fully remove onServerPrefetch to make it work with vite.

vite: 1.0.0-rc4
vue: 3.0.2
graphql: 15.3.0
@apollo/client: 3.2.5
@vue/apollo-composable: 4.0.0-alpha.12

const fs = require("fs")
const path = require("path")

fs.rmdirSync(path.resolve(__dirname, "../node_modules/.vite_opt_cache"), {
  recursive: true,
})

const useQueryPath = path.resolve(
  __dirname,
  "../node_modules/@vue/apollo-composable/dist/useQuery.js"
)

const vueApolloComposablePath = path.resolve(
  __dirname,
  "../node_modules/@vue/apollo-composable/dist/vue-apollo-composable.js"
)

fs.writeFileSync(
  useQueryPath,
  fs.readFileSync(useQueryPath, "utf8").replace(/^onServerPrefetch, /mu, "")
)

fs.writeFileSync(
  useQueryPath,
  fs.readFileSync(useQueryPath, "utf8").replace(/onServerPrefetch === null.*?\}\);/mus, "")
)

fs.writeFileSync(
  vueApolloComposablePath,
  fs.readFileSync(vueApolloComposablePath, "utf8").replace(/vue_demi_5.onServerPrefetch === null.*?\}\);/mus, "")
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.