4

在 express-graphql 应用程序中,我有一个userLogin像这样的解析器:

const userLogin = async ({ id, password }), context, info) => {

    if (!id) {
      throw new Error('No id provided.')
    }

    if (!password) {
      throw new Error('No password provided.')
    }

    // actual resolver logic here
    // … 
}

如果用户不提供idAND a password,它只会抛出一个错误。

{
  "errors": [
    {
      "message": "No id provided.",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "userLogin"
      ]
    }
  ],
  "data": {
    "userLogin": null
  }
}

怎么可能在errors响应数组中抛出多个错误?

4

1 回答 1

9

没有办法在 JavaScript 中抛出一系列错误,或者让一个解析器拒绝多个错误。GraphQL 响应包括一个errors数组而不仅仅是一个对象,因为当这些错误源自不同字段时error,总响应可能包括多个错误。考虑这个模式和解析器:

type Query {
  a: String
  b: String
  c: String
}

const resolvers = {
  Query: {
    a: () => { throw new Error('A rejected') },
    b: () => { throw new Error('B rejected') },
    c: () => 'Still works!',
  },
}

如果您查询所有三个字段...

查询 { a b c }

您的数据将如下所示:

{
  "errors": [
    {
      "message": "A rejected",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "a"
      ]
    },
    {
      "message": "B rejected",
      "locations": [
        {
          "line": 3,
          "column": 3
        }
      ],
      "path": [
        "b"
      ]
    }
  ],
  "data": {
    "a": null,
    "b": null,
    "c": "Still works!"
  }
}

这是因为 GraphQL 支持部分响应。但是,请记住,这是有效的,因为这些字段可以为空。如果它们不为空,则这些错误将冒泡到最近的可为空的父字段

以下是一些替代方法:

您可以利用formatError更改 GraphQL 返回的错误如何显示给客户端。这意味着您可以在错误中包含任何类型的额外信息,例如错误代码或多个错误消息。一个简单的例子:

// The middleware
app.use('/graphql', graphqlExpress({
    schema: schema,
    formatError: (error) => ({
      message: error.message,
      path: error.path,
      locations: error.locations,
      errors: error.originalError.details
    })
}))

// The error class
class CustomError extends Error {
  constructor(detailsArray) {
    this.message = String(details)
    this.details = details
  }
}

// The resolver
const userLogin = async ({ id, password }), context, info) => {
    const errorDetails = []
    if (!id) errorDetails.push('No id provided.')
    if (!password) errorDetails.push('No password provided.')
    if (errorDetails.length) throw new CustomError(errorDetails)

    // actual resolver logic here
}

您的回复看起来更像这样:

{
  "errors": [
    {
      "message": "[No id provided.,No password provided.]",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "userLogin"
      ]
      "errors" [
        "No id provided.",
        "No password provided."
      ]
    }
  ],
  "data": {
    "userLogin": null
  }
}

也就是说,在返回 GraphQL 验证错误的同时返回面向用户的错误消息有点令人讨厌。一些 API 采用的另一种方法是errors在实际突变响应旁边包含一个字段。例如:

type Mutation {
  userLogin: UserLoginResponse
}

type UserLoginResponse {
  response: User
  errors: [String!]
}

你也可以使用联合来达到类似的效果:

type Mutation {
  userLogin: UserLoginResponse
}

type Errors {
  errors: [String!]!
}

union UserLoginResponse = User | Errors
于 2018-09-27T11:56:34.420 回答