阿波罗客户端导入到redux-saga

时间:2019-01-07 10:27:28

标签: reactjs graphql apollo redux-saga apollo-client

作为Apollo消费者(前端),我具有以下客户端设置:

import { ApolloClient } from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { HttpLink } from "apollo-link-http";
import { onError } from "apollo-link-error";
import { ApolloLink } from "apollo-link";

  const api = new ApolloClient({
    link: ApolloLink.from([
      onError(({ graphQLErrors, networkError }) => {
        if (graphQLErrors)
          graphQLErrors.map(({ message, locations, path }) =>
            console.log(
              `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
            )
          );
        if (networkError) console.log(`[Network error]: ${networkError}`);
      }),
      new HttpLink({
        uri: "https://api.graph.cool/simple/v1/cixmkt2ul01q00122mksg82pn",
        credentials: "same-origin"
      })
    ]),
    cache: new InMemoryCache()
  });

如果我在 redux传奇中内联使用此客户端,则可以正常运行,但是如果我尝试将其移动到单独的文件并将其导入到saga中,则会出现以下错误:

Error:
fetch is not found globally and no fetcher passed, to fix pass a fetch 
for your environment like https://www.npmjs.com/package/node-fetch.

For example:
import fetch from 'node-fetch';
import { createHttpLink } from 'apollo-link-http';

为什么可以内联获取而不是导入时可用?

更新:我通过如提到的错误中所建议的向HttpLink提供特定的提取程序来解决了此问题。

我没有回答这个问题,因为我的问题是关于为什么会发生而不是如何解决这个问题

0 个答案:

没有答案