无效的GraphQL模式

时间:2019-02-13 21:47:29

标签: 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 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

如果我将在:属性之后添加auth,则会收到以下错误消息:

Syntax Error: Expected Name, found {

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

What am I doing wrong?

1 个答案:

答案 0 :(得分:1)

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

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

type Query {
  user(id: Int!): User
  auth: Auth
}

假设您使用的是apollo-servergraphql-tools,那么您的解析器需要看起来像这样:

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

要记住的是,resolvers对象只是一个类型名称的映射,每个类型名称都映射到另一个字段名称映射。