0

I copy-pasted example from official documentation in hope that I will see some different error (invalid URL or invalid auth key or something similar) However I get some webpack/sandbox error:

const fetch = require('isomorphic-fetch')
const Base64 = require('Base64')
const FormData =require('form-data')

const apiKey = '__MAILGUN_API_KEY__'
const url = '__MAILGUN_URL__'

export default event => {

  const form = new FormData()
  form.append('from', 'Nilan <nilan@graph.cool>')
  form.append('to', 'Nikolas <nikolas@graph.cool>')
  form.append('subject', 'Test')
  form.append('text', 'Hi')

  return fetch(url, {
    headers: {
      'Authorization': `Basic ${Base64.btoa(apiKey)}`
    },
    method: 'POST',
    body: form
  })
}

enter image description here

Even simple API requests fail:

require('isomorphic-fetch')

module.exports = function (event) {
  const url = 'https://jsonplaceholder.typicode.com/posts'
  return fetch(url)
}

The code above also returns:

TypeError: Converting circular structure to JSON

at Object.stringify (native)

at /data/sandbox/lib/sandbox.js:532:48

at /data/io/8e0059b3-daeb-4989-972f-e0d88e27d15e/webtask.js:46:33

at process._tickDomainCallback (node.js:481:9)

How do I successfully call API from custom graphcool subscription/resolver?

Michal
  • 4,952
  • 8
  • 30
  • 63

1 Answers1

0

This is the simplest working example:

require('isomorphic-fetch')

module.exports = function (event) {
  const url = 'https://jsonplaceholder.typicode.com/posts'
  return fetch(url)
    .then(res => res.json())
    .then(data => {
      console.log(data)
      return {
        data: {
          sum: 3  
        }
      }
    })
}
Michal
  • 4,952
  • 8
  • 30
  • 63