如何在graphql中获取查询?

时间:2018-01-10 12:56:42

标签: graphql express-graphql

我正在学习graphql。我坚持拿api信息。

我的架构是:

const particularUser = new GraphQLObjectType({
    name: "particularUser",
    description: "particular user field for person",
    fields: () => ({
        "id": {type: GraphQLInt},
        "first_name":  {type: GraphQLString},
        "last_name": {type: GraphQLString},
        "avatar": {type: GraphQLString}
    })
}); 

我的主要查询架构是:

specificUser:{
            type:particularUser,
            description:"fetching particular user for specific id",
            args:{
                id:{
                    type:new GraphQLNonNull(GraphQLString)
                }
            },
            resolve:(_,{id}) => {
                console.log({id})
                const url = `https://reqres.in/api/users/${id}`;
                console.log(url)
                return axios.get(url).then((response) => {
                    console.log(response.data)
                        return response.data;
                    })
                    .catch((error) => {
                        return error;
                    })
            }

        }

当我在graphql服务器上运行时,它返回空值我在哪里做错了?

1 个答案:

答案 0 :(得分:0)

您必须返回 response.data.data

const specificUser = {
    type: ParticularUser,
    args: { id: { type: new GraphQLNonNull(GraphQLString) } },
    description: 'fetching particular user for specific id',
    resolve: (_, { id }) =>
      axios
        .get(`https://reqres.in/api/users/${id}`)
        .then(response => response.data.data)
        .catch(error => error),
};

enter image description here