v-graphql

vue package for graphql requests

Usage no npm install needed!

<script type="module">
  import vGraphql from 'https://cdn.skypack.dev/v-graphql';
</script>

README

v-graphql

graphql client for VueJs and javascript

github page

npm page

based on

Installation

You can install it via yarn or NPM.

$ npm i v-graphql
$ yarn add v-graphql

Usage

import Vue from 'vue';
import grahpql from 'v-graphql';
window.$graphql = graphql;

Headers

graphql.axios.defaults.baseURL = 'http://127.0.0.1:8080';
graphql.axios.defaults.headers.common['Authorization'] = 'Access-token';
graphql.axios.defaults.headers.post['Content-Type'] = 'application/json';
graphql.axios.defaults.headers.post['Accept'] = 'application/json';
  • Use
Vue.use(grahpql);

Resource

Collection of your queries and mutation in a js file.

  • graphql.js
export default {
  query: {
    Users: `
    query fetchUsers {
      AllUsers: Tickets {
        id,
        name
      }
    }`,
  },

  mutation: {
    addComment: `
      mutation addComment {
        CommentAdd(text: $text) {
          text,
          likes
        }
      }
    `,
  }
};

graphql.js file must contain two section (query, mutation) and set your queries in query and mutations in mutation key.

  • Notice: you have to create graphql.js in your needed place with ideal name.

  • Use graphql.js

this.$graphql.setResources(require('./graphql.js').default);

available methods

  • query
  • mutation

Make request

  • Query example
this.$graphql.query('Users')
  .then(function (response) {
    console.log(response);
  }).catch(function (error) {
    console.log(error);
  });
  • Mutation example
let variables = {text: "i like this package"};
this.$graphql.mutation('addComment', {variables: variables})
  .then(function (response) {
    console.log(response);
  }).catch(function (error) {
    console.log(error);
  });

also

  • you can pass manually query to the method like below
let variables = {text: "i like this package"};
let queryString = `
mutation addComment($text: String) {
  comment: Comment(text: $text) {
    id
    title
  }
}
`;

this.$graphql.mutation({query: queryString, variables: variables})
  .then(function (response) {
    console.log(response);
  }).catch(function (error) {
    console.log(error);
  });

More details

if you use another schema except default schema you can define your schema in your request

  • like this
let variables = {text: "i like this package"};
this.$graphql.mutation('addComment', {variables: variables, schema: 'custome_schema'})
  .then(function (response) {
    console.log(response);
  }).catch(function (error) {
    console.log(error);
  });
  • set custom prefix for graphql url by default is 'graphql' if you need change it use below code
this.$graphql.setPrefix('api_grahpql');