Vue-apollo — 在Vue-cli項目中使用Graphql

Vue-apollo — 在Vue-cli項目中使用Graphql

Vue-apollo — Integrates apollo in your Vue components with declarative queries.

固然咱們能夠經過直接在url中攜帶參數直接請求,這樣太過麻煩。vue-apollo爲咱們提供了一整套解決方案,能夠解決大部分問題。javascript

本篇文章將介紹如何在你的vue-cli項目中簡單使用vue-apollo和一些目前遇到的小坑。vue

安裝

npm install --save vue-apollo graphql apollo-client apollo-link apollo-link-http apollo-cache-inmemory graphql-tag

建立ApolloClient實例, 安裝VueApollo插件java

import Vue from 'vue'
import { ApolloClient } from 'apollo-client'
import { HttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import VueApollo from 'vue-apollo'

const httpLink = new HttpLink({
  // You should use an absolute URL here
  uri: 'http://localhost:3020/graphql',
})

// Create the apollo client
const apolloClient = new ApolloClient({
  link: httpLink,
  cache: new InMemoryCache(),
  connectToDevTools: true,
})

// Install the vue plugin
Vue.use(VueApollo)

若是你開啓了vue-cli提供的代理, 這裏一樣適用.vue-router

建立PROVIDER

就像vue-routervuex同樣, 須要將apolloProvider添加爲根組件.vuex

const apolloProvider = new VueApollo({
  defaultClient: apolloClient,
})

new Vue({
  el: '#app',
  provide: apolloProvider.provide(),
  render: h => h(App),
})

quasar-cli 中安裝

若是你不瞭解Quasar Framework而且不打算使用, 這段能夠跳過.vue-cli

plugins目錄中建立新的js文件, 並在 quasar.conf.js 中加入它. shell

打開建立好的文件:npm

import {ApolloClient} from 'apollo-client'
import {HttpLink} from 'apollo-link-http'
import {InMemoryCache} from 'apollo-cache-inmemory'
import VueApollo from 'vue-apollo'


// Create the apollo provider
const apolloProvider = new VueApollo({
  defaultClient: new ApolloClient({
    link: new HttpLink({
      // You should use an absolute URL here
      uri: 'http://localhost:3020/graphql',
    }),
    cache: new InMemoryCache(),
    connectToDevTools: true
  })
})

// leave the export, even if you don't use it
export default ({ app, Vue }) => {
  // something to do
  Vue.use(VueApollo)
  app.provide = apolloProvider.provide()
}

使用

query

須要提早在組件中定義graphql字符串.promise

<script>
import gql from "graphql-tag";
export default {
  data() {
    return {hello:'',loading:0};
  },
  apollo: {
    hello: {
      query() {
        return gql`{hello}`
      },
      loadingKey: "loading"
    }
  }
};
</script>

data中必須提早建立apollo中相應字段且字段名必須相同. app

經過gql建立graphql字符串, 特別注意:使用 query(){return gql` } 用來建立須要計算獲得的字符串, 如字符串中攜帶${this.hello}等. 純字符串可用query:gql` 直接建立.

loadingKey指定data中建立的字段, 用於表示狀態, loadingKey應爲初始值爲0的整數. 處於加載狀態時會執行loadingKey++操做, 加載結束會執行loadingKey—操做.

mutation

隨時使用, 不須要提早聲明或定義. 請求結果爲一個promise.

this.$apollo.mutate({
      // Query
      mutation: gql`mutation ($label: String!) {
        addTag(label: $label) {
          id
          label
        }
      }`,
      // Parameters
      variables: {
        label: this.newTag,
      }
}).then(data=>{
    console.log(data)
}).catch(error=>{
    console.log(error)
})

並無mutation(){return gql`} 這樣的操做, 因此計算屬性須要經過 variables`傳入. 固然這種方法也適用於query.

數據更新

通常的, 在組件建立時會發起一次query請求. 若是須要從新請求數據:this.$apollo.queries.<$name>.refetch()

this.$apollo.queries.hello.refetch() 請求指定字段.

請求發起後loadingKey也將從新計算.

相關文章
相關標籤/搜索