Git Product home page Git Product logo

v-graphql's Introduction

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');

v-graphql's People

Contributors

aminnazarii avatar rtler avatar ma7 avatar azadehmn avatar

Stargazers

Amir Rahimzade avatar  avatar VahiD Davari avatar  avatar  avatar

Watchers

 avatar

Forkers

rtler ma7 azadehmn

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.