2018-09-18 20:42:51 +10:00

41 lines
817 B
JavaScript

import { graphql } from 'react-apollo'
import gql from 'graphql-tag'
import uploadsQuery from '../queries/uploads'
const UploadFile = ({ mutate }) => {
const handleChange = ({
target: {
validity,
files: [file]
}
}) =>
validity.valid &&
mutate({
variables: { file },
update(
proxy,
{
data: { singleUpload }
}
) {
const data = proxy.readQuery({ query: uploadsQuery })
data.uploads.push(singleUpload)
proxy.writeQuery({ query: uploadsQuery, data })
}
})
return <input type="file" required onChange={handleChange} />
}
export default graphql(gql`
mutation($file: Upload!) {
singleUpload(file: $file) {
id
filename
encoding
mimetype
path
}
}
`)(UploadFile)