GraphQL

GraphQL Client

GraphQL Server


type CityWeather {
 temp: String
 min_temp: String
 max_temp: String
 city_name: String!
 applicable_date: String!
}
 
type Query {
  cityWeather(city_name: String! applicable_date: String): CityWeather
}
 
query {
  cityWeather(city_name: "Paris", applicable_date: "today") {
     temp,
     min_temp,
     max_temp,
     city_name,
     applicable_date
  }
}
const resolvers = {
  Query: {
    cityWeather: (root, args, context, info) => {
      return getWoeid(args.city_name).then( (response) => {
        if (!response) {
          return null;
        }
        return getWeather(response).then( (weather) => {
          if (!weather) {
            return null;
          }
          let consolidated_weather = weather.consolidated_weather;
          // check for args applicable_date to apply filter
          consolidated_weather = args.applicable_date ? consolidated_weather.find(item => item.applicable_date === args.applicable_date) : consolidated_weather[0];
          const respObj = {'temp': consolidated_weather.the_temp.toString(), 'min_temp': consolidated_weather.min_temp.toString(), 'max_temp': consolidated_weather.max_temp.toString(), 'city_name': weather.title, 'applicable_date': consolidated_weather.applicable_date};
          return respObj;
        });
      });
    }
  },
};

deck

By Andrey Kucherenko

Loading comments...

More from Andrey Kucherenko