Print GraphQL query logs and integrate DataLoader

Print GraphQL query logs and integrate DataLoader


0

I have been trying to log out my query logs to see how many queries my query is performing to get the feeling why to use DataLoader.

The project im working on is a star wars GraphQL api , and i noticed bad performance
when doing such a query:

query {
  film(id: 2){
    title
    charecters {
      name
    }
  }
}

so becuase it is a nested query i wanted to print out the logs to see it in action.
And secondly , i coundnt success adding DataLoader to this part(or any part of the project) i would love to get some help on that.

here is my film.js file:

const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLInt,
  GraphQLList,
} = require("graphql");

const specieType = require("./specie"); //Next - resolve circul module problem
const starshipType = require("./starship");
const vehicleType = require("./vehicle");
const planetType = require("./planet");

const { getFilteredData, dataLoaderResolver } = require("../util/loader");

const filmType = new GraphQLObjectType({
  name: "Film",
  fields: () => {
    const charecterType = require("./charecter");
    return {
      title: {
        type: GraphQLString,
      },
      episode_id: {
        type: GraphQLInt,
      },
      opening_crawl: {
        type: GraphQLString,
      },
      director: {
        type: GraphQLString,
      },
      producer: {
        type: GraphQLString,
      },
      release_date: {
        type: GraphQLString,
      },
      characters: {
        type: new GraphQLList(charecterType),
        resolve: (film, args) => {
          return getFilteredData(film.characters);
        },
      },
      planets: {
        type: new GraphQLList(planetType),
        resolve: (film, parent, args) => {
          return getFilteredData(film.planets);
        },
      },
      species: {
        type: new GraphQLList(specieType),
        resolve: (film, parent, args) => {
          return getFilteredData(film.species);
        },
      },
      starships: {
        type: new GraphQLList(starshipType),
        resolve: (film, parent, args) => {
          return getFilteredData(film.starships);
        },
      },
      vehicles: {
        type: new GraphQLList(vehicleType),
        resolve: (film, parent, args) => {
          return getFilteredData(film.vehicles);
        },
      },
      created: {
        type: GraphQLString,
      },
      edited: {
        type: GraphQLString,
      },
    };
  },
});

module.exports = filmType;

the file containing the getFilteredData function(loader.js):

const axios = require("axios");
const DataLoader = require("dataloader");

//TODO: My attempt on using DataLoader that didnt work 
const dataLoaderResolver = () => {
  return new DataLoader((urls) => {
    return axios.all(
      urls.map((url) => {
        return axios
          .get(url)
          .then((res) => {
            console.log("res.data inside dataLoaderResolver: ", res.data);
            return res.data;
          })
          .catch((err) =>
            console.log("error in axios.get in dataLoaderResolver", err)
          );
      })
    );
  });
};

const getFilteredData = (urls) => {
  // console.log("urls are : " , urls);
  return axios.all(
    urls.map((url) => {
      return axios
        .get(url)
        .then((res) => {
          return res.data;
        })
        .catch((err) => console.log("error fetching .get: ", err));
    })
  );
};


module.exports = {
  getFilteredData,
  dataLoaderResolver,
};

And the rootQuery file: (root-query.js)

const axios = require("axios");
const {
  GraphQLObjectType,
  GraphQLID,
  GraphQLSchema,
  GraphQLList,
} = require("graphql");

const filmType = require("./swapi-res/film");
const characterType = require("./swapi-res/charecter");
const specieType = require("./swapi-res/specie");
const starshipType = require("./swapi-res/starship");
const vehicleType = require("./swapi-res/vehicle");

module.exports = new GraphQLObjectType({
  name: "RootQuery",
  fields: () => ({
    film: {
      type: filmType,
      args: {
        id: { type: GraphQLID },
      },
      resolve: async (parent, args) => {
        const res = await axios.get(`https://swapi.dev/api/films/${args.id}/`);
        return res.data;
      },
    },

    films: {
      type: new GraphQLList(filmType),
      resolve: async (parent, args) => {
        try {
          const res = await axios.get("https://swapi.dev/api/films/");
          return res.data.results;
        } catch (err) {
          console.log("Inside errro");
          throw ("Error fetching all films", err);
        }
      },
    },

    character: {
      type: characterType,
      args: {
        id: {
          type: GraphQLID,
        },
      },
      resolve: async (parent, args) => {
        const res = await axios.get(`https://swapi.dev/api/people/${args.id}/`);
        return res.data;
      },
    },
    charecters: {
      type: new GraphQLList(characterType),
      resolve: async (parent, args, {rootValue}) => {
        try {
          const res = await axios.get("https://swapi.dev/api/people/");
          return res.data.results;
        } catch (err) {
          console.log("Inside errro");
          throw ("Error fetching all charecters", err);
        }
      },
    },
  }),
});

Thanks.


Load 6 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *