Skip to content

Instantly share code, notes, and snippets.

@aligos
Created November 5, 2016 06:48
Show Gist options
  • Save aligos/bcfdf5e8b3a267c09615545ee4ccf28c to your computer and use it in GitHub Desktop.
Save aligos/bcfdf5e8b3a267c09615545ee4ccf28c to your computer and use it in GitHub Desktop.
resolvers js votemon server
import { find, filter } from 'lodash';
import { pubsub } from './subscriptions';
const types = [
{ id: 1, title: 'Water' },
{ id: 2, title: 'Electric' },
];
const pokemons = [
{ id: 1, typeId: 1, name: 'Squirtle', image: 'https://img.pokemondb.net/artwork/squirtle.jpg', votes: 2 },
{ id: 2, typeId: 2, name: 'Pikachu', image: 'https://img.pokemondb.net/artwork/pikachu.jpg', votes: 3 },
{ id: 3, typeId: 2, name: 'Raichu', image: 'https://img.pokemondb.net/artwork/raichu.jpg', votes: 1 },
];
const resolveFunctions = {
Query: {
pokemons() {
return pokemons;
},
},
Mutation: {
upvotePokemon(_, { pokemonId }) {
const pokemon = find(pokemons, { id: pokemonId });
if (!pokemon) {
throw new Error(`Couldn't find post with id ${pokemonId}`);
}
pokemon.votes += 1;
pubsub.publish('pokemonUpvoted', pokemon);
return pokemon;
},
},
Subscription: {
pokemonUpvoted(pokemon) {
return pokemon;
},
},
Type: {
pokemons(type) {
return filter(pokemons, { typeId: type.id });
},
},
Pokemon: {
type(pokemon) {
return find(types, { id: pokemon.typeId });
},
},
};
export default resolveFunctions;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment