繁体   English   中英

获取 apollo-rest-datasource 发布请求的 Response-Body

[英]Get Response-Body of apollo-rest-datasource post request

如何获取 Apollo REST-Datasource 模块提交的 Post-Request 的响应体?

数据接口:

async postEntry(body) {
return this.post(
  "/domain.com/myService",
  body
).catch((err) => console.log(err));

解析器:

  Mutation: {
    addEntry: async (_source, { entry }, { dataSources }) => {
    const response = await dataSources.dataAPI.postEntry(entry);
    return response;
},

响应仅包含不同的标头。 与响应包含预期正文的简单 Get-Request 不同。

参考: NPM 站点 apollo-datasource-rest

可以通过覆盖RESTDataSourcedidReceiveResponse方法来获取响应 object 的标头。

这是一个代码片段,如果您只关心标题:

async didReceiveResponse(response, _request) {
    // Extract the required headers from response and return them
    return {
        header_name: response.headers.get('header_name'),
        // ...
    };
}

或者,如果您希望将标头与默认响应放在一起:

async didReceiveResponse(response, _request) {
    return super.didReceiveResponse(response, _request).then((defaultReturnValue) => {
        return {
        ...defaultReturnValue,
        headers: {
            header_name: response.headers.get('header_name'),
        },
        };
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM