Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js “如何存储”;阿波罗取回“;变量中的graphql响应?_Node.js_Graphql_Apollo_Apollo Client - Fatal编程技术网

Node.js “如何存储”;阿波罗取回“;变量中的graphql响应?

Node.js “如何存储”;阿波罗取回“;变量中的graphql响应?,node.js,graphql,apollo,apollo-client,Node.js,Graphql,Apollo,Apollo Client,我创建了以下代码以从graphql响应返回标题、名字和姓氏。我正在使用这些返回值进行自动测试,以确认它们的值: const { createApolloFetch } = require('apollo-fetch'); const uri = 'http://localhost:3000'; const query = `{ Post(id: 1) { id title forename

我创建了以下代码以从graphql响应返回标题、名字和姓氏。我正在使用这些返回值进行自动测试,以确认它们的值:

const { createApolloFetch } = require('apollo-fetch');

const uri = 'http://localhost:3000';

const query = `{
    Post(id: 1) {
            id
            title
            forename
            surname
    }
}
`;

const apolloFetch = createApolloFetch({ uri });

const returnApolloBody = () => {
    return apolloFetch({query}).
    then(res => res.data)
        .catch(err => {
            throw new Error(err);
        });
};

const responseForename = returnApolloBody().then(result => result.Post.forename);
const responseTitle = returnApolloBody().then(result => result.Post.title);
const responeSurname = returnApolloBody().then(result => result.Post.surname);

module.exports = {
    responseForename: responseForename,
    responseTitle: responseTitle,
    responeSurname: responeSurname,
};
然而,我意识到这不是最有效的方法,因为我正在为每个值设置一个单独的http请求。我认为将整个响应存储为JSON值会容易得多,然后从这个变量中提取值,但是我运气不太好。我有以下代码,返回的值为未定义:

const { createApolloFetch } = require('apollo-fetch');

const uri = 'http://localhost:3000';

const query = `{
    Post(id: 1) {
            id
            title
            forename
            surname
    }
}
`;

const apolloFetch = createApolloFetch({ uri });

const returnApolloBody = () => {
    return apolloFetch({query}).
    then(res => res)
        .catch(err => {
            throw new Error(err);
        });
};

const response = returnApolloBody().then(response => response);

const forename = response.then(response => response.Post.forename);
const surname = response.then(response => response.Post.surname);
const title = response.then(response => response.Post.title);


module.exports = {
    forename: forename,
    title: title,
    surname: surname
};
我缺少什么来允许我将整个响应存储在一个常量中,然后在该常量中查询特定属性


提前感谢

您应该能够返回结果
响应。将
作为对象发布,例如下面我正在使用Apollo客户端获取用户配置文件

apolloclient.query({
query: q_my_profile}).then(
resp => {
    resolve(resp.data.my_profile);
},
error => {
    reject(error.message);
});