当前位置: 首页 > Web前端 > vue.js

GraphQL实践第二部分

时间:2023-03-31 18:04:43 vue.js

GraphQL瀹炶返绡嘨ue+GraphQL鎼缓瀹㈡埛绔湪涓婁竴绡囨枃绔犱腑锛屾垜浠粙缁嶄簡浣跨敤Nestjs+GraphQL鎼缓鏈嶅姟绔€傛湰鏂囪褰曚娇鐢╒ue+GraphQL鎼缓瀹㈡埛绔€傚鎴风椤圭洰鐩綍缁撴瀯濡備笅锛氶鍏堟垜浠娇鐢╲ue-cli鏂板缓涓€涓」鐩紝鐒跺悗瀹夎渚濊禆锛歯pminstallapollo-cache-inmemoryapollo-clientapollo-linkapollo-link-httpapollo-link-wsapollo-utilitiesvue-apollo-Simportdependencies//main.jsimportVuefrom'vue'importAppfrom'./App.vue'import{apolloProvider}from'./vue-apollo';Vue.config.productionTip=falsenewVue({render:h=>h(App),//娉ㄥ叆apolloProviderlikevue-routerorvuexapolloProvider,}).$mount('#app')//vue-apollo.js//鐩稿叧鏂囨。锛岃鍙傝€僪ttps://apollo.vuejs.org/zh-cn/import{ApolloClient}from'apollo-client'import{createHttpLink}from'apollo-link-http'import{InMemoryCache}from'apollo-cache-inmemory'importVuefrom'vue'importVueApollofrom'vue-apollo'//鏂扮殑瀵煎叆鏂囦欢-utilities'Vue.use(VueApollo)//HTTP杩炴帴鍒癆PIconsthttpLink=createHttpLink({//杩欓噷闇€瑕佷娇鐢ㄧ粷瀵硅矾寰剈ri:'http://localhost:3001/graphql',})//鍒涘缓璁㈤槄鐨剋ebsocket杩炴帴constwsLink=newWebSocketLink({uri:'ws://localhost:3001/graphql',options:{reconnect:true,}})//浣跨敤鎷嗗垎杩炴帴鐨勫姛鑳?/鍙互鏍规嵁鍙戦€佺殑鎿嶄綔绫诲瀷灏嗘暟鎹彂閫佸埌涓嶅悓鐨勮繛鎺onstlink=split(({query})=>{constdefinition=getMainDefinition(query)returndefinition.kind==='OperationDefinition'&&definition.operation==='subscription'},wsLink,httpLink)//鍒涘缓apollo瀹㈡埛绔痗onstapolloClient=newApolloClient({link,cache:newInMemoryCache(),connectToDevTools:true,})exportconstapolloProvider=newVueApollo({defaultClient:apolloClient,})缂栧啓涓氬姟浠g爜//App.vue//HelloGraphQL.vue鑷虫锛屾垜浠氨鍙互璇锋眰鏈嶅姟绔湇鍔′簡锛侌煄夝煄夝煄?/p>