错误:由于未定义类型“查询”,因此无法扩展

时间:2019-10-23 05:41:23

标签: node.js typescript graphql apollo apollo-server

在GraphQL模式中扩展查询时出现此错误。这是错误堆栈:

stack=[Error: Cannot extend type "Query" because it is not defined.,     at assertValidSDL (/NodeGraphQL/node_modules/graphql/validation/validate.js:108:11),     
at Object.buildASTSchema (/NodeGraphQL/node_modules/graphql/utilities/buildASTSchema.js:71:34),     
at Object.buildSchemaFromTypeDefinitions (/NodeGraphQL/node_modules/graphql-tools/dist/generate/buildSchemaFromTypeDefinitions.js:23:28),     
at Object.makeExecutableSchema (/NodeGraphQL/node_modules/graphql-tools/dist/makeExecutableSchema.js:26:29),     
at ApolloServer.initSchema (/NodeGraphQL/node_modules/apollo-server-core/dist/ApolloServer.js:272:49),     
at new ApolloServerBase (/NodeGraphQL/node_modules/apollo-server-core/dist/ApolloServer.js:202:30),     
at new ApolloServer (/NodeGraphQL/node_modules/apollo-server-express/dist/ApolloServer.js:59:9),     
at Object.<anonymous> (/NodeGraphQL/dist/server.js:17:16),     
at Module._compile (internal/modules/cjs/loader.js:689:30),     at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10),     
at Module.load (internal/modules/cjs/loader.js:599:32),     at tryModuleLoad (internal/modules/cjs/loader.js:538:12),     
at Function.Module._load (internal/modules/cjs/loader.js:530:3),     at Function.Module.runMain (internal/modules/cjs/loader.js:742:12),     
at startup (internal/bootstrap/node.js:283:19),     at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)]

这是我的架构的样子:

import { gql } from 'apollo-server-express';

export const ticket = gql`
  type Ticket {
    id: ID!
    subject: String
    priority_id: Int
    status_id: Int
    user_id: Int
    assigned_to_user_id: Int
  }

  extend type Query {
    tickets: [Ticket]
    ticket(id: ID!): Ticket
  }
`;

寻找解决方案:)

1 个答案:

答案 0 :(得分:1)

extend关键字用于扩充同一模式中的现有类型。例如,我们可以这样做:

type User {
  username: String!
}

extend type User {
  firstName: String!
  lastName: String!
}

在将架构拆分为多个文件或从基本架构构建架构时,这很有用。

但是,您不能扩展不存在的类型。您尚未在架构中定义Query类型,因此无法扩展它。只需删除extend关键字即可。