// talk to the GraphQL endpoint const gql = async (query, variables={}) => { let response = await fetch('/graphql', { method: 'POST', headers: { 'content-type': 'application/json', }, body: JSON.stringify({ operationName: null, query, variables, }), }); let json = await response.json(); if (json.errors && json.errors.length) { throw json.errors; } else { return json.data; } }; // some queries/mutations const getConfession = async hash => gql('query Q($hash: String) { confession(hash: $hash) { title, hash } }', { hash }).then(d => d.confession); const getConfessionWithMessage = async id => gql('mutation Q($id: String) { confessionWithMessage(id: $id) { title, hash, message } }', { id }).then(d => d.confessionWithMessage); const addConfession = async (title, message) => gql('mutation M($title: String, $message: String) { addConfession(title: $title, message: $message) { id } }', { title, message }).then(d => d.addConfession); const previewHash = async (title, message) => gql('mutation M($title: String, $message: String) { addConfession(title: $title, message: $message) { hash } }', { title, message }).then(d => d.addConfession); // the important elements const title = document.querySelector('#title'); const message = document.querySelector('#message'); const publish = document.querySelector('#publish'); const preview = document.querySelector('#preview'); // render a confession const show = async confession => { if (confession) { preview.querySelector('.title').textContent = confession.title || '