Skip to content

Instantly share code, notes, and snippets.

@ardani
Created November 21, 2021 05:36
Show Gist options
  • Save ardani/3f018fb0ae27a60ef6e94a8e6ae5a552 to your computer and use it in GitHub Desktop.
Save ardani/3f018fb0ae27a60ef6e94a8e6ae5a552 to your computer and use it in GitHub Desktop.
const {map, first} = require('lodash');
const resolvers = {
Query: {
hero: async (_, {id}, {dataSources}) => {
const res = await dataSources.marvelApi.getHero(id);
const data = res.data;
const item = first(data.results);
return {
id: item.id,
name: item.name,
description: item.description,
thumbnail: item.thumbnail
}
},
heros: async (_, {name = null, offset, limit}, {dataSources}) => {
const res = await dataSources.marvelApi.getHeros(name, offset, limit);
const data = res.data;
// formatted results
const edges = map(data.results, function (item) {
return {
id: item.id,
name: item.name,
description: item.description,
thumbnail: item.thumbnail
}
});
return {
edges: edges,
page_info: {
limit: data.limit,
offset: data.offset,
total: data.total
}
}
},
},
Hero: {
comics: async (parent, {offset, limit }, { dataSources }) => {
const res = await dataSources.marvelApi.getHeroComics(parent.id, offset, limit);
const data = res.data;
// formatted results
const edges = map(data.results, function (item) {
return {
id: item.id,
title: item.title,
description: item.description,
resourceURI: item.resourceURI
}
});
return {
edges: edges,
page_info: {
limit: data.limit,
offset: data.offset,
total: data.total
}
}
}
}
}
module.exports = resolvers;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment