Moshe Shmukler
Moshe Shmukler

Reputation: 1300

Function export is not working as expected

Have the below:

module.exports = function() {
  const LOGIN_URL = 'https://localhost/admin/login'
  const URL_ROOT = 'http://localhost/managed/'

  function createHeaders() {
    return {
      Accept: 'application/json',
     'Content-Type': 'application/json',
      Authorization: 'Basic RW1....M='
    }
  }

  function login(userInfo) {
    return post(LOGIN_URL, userInfo)
  }

  function listRoles() {
    return get(URL_ROOT + 'role?_queryFilter=true')
  }

  return {
    login,
    listRoles,
    ...
  }
}

Import this with a require() and stick inside my GraphQL resolver, as required by the app architecture.

Login works for fine, but when I try to do api.listRoles().then(...) it comes-up with an error: GraphQLError: api.listRoles is not a function.

What is wrong with my export

Upvotes: 0

Views: 49

Answers (1)

Ishan Thilina Somasiri
Ishan Thilina Somasiri

Reputation: 1244

Use the following format.

module.exports = {
    LOGIN_URL: 'https://localhost/admin/login',
    URL_ROOT: 'http://localhost/managed/',

    createHeaders: function () {
        return {
            Accept: 'application/json',
            'Content-Type': 'application/json',
            Authorization: 'Basic RW1....M='
        }
    },
    login: function () {
        return post(LOGIN_URL, userInfo);
    },
    listRoles: function () {
        return get(URL_ROOT + 'role?_queryFilter=true');
    }
};

Upvotes: 1

Related Questions