import axios from 'axios';
import _ from 'lodash'
import store from '../store'



const graphql = (query, variables, callbacks)=>{

  const auth = store.getState().auth

  const config = {
    //"access-token": auth.accessToken,
    //"token-type":   "Bearer",
    //"client":       auth.client,
    //"expiry":       auth.expiry,
    //"uid":          auth.uid,
    "authorization": auth.jwt
  }

  axios.create({
    baseURL: '/graphql',
  }).post('', {
    query: query,
    variables: variables,
    
  }, {headers: config})
  .then( r => {
    const data = r.data.data
    const res = r
  
    const errors = r.data.errors //? r.data.errors[0].message : null
    // get first key of data and check if has errors
    //const errors = data[Object.keys(data)[0]].errors || r.data.errors

    if (_.isObject(errors) && !_.isEmpty(errors)) {
      //const errors = data[Object.keys(data)[0]];
      //callbacks['error'] ? callbacks['error'](res, errors['errors']) : null
      if(callbacks['error'])
        return callbacks['error'](res, errors)
    }

    callbacks['success'] ? callbacks['success'](data, res) : null
  })
  .catch( r => {
    //throw r
    //const res = r.response
    callbacks['fatal'] ? callbacks['fatal'](r) : null
  })
  .then( () => {
    callbacks['always'] ? callbacks['always']() : null
  });
}

export default graphql

@@ -72,16 +72,140 @@
 /store'%0A
+import %7BerrorMessage, successMessage%7D from '../actions/status_messages'%0Aimport %7BexpireAuthentication%7D from '../actions/auth'
 %0A%0A%0Aconst
@@ -1251,19 +1251,30 @@
  .catch(
+(
  r
- 
+eq, error )
 =%3E %7B%0A   
@@ -1313,16 +1313,318 @@
 esponse%0A
+    switch (req.response.status) %7B%0A      case 500:%0A        store.dispatch(errorMessage(%22server error ocurred%22))%0A        break;%0A      case 401:%0A        store.dispatch(errorMessage(%22session expired%22))%0A        store.dispatch(expireAuthentication())%0A        break;%0A      default:%0A        break;%0A    %7D%0A    %0A
     call
@@ -1659,16 +1659,20 @@
 fatal'%5D(
+erro
 r) : nul
@@ -1688,16 +1688,17 @@
 .then( (
+r
 ) =%3E %7B%0A 

Update client.js