Created
March 31, 2018 20:41
-
-
Save SaraVieira/6b65ec74bd8f4ba45773c737f46fd33b 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
<script> | |
import { GET_NAMES, ADD_NAME } from "../queries"; | |
export default { | |
name: "Form", | |
data() { | |
return { | |
person: "", | |
error: null | |
}; | |
}, | |
methods: { | |
onSubmit() { | |
const name = this.person; | |
this.person = ""; | |
this.$apollo.mutate({ | |
mutation: ADD_NAME, | |
variables: { | |
name | |
}, | |
update: (cache, { data: { createNames } }) => { | |
const { allNameses } = cache.readQuery({ query: GET_NAMES }); | |
cache.writeQuery({ | |
query: GET_NAMES, | |
data: { | |
allNameses: allNameses.concat(createNames) | |
} | |
}); | |
} | |
}) | |
.catch(error => { | |
console.error(error); | |
this.person = name; | |
this.error = `There has been a problem adding ${name} :(`; | |
}); | |
}, | |
} | |
}; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment