Skip to content

Commit dcecb94

Browse files
authored
chore: Update apollo packages (#3779)
* chore: update apollo packages * fix: better project switching logic * fix(SharedData): wait for current project id * refactor(apollo): use 2.5 client-size state API
1 parent b170e15 commit dcecb94

File tree

7 files changed

+352
-286
lines changed

7 files changed

+352
-286
lines changed

packages/@vue/cli-ui/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"fkill": "^5.3.0",
4444
"fs-extra": "^7.0.1",
4545
"globby": "^9.0.0",
46+
"graphql-subscriptions": "^1.1.0",
4647
"graphql-tag": "^2.9.2",
4748
"graphql-type-json": "^0.2.1",
4849
"javascript-stringify": "^1.6.0",
@@ -58,7 +59,7 @@
5859
"rss-parser": "^3.4.3",
5960
"semver": "^5.5.0",
6061
"shortid": "^2.2.11",
61-
"vue-cli-plugin-apollo": "^0.19.1",
62+
"vue-cli-plugin-apollo": "^0.19.2",
6263
"vue-virtual-scroller": "^1.0.0-rc.2",
6364
"watch": "^1.0.2"
6465
},

packages/@vue/cli-ui/src/components/app/ProjectQuickDropdown.vue

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ import PROJECTS from '@/graphql/project/projects.gql'
8989
import PROJECT_OPEN from '@/graphql/project/projectOpen.gql'
9090
import PROJECT_SET_FAVORITE from '@/graphql/project/projectSetFavorite.gql'
9191
import OPEN_IN_EDITOR from '@/graphql/file/fileOpenInEditor.gql'
92-
import CURRENT_PROJECT_ID_SET from '@/graphql/project/currentProjectIdSet.gql'
9392
9493
export default {
9594
apollo: {
@@ -125,13 +124,6 @@ export default {
125124
})
126125
127126
await resetApollo()
128-
129-
await this.$apollo.mutate({
130-
mutation: CURRENT_PROJECT_ID_SET,
131-
variables: {
132-
projectId: project.id
133-
}
134-
})
135127
},
136128
137129
async toggleCurrentFavorite () {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export default {
1+
export default () => ({
22
connected: true,
33
loading: 0,
44
darkMode: false,
55
currentProjectId: null
6-
}
6+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import gql from 'graphql-tag'
2+
3+
export default gql`
4+
extend type Query {
5+
connected: Boolean!
6+
loading: Boolean!
7+
darkMode: Boolean!
8+
currentProjectId: String
9+
}
10+
11+
extend type Mutation {
12+
connectedSet (value: Boolean!): Boolean
13+
loadingChange (mod: Int!): Boolean
14+
darkModeSet (enabled: Boolean!): Boolean
15+
currentProjectIdSet (projectId: String): Boolean
16+
}
17+
`

packages/@vue/cli-ui/src/util/shared-data.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@ export default {
4343
})
4444
},
4545

46-
created () {
46+
async created () {
4747
const options = this.$options.sharedData
4848
if (options) {
4949
if (typeof options === 'function') {
5050
let smartQueries
51-
this.$watch(options.bind(this), result => {
51+
this.$watch(options.bind(this), async result => {
5252
if (smartQueries) {
5353
smartQueries.forEach(s => s.destroy())
5454
}
55-
smartQueries = this.$syncSharedData(result)
55+
smartQueries = await this.$syncSharedData(result)
5656
}, {
5757
immediate: true
5858
})
5959
} else {
60-
this.$syncSharedData(options)
60+
await this.$syncSharedData(options)
6161
}
6262
// Force watchers to re-evaluate
6363
// Because we just added the proxies to this.$data.$sharedData[key]
@@ -69,15 +69,24 @@ export default {
6969

7070
methods: {
7171
$getProjectId () {
72-
const client = this.$apollo.getClient()
73-
const result = client.readQuery({
74-
query: CURRENT_PROJECT_ID
72+
return new Promise((resolve) => {
73+
const client = this.$apollo.getClient()
74+
const observable = client.watchQuery({
75+
query: CURRENT_PROJECT_ID
76+
})
77+
const sub = observable.subscribe({
78+
next ({ data }) {
79+
if (data.currentProjectId) {
80+
sub.unsubscribe()
81+
resolve(data.currentProjectId)
82+
}
83+
}
84+
})
7585
})
76-
return result.currentProjectId
7786
},
7887

7988
async $getSharedData (id) {
80-
const projectId = this.$getProjectId()
89+
const projectId = await this.$getProjectId()
8190
const result = await this.$apollo.query({
8291
query: SHARED_DATA,
8392
variables: {
@@ -88,8 +97,8 @@ export default {
8897
return result.sharedData.value
8998
},
9099

91-
$watchSharedData (id, cb) {
92-
const projectId = this.$getProjectId()
100+
async $watchSharedData (id, cb) {
101+
const projectId = await this.$getProjectId()
93102
return this.$apollo.addSmartQuery(id, {
94103
...genQuery(id, projectId),
95104
manual: true,
@@ -99,8 +108,8 @@ export default {
99108
})
100109
},
101110

102-
$setSharedData (id, value) {
103-
const projectId = this.$getProjectId()
111+
async $setSharedData (id, value) {
112+
const projectId = await this.$getProjectId()
104113
return this.$apollo.mutate({
105114
mutation: SHARED_DATA_UPDATE,
106115
variables: {
@@ -111,9 +120,7 @@ export default {
111120
})
112121
},
113122

114-
$syncSharedData (options) {
115-
const projectId = this.$getProjectId()
116-
const smartQueries = []
123+
async $syncSharedData (options) {
117124
for (const key in options) {
118125
const id = options[key]
119126
this.$set(this.$data.$sharedData, key, null)
@@ -127,6 +134,11 @@ export default {
127134
enumerable: true,
128135
configurable: true
129136
})
137+
}
138+
const projectId = await this.$getProjectId()
139+
const smartQueries = []
140+
for (const key in options) {
141+
const id = options[key]
130142
const smartQuery = this.$apollo.addSmartQuery(key, {
131143
...genQuery(id, projectId),
132144
update: undefined,

packages/@vue/cli-ui/src/vue-apollo.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import VueApollo from 'vue-apollo'
33
import { createApolloClient } from 'vue-cli-plugin-apollo/graphql-client'
44
import clientStateDefaults from './state/defaults'
55
import clientStateResolvers from './state/resolvers'
6+
import clientStateTypeDefs from './state/typeDefs'
67
// GraphQL documents
8+
import PROJECT_CURRENT from './graphql/project/projectCurrent.gql'
9+
import CURRENT_PROJECT_ID_SET from './graphql/project/currentProjectIdSet.gql'
710
import CONNECTED_SET from '@/graphql/connected/connectedSet.gql'
811
import LOADING_CHANGE from '@/graphql/loading/loadingChange.gql'
912
import DARK_MODE_SET from '@/graphql/dark-mode/darkModeSet.gql'
@@ -24,9 +27,10 @@ const options = {
2427
wsEndpoint: endpoint,
2528
persisting: false,
2629
websocketsOnly: true,
27-
clientState: {
28-
defaults: clientStateDefaults,
29-
resolvers: clientStateResolvers
30+
typeDefs: clientStateTypeDefs,
31+
resolvers: clientStateResolvers,
32+
onCacheInit: cache => {
33+
cache.writeData({ data: clientStateDefaults() })
3034
}
3135
}
3236

@@ -64,11 +68,26 @@ export const apolloProvider = new VueApollo({
6468

6569
export async function resetApollo () {
6670
console.log('[UI] Apollo store reset')
71+
72+
const { data: { projectCurrent } } = await apolloClient.query({
73+
query: PROJECT_CURRENT,
74+
fetchPolicy: 'network-only'
75+
})
76+
const projectId = projectCurrent.id
77+
6778
try {
6879
await apolloClient.resetStore()
6980
} catch (e) {
7081
// Potential errors
7182
}
83+
84+
await apolloClient.mutate({
85+
mutation: CURRENT_PROJECT_ID_SET,
86+
variables: {
87+
projectId
88+
}
89+
})
90+
7291
loadDarkMode()
7392
}
7493

0 commit comments

Comments
 (0)