Apollo makeExecutableSchema 抛出错误 "Cannot read property 'kind' of undefined"

Apollo makeExecutableSchema throws error "Cannot read property 'kind' of undefined"

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

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

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

TypeError: Cannot read property 'kind' of undefined

即使只是复制 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 时总是遇到同样的错误。

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

constructor(options)

你应该传递 new ApolloServer({ schema: schema }) 而不是 new ApolloServer(schema)

我用你给出的例子试过了,它成功了:)