Apollo-link-rest: 突变原因:ERROR 错误:网络错误:全局未定义

创建于 2019-07-12  ·  5评论  ·  资料来源: apollographql/apollo-link-rest


目前使用 Angular 7,apollo-rest 链接 0.7.3

我终于让突变部分工作,并且存在一个问题,即global未初始化并导致此错误:

core.js:15724 ERROR Error: Network error: global is not defined
    at new ApolloError (bundle.esm.js:60)
    at Object.error (bundle.esm.js:1032)
    at notifySubscription (Observable.js:134)
    at onNotify (Observable.js:165)
    at SubscriptionObserver.error (Observable.js:224)
    at Object.error (Observable.js:463)
    at notifySubscription (Observable.js:134)
    at onNotify (Observable.js:165)
    at SubscriptionObserver.error (Observable.js:224)
    at bundle.umd.js:807

我初始化客户端

const restLink = new RestLink({
    uri: 'http://localhost:5000/api',
    credentials: 'same-origin',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json'
    }
  });
  return {
    link: restLink,
    cache
  };

我有这个突变:

export const addTodo = gql`
    mutation addTodo($input: Todo!){
      addTodo(input: $input) @rest( type: "Todo", method: "POST", path: "/todos" ) {
       ...todoFragment
      }
  }
  ${todoFragment}
`;

我是这样执行的:

 add(text: string): void {

    this.apollo.mutate({
      mutation: addTodo,
      variables: {
        input: {
          description: text,
          completed: false
        }
      }
    }).subscribe(({data}) => {
      console.log('got data', data);
    }, (error) => {
      console.log('there was an error sending the query', error);
    });

导致错误的行在图片下方

Screen Shot 2019-07-12 at 12 17 35 PM

知道为什么这个global.FileList全局对象是未定义的吗?

// FileList/File are only available in some browser contexts
        // Notably: *not available* in react-native.
        if ((global.FileList && object instanceof FileList) ||
            (global.File && object instanceof File)) {
            // Object is a FileList or File object => no keys to convert!
            return object;
        }
help wanted 🛠 question❔

所有5条评论

这似乎是最初的 angular-cli 问题,因为我不得不在 polyfills.ts 中添加一行

https://github.com/angular/angular-cli/issues/8160
但对此的回应是: globals does not exists in browser and should not be in the client-side code

它应该在 REST LINK 中吗?

这对我来说是个新闻,在 React-Native 和其他环境中, global确实存在吗? 在浏览器中, global对象是窗口,所以你可以做一些类似的事情: window.global = window来保证它会存在?

我所做的事情是在 Angular 上建议的,但他们认为这很糟糕。 这就是我在底部所做的:

https://github.com/fkolar/todo-rest/blob/master/src/polyfills.ts

https://github.com/fkolar/todo-rest

我同意这是一个 hack,但只有你知道你的代码是应该在浏览器中运行还是在 react-native 上运行。 如果您知道/可以链接到编写 Apollo-link-rest 的更好方法,我会支持更改! 不幸的是window在移动设备上不存在,所以我们有点卡在这里!

我刚碰到这个问题..已经有解决方案了吗?

此页面是否有帮助?
0 / 5 - 0 等级