2

Im working on fetching data from a GraphQL Server and I'm attempting to implement ES7 Async functions through babel. I'm currently receiving undefined in the console and I'm not sure what I'm doing wrong.

import fetch from 'isomorphic-fetch';
/**
 * [transport creates call to server with isomorphic-fetch]
 * @param  {[String]} path        [url to hit with request]
 * @param  {[Object]} query       [The GraphQL query/mutation]
 * @param  {[Object]} queryParams = {} [Params to pass into query]
 * @return {[Promise]}            [Promise containing payload]
 */
 //function that returns a promise
 export function transport (path, query, queryParams = {}) {
     return new Promise ((resolve, reject) => {
       return fetch(path, {
             method: 'POST',
             headers: {
                 'Accept': 'application/json',
                 'content-type': 'application/json'
             },
             body: JSON.stringify({
                 query,
                 queryParams
             })
         })
         .then(res => res.json())
         .then(response => {
           if(response.errors) {
             return error(response.errors);
           }
           return resolve(response.data);
         })
         .catch(error);
     });
 }
import { transport } from './utils/transport.js';

/**
 * [reachGraphQL Makes queres or mutations against GraphQL]
 * @param  {[String]} path        [path to the GraphQL server]
 * @param  {[Object]} query       [The query that GraphQL will use to fetch your data]
 * @param  {[object]} queryParams =  {} [Should contain object with different query params]
 * @return {[Object]}             [Data that was queried or mutated]
 */
//Heres Where I'm awaiting a promise from the transport function
export function reachGraphQL (path, query, queryParams = {}) {
  async () => {
    try{
      let response = await transport(path, query, queryParams);
      return response;
    } catch (error) {
      console.log(error)
    }
  }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
kennet postigo
  • 525
  • 1
  • 5
  • 17

1 Answers1

1

Your reachGraphQL just defines an async arrow function but doesn't do anything with it. And it doesn't return anything. Rather, it should be async itself:

export async function reachGraphQL (path, query, queryParams = {}) {
    try {
        return await transport(path, query, queryParams);
    } catch (error) {
        console.log(error)
    }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • They causes the following: bundle.js:53 Promise {_d: Object} bundle.js:125 ReferenceError: error is not defined(…) but doesn't respond with the retrieved information. – kennet postigo Jan 30 '16 at 14:22
  • As stated in the comments, `.catch(error);` is a problem. – Bergi Jan 30 '16 at 15:28