无法从Apollo客户端的非Apollo GraphQL服务器进行查询

时间:2016-05-05 11:17:32

标签: graphql apollo

我尝试使用Apollo Client向非Apollo GraphQL服务器发出请求,如下所示:

import ApolloClient, { createNetworkInterface } from 'apollo-client';

// Register gql globally
import { registerGqlTag } from 'apollo-client/gql';
registerGqlTag();

const networkInterface = createNetworkInterface(' http://localhost:8080/graphql', {
  headers: {
  'Content-type': "application/json"
  }
});

var client = new ApolloClient({
  networkInterface
});

client.query({
  query: gql`
    query getTodo($todoId: Int!) {
      node(id: $todoId) {
        ... on Todo {
          id
          text
        }
      }
    }
  `,
  variables: {
    todoId: "todo:100000",
  },
  forceFetch: false,
}).then((graphQLResult) => {
  var errors  = graphQLResult.errors;
  var data = graphQLResult.data;

  if (data) {
    console.log('got data', data);
  }

  if (errors) {
    console.log('got some GraphQL execution errors', errors);
  }
}).catch((error) => {
  console.log('there was an error sending the query', error);
});

但是我在运行代码时遇到错误: there was an error sending the query [ReferenceError: fetch is not defined]

我尝试搜索与此相关的问题,但文档并没有真正帮助。

先谢谢。

2 个答案:

答案 0 :(得分:3)

在幕后,Apollo客户端使用fetch这是围绕发出http请求的新标准。它存在于大多数主流浏览器中,但在节点或IE / Edge中找不到它。该团队致电fetch,因为它的采用率正在增长,并且旧版浏览器和节点的polyfill可用。

我们在此处针对此问题的文档添加了一个部分:http://docs.apollostack.com/apollo-client/core.html#fetch-polyfill

要了解有关获取提取的决定,请查看此问题:https://github.com/apollostack/apollo-client/pull/126

答案 1 :(得分:1)

如果您在节点环境中工作,则需要将提取添加到Error in setup_twitter_oauth(consumerKey, consumerSecret, access_token, : could not find function "setup_twitter_oauth" 对象:

global