由于未定义self,用于调用graphql简单查询的Browserized js文件给出错误

时间:2019-01-29 16:53:38

标签: node.js graphql browserify

我已经编写了一个简单的js文件,该文件调用了graphql查询 testFunction 。我需要从浏览器中运行它,但是由于js文件具有require()模块,因此我试图对js文件进行浏览器化,并在html页面的脚本标签中使用browserized文件。未浏览器的文件给了我预期的结果,而被浏览器的文件给我抛出了错误

  

module.exports = self.fetch.bind(self);

     

ReferenceError:未定义自我

fetch.js文件

require('es6-promise').polyfill();
var fetch=require('isomorphic-fetch').fetch;

const { createApolloFetch } = require('apollo-fetch');
const fetchQL = createApolloFetch({
uri: 'http://localhost:4000/graphql',
});

fetchQL({
query: '{testFunction}',
}).then(res => {
console.log(res.data.testFunction);
});

模式

type myQuery {
    testFunction: String!
}

解析器

myQuery:{
    testFunction(){
        return "returned from custom test function";
    }

当我使用命令node fetch.js运行fetch.js文件时,我得到的预期输出为returned from custom test function

使用命令browserify fetch.js -o bundle.js浏览fetch.js文件并运行bundle.js文件后,出现未定义自身错误

请帮助我了解我要去哪里哪里以及如何纠正此问题。

0 个答案:

没有答案