Apollo makeExecutableSchema引发错误“无法读取未定义的属性'种类'”

时间:2018-07-04 12:29:59

标签: javascript graphql apollo apollo-server

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

  

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

     

->如果(typeDef.kind!==未定义)

     

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时总是出现相同的错误。

1 个答案:

答案 0 :(得分:2)

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

constructor(options)

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

我用您提供的示例进行了尝试,它很有效:)