【问题标题】:TypeError: possibleTypes[supertype].forEach is not a functionTypeError: possibleTypes[supertype].forEach 不是函数
【发布时间】:2020-11-17 02:43:28
【问题描述】:

这是我的代码。

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 不是函数

如何修复错误?

【问题讨论】:

  • 代码的哪一行抛出了这个错误?
  • @RezaBehzadpour const cache = new InMemoryCache({possibleTypes,})

标签: reactjs graphql apollo apollo-client


【解决方案1】:

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    • 2020-06-29
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多