GraphQL returning null data from axios [duplicate]

梦想的初衷 提交于 2019-11-29 13:08:07
Waweru wa Kamau

I struggled with the exact same issue sometime back. I realized that the data from the REST API / JSON file take some time to be fetched and GraphQL doesn't wait to receive it. So I decided to use Async and Await, and that way, GraphQL waited for the data to be received. In your case, the code would look like:

const RootQuery = new GraphQLObjectType({
name:'RootQueryType',
fields: {
    holiday: {
        type: Holiday,
        args:{
            date:{type:GraphQLString},
        },
        Async resolve(parentValue, args) {
            const results= await axios.get('http://localhost:3000/holidays?date='+ args.date)
           .then(res => res.data);
           return results;
        }
    },
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!