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也将从新计算.

相关文章
相关标签/搜索