TypeError:可能类型[超类型] .forEach不是一个函数

时间:2020-07-27 17:56:55

标签: reactjs graphql apollo apollo-client

这是我的代码。

import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client";
import { createHttpLink } from "apollo-link-http";
import { render } from "react-dom";
import React from "react";

import RouteCollector from "./routeCollector";

import "./index.css";
import possibleTypes from "./tools/graphqlCodeGenerator/possibleTypes.json";

const cache = new InMemoryCache({
  possibleTypes,
});

const link = createHttpLink({
  uri: "http://localhost:8000/graphql",
  credentials: "include",
});

const client = new ApolloClient({
  cache,
  link,
});

render(
  <ApolloProvider client={client}>
    <RouteCollector />
  </ApolloProvider>,
  document.getElementById("root")
);

我正在使用软件包" @ apollo / client ":" ^ 3.0.2 ""apollo-link-http": "^1.5.17", "graphql": "^15.3.0""react": "^16.13.1",

运行程序时出现错误

TypeError:可能的类型[超类型]。forEach不是函数

如何解决该错误?

1 个答案:

答案 0 :(得分:1)

如果您使用的是GraphQL代码生成器,则应确保使用的是possibleTypes插件生成的对象的fragment-matcher属性,如下所示:

import introspectionResult from '../introspection-result';

const cache = new InMemoryCache({
  possibleTypes: introspectionResult.possibleTypes,
});

您不能传递所生成的整个对象。

您还需要确保在代码生成配置中将apolloClientVersion设置为3,以确保生成正确的对象。