在阿波罗响应中添加来自服务器的消息Node.js

问题描述

当您的api宁静时,我将发送如下响应:

{
data,code: 1,msg: 'This is the messages'
}

有了这样的响应,如果代码= 1,我可以显示成功消息;如果代码= 2,我可以显示错误消息。 但是当我尝试使用graphql(apollo响应)...我不知道该怎么做...来自apollo响应的响应只有这样在Schema中定义的数据...我无法添加任何其他内容

{
  "data": {
    "getBanners": [
      {
        "link": "example.com","avatar": null
      },{
        "link": "example.com","avatar": "example.com/0_banner_1597917871_3211.jpg"
      },{
        "link": "example.com/16709365405930105","avatar": "example.com/0_banner_1597917938_5116.jpg"
      }
    ]
  }
}

我想要像我休息时一样的反应。怎么做。请帮助我

解决方法

您可以在GraphQL模式中定义一个返回类型:

type GetBannersResponse {
  data: [Banners]
  code: Int!
  msg: String
}

然后在执行查询时将其定义为结果:

type Query{
  getBanners: GetBannersResponse
}

现在,您应该能够请求所有想要的对象,并且在查询的解析器中,您可以创建一个包含数据的对象。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...