DEV Community

[Comment from a deleted post]
Collapse
 
simonlegg profile image
Simon • Edited

Assuming the sorting you’re wanting to do would be is something like return the countries by number of cases ascending/descending. What you could do is to add args to the countries field

  args: {
    sortBy: { type: GraphQLString, defaultValue: 'cases' },
    order: { type: GraphQLString, defaultValue: 'desc' },
  },

And then instead of simply returning res.data, you would look at those args being passed in and decide how to sort them inside your resolver before returning.

  resolve(parentValue, args) {
    return axios.get('https://covid19search.netlify.com/_data/world.json').then(res => {
      return res.data.sort((a, b) => {
        if (args.order === 'desc') {
          return a[args.sortBy] > b[args.sortBy];
        } else {
          return b[args.sortBy] > a[args.sortBy];
        }
      });
    });
  },

disclaimer: the code might not be perfect, it was something i just threw together, but this approach is how I’ve done it previously

Collapse
 
sharadcodes profile image
Sharad Raj (He/Him)

Thanks will look at this