Last active
December 10, 2020 04:04
-
-
Save eduardopc/50423a056da25d42ab015cd1dd7f08bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let animals = [ | |
{ | |
id: 0, | |
animal: 'dog', | |
emoji: '๐ถ' | |
}, | |
{ | |
id: 1, | |
animal: 'cat', | |
emoji: '๐ฑ' | |
}, | |
{ | |
id: 2, | |
animal: 'pig', | |
emoji: '๐ท' | |
}, | |
{ | |
id: 3, | |
animal: 'cow', | |
emoji: '๐ฎ' | |
}, | |
{ | |
id: 4, | |
animal: 'chicken', | |
emoji: '๐' | |
}]; | |
export const typeDefs = ` | |
type Animal { | |
id: ID | |
animal: String | |
emoji: String | |
} | |
type Query { | |
whatsAnimal: Animal | |
allAnimals: [Animal] | |
} | |
type Mutation { | |
createAnimal(animal: String, emoji: String): Animal | |
deleteAnimal(id: ID): Animal | |
} | |
`; | |
export const resolvers = { | |
Query: { | |
whatsAnimal: () => { | |
const idx = Math.floor(Math.random() * animals.length); | |
return animals[idx]; | |
}, | |
allAnimals: () => { | |
return animals; | |
}, | |
}, | |
Mutation: { | |
createAnimal: (_: any, { animal, emoji }: any) => { | |
let count = animals.length; | |
const newAnimal = { | |
id: count++, | |
animal, | |
emoji | |
} | |
animals = [...animals, newAnimal]; | |
return newAnimal; | |
}, | |
deleteAnimal: (_: any, { id }: any) => { | |
const animalToDelete = animals.find(x => x.id.toString() === id); | |
if (animalToDelete !== undefined) { | |
animals = animals.filter(animal => { | |
return animal.id !== animalToDelete.id; | |
}); | |
} | |
return animalToDelete; | |
} | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment