使用 graphql 时是否必须将响应数据发送到 dto?

Is it essential to send response data to dto when working with graphql?

您好,我正在使用 nestjs 和 graphql 创建 api 服务器。

我有一个问题。

当api服务器将响应传递给上面的堆栈时。

  1. 将响应加载到dto中是否正确?
  2. 如果第一个问题是正确的,最好的方法是什么?
  3. 还是graphql schema类型起到了dto的作用?

告诉我谢谢!

A DTO is an object that helps developers and consumers know what shape the data is going to be in as it goes "over the wire" (when the request or response is made). In GraphQL we use schemas to be our DTOs and follow the GraphQL Query Language spec 以便我们的 GraphQL 服务器知道如何根据我们创建的 shcema 反序列化传入请求和序列化传出请求。最后 唯一 重要的是数据是正确的形状^,而不是我们为服务器创建的 class 的实例。使用 NestJS,这取决于您采用代码优先还是模式优先方法,但通常在代码优先方法中,可以说您的 GQL 模式 您的 DTO。从那里开始,只要数据 看起来像 DTO 你应该没问题。

^ 不包括嵌套模式的使用,如果您尝试 return 原始 JSON 而没有其他

,嵌套模式可能会出现问题