无效的GraphQL架构

无效的GraphQL架构,graphql,Graphql,我有以下GraphQL模式: type User { id: String! email: String } input CreateUserDto { email: String! password: String! } input CredentialsDto { email: String! password: String! } type CreateUserResponseDto { id: String! } type TokenResponseD

我有以下GraphQL模式:

type User {
  id: String!
  email: String
}

input CreateUserDto {
  email: String!
  password: String!
}

input CredentialsDto {
  email: String!
  password: String!
}

type CreateUserResponseDto {
  id: String!
}

type TokenResponseDto {
  token: String!
}

type Mutation {
  signup(input: CreateUserDto!): CreateUserResponseDto!
}

type Query {
  user(id: Int!): User

  auth {
    login(credentials: CredentialsDto!): TokenResponseDto
  }
}
出于某种原因,我得到以下错误:

Syntax Error: Expected :, found {

GraphQL request (13:8)
12: 
13:   auth {
           ^
14:     login(credentials: CredentialsDto!): TokenResponseDto
Syntax Error: Expected Name, found {

GraphQL request (13:9)
12: 
13:   auth: {
            ^
14:     login(credentials: CredentialsDto!): TokenResponseDto

What am I doing wrong?
如果在auth属性后添加:则会出现以下错误:

Syntax Error: Expected :, found {

GraphQL request (13:8)
12: 
13:   auth {
           ^
14:     login(credentials: CredentialsDto!): TokenResponseDto
Syntax Error: Expected Name, found {

GraphQL request (13:9)
12: 
13:   auth: {
            ^
14:     login(credentials: CredentialsDto!): TokenResponseDto

What am I doing wrong?

定义架构时不能使用匿名对象。必须为auth字段创建单独的类型才能返回:

type Auth {
  login(credentials: CredentialsDto!): TokenResponseDto
}

type Query {
  user(id: Int!): User
  auth: Auth
}
假设您使用的是apollo server或graphql工具,那么您的解析器需要如下所示:

const resolvers = {
  Query: {
    user: () => {
      // TODO: resolve field
    }
    auth: () => ({}) // return an empty object
  },
  Auth: {
    login: () => {
      // TODO: resolve field  
    }
  }
}

需要记住的是,解析器对象只是一个类型名映射,每个类型名映射到另一个字段名映射。

是的,刚刚解决了这个问题。但是如何解析登录字段呢?我遇到了这样一个错误:Query.login是在解析器中定义的,而不是在schemaAs中。错误显示,您的查询类型中没有名为login的字段,因此您不能为它定义解析器。请参阅我的编辑。