简体   繁体   English

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

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

how is it possible to get the Response-Body of a Post-Request submitted by the Apollo REST-Datasource module?如何获取 Apollo REST-Datasource 模块提交的 Post-Request 的响应体?

dataApi:数据接口:

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

Resolvers:解析器:

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

The response only contains different headers.响应仅包含不同的标头。 Different to a simple Get-Request where the response contains the body as expected.与响应包含预期正文的简单 Get-Request 不同。

Reference: NPM Site apollo-datasource-rest参考: NPM 站点 apollo-datasource-rest

Headers from the response object can be obtained by overriding the RESTDataSource 's didReceiveResponse method.可以通过覆盖RESTDataSourcedidReceiveResponse方法来获取响应 object 的标头。

Here's a code snippet, if you only care about the headers:这是一个代码片段,如果您只关心标题:

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

Or if you want the headers alongside the default response:或者,如果您希望将标头与默认响应放在一起:

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