0

I am learning graphql. I stuck in fetching api info.

My schema is:

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}
    })
}); 

My main query schema is this:

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;
                    })
            }

        }

when i run on graphql server it returns null values where am i doing wrong??

1 Answers1

0

You have to return 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

Lafi
  • 1,310
  • 1
  • 15
  • 14