【问题标题】:Apollo makeExecutableSchema throws error "Cannot read property 'kind' of undefined"Apollo makeExecutableSchema 抛出错误“无法读取未定义的属性‘种类’”
【发布时间】:2018-12-12 21:37:56
【问题描述】:

我正在尝试使用 apollo 进行模式拼接,但每次使用 makeExecutableSchema 时都会出错。错误如下:

../node_modules/graphql-tools/dist/generate/concatenateTypeDefs.js:9:

-> if (typeDef.kind !== undefined)

TypeError: 无法读取未定义的属性“种类”

即使只是复制 Apollo 的 website 上的基本示例,我也重现了该问题

const { ApolloServer, gql, makeExecutableSchema } = require("apollo-server");
const { addMockFunctionsToSchema, mergeSchemas } = require("graphql-tools");

const chirpSchema = makeExecutableSchema({
  typeDefs: `
      type Chirp {
        id: ID!
        text: String
        authorId: ID!
      }

      type Query {
        chirpById(id: ID!): Chirp
        chirpsByAuthorId(authorId: ID!): [Chirp]
      }
    `
});

addMockFunctionsToSchema({ schema: chirpSchema });

const authorSchema = makeExecutableSchema({
  typeDefs: `
      type User {
        id: ID!
        email: String
      }

      type Query {
        userById(id: ID!): User
      }
    `
});

addMockFunctionsToSchema({ schema: authorSchema });

const schema = mergeSchemas({
  schemas: [chirpSchema, authorSchema]
});

const server = new ApolloServer(schema);

server.listen().then(({ url }) => {
  console.log(`???? Server ready at ${url}`);
});

我做错了什么?但是我尝试使用 makeExecutableSchema 时总是遇到同样的错误。

【问题讨论】:

    标签: javascript graphql apollo apollo-server


    【解决方案1】:

    据我所知,您正在使用 apollo-server@2.0.0-rc 在这个版本中 ApolloServer 构造函数接收一个 options 参数

    constructor(options)

    你应该通过new ApolloServer({ schema: schema }) 而不是new ApolloServer(schema)

    我用你给出的例子试过了,它奏效了:)

    【讨论】:

      猜你喜欢
      • 2021-08-04
      • 2018-07-03
      • 2018-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      相关资源
      最近更新 更多