首页 > 解决方案 > TypeError: possibleTypes[supertype].forEach 不是函数

问题描述

这是我的代码。

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: possibleTypes[supertype].forEach 不是函数

如何修复错误?

标签: reactjsgraphqlapolloapollo-client

解决方案


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

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

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

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

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


推荐阅读