Merge pull request #3 from samcoenen/blob-support

Added Blob type examples.
This commit is contained in:
Jayden Seric 2018-01-29 14:01:29 +11:00 committed by GitHub
commit adbe5316c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 130 additions and 35 deletions

13
app/components/field.js Normal file
View File

@ -0,0 +1,13 @@
const Field = ({ children }) => (
<label>
{children}
<style jsx>{`
label {
display: block;
margin-bottom: 0.5em;
}
`}</style>
</label>
)
export default Field

View File

@ -1,12 +0,0 @@
const FileInput = props => (
<div>
<input type="file" {...props} />
<style jsx>{`
div {
padding: 0.5em;
}
`}</style>
</div>
)
export default FileInput

15
app/components/section.js Normal file
View File

@ -0,0 +1,15 @@
const Section = ({ heading, children }) => (
<section>
<h1>{heading}</h1>
{children}
<style jsx>{`
h1 {
margin-top: 1.5em;
margin-bottom: 0.5em;
font-size: 120%;
}
`}</style>
</section>
)
export default Section

View File

@ -0,0 +1,70 @@
import { Component } from 'react'
import { graphql } from 'react-apollo'
import gql from 'graphql-tag'
import Field from './field'
import uploadsQuery from '../queries/uploads'
class UploadBlob extends Component {
state = {
name: '',
content: ''
}
handleChange = ({ target: { name, value } }) =>
this.setState({ [name]: value })
handleSubmit = event => {
event.preventDefault()
const file = new Blob([this.state.content], { type: 'text/plain' })
file.name = `${this.state.name}.txt`
this.props.mutate({
variables: { file },
update: (proxy, { data: { singleUpload } }) => {
const data = proxy.readQuery({ query: uploadsQuery })
data.uploads.push(singleUpload)
proxy.writeQuery({ query: uploadsQuery, data })
}
})
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<Field>
<input
name="name"
placeholder="Name"
required
value={this.state.name}
onChange={this.handleChange}
/>{' '}
.txt
</Field>
<Field>
<textarea
name="content"
placeholder="Content"
required
value={this.state.content}
onChange={this.handleChange}
/>
</Field>
<button>Upload</button>
</form>
)
}
}
export default graphql(gql`
mutation($file: Upload!) {
singleUpload(file: $file) {
id
filename
encoding
mimetype
path
}
}
`)(UploadBlob)

View File

@ -1,13 +1,12 @@
import { graphql } from 'react-apollo'
import gql from 'graphql-tag'
import FileInput from './file-input'
import uploadsQuery from '../queries/uploads'
const SingleUploader = ({ mutate }) => {
const handleChange = ({ target }) =>
target.validity.valid &&
const UploadFile = ({ mutate }) => {
const handleChange = ({ target: { validity, files: [file] } }) =>
validity.valid &&
mutate({
variables: { file: target.files[0] },
variables: { file },
update: (proxy, { data: { singleUpload } }) => {
const data = proxy.readQuery({ query: uploadsQuery })
data.uploads.push(singleUpload)
@ -15,7 +14,7 @@ const SingleUploader = ({ mutate }) => {
}
})
return <FileInput required onChange={handleChange} />
return <input type="file" required onChange={handleChange} />
}
export default graphql(gql`
@ -28,4 +27,4 @@ export default graphql(gql`
path
}
}
`)(SingleUploader)
`)(UploadFile)

View File

@ -1,13 +1,12 @@
import { graphql } from 'react-apollo'
import gql from 'graphql-tag'
import FileInput from './file-input'
import uploadsQuery from '../queries/uploads'
const MultipleUploader = ({ mutate }) => {
const handleChange = ({ target }) =>
target.validity.valid &&
const UploadFileList = ({ mutate }) => {
const handleChange = ({ target: { validity, files } }) =>
validity.valid &&
mutate({
variables: { files: target.files },
variables: { files },
update: (proxy, { data: { multipleUpload } }) => {
const data = proxy.readQuery({ query: uploadsQuery })
data.uploads.push(...multipleUpload)
@ -15,7 +14,7 @@ const MultipleUploader = ({ mutate }) => {
}
})
return <FileInput multiple required onChange={handleChange} />
return <input type="file" multiple required onChange={handleChange} />
}
export default graphql(gql`
@ -28,4 +27,4 @@ export default graphql(gql`
path
}
}
`)(MultipleUploader)
`)(UploadFileList)

View File

@ -2,7 +2,7 @@ import { graphql } from 'react-apollo'
import { Table, Head, Cell } from './table'
import uploadsQuery from '../queries/uploads'
const UploadList = ({ data: { uploads = [] } }) => (
const Uploads = ({ data: { uploads = [] } }) => (
<Table
thead={
<tr>
@ -23,4 +23,4 @@ const UploadList = ({ data: { uploads = [] } }) => (
/>
)
export default graphql(uploadsQuery)(UploadList)
export default graphql(uploadsQuery)(Uploads)

View File

@ -9,7 +9,7 @@
"apollo-cache-inmemory": "^1.1.5",
"apollo-client": "^2.2.0",
"apollo-link": "^1.0.7",
"apollo-upload-client": "^7.0.0-alpha.3",
"apollo-upload-client": "^7.0.0-alpha.4",
"babel-plugin-transform-inline-environment-variables": "^0.2.0",
"dotenv-cli": "^1.4.0",
"graphql": "^0.12.3",

View File

@ -1,7 +1,9 @@
import Page from '../components/page'
import SingleUploader from '../components/single-uploader'
import MultipleUploader from '../components/multiple-uploader'
import UploadList from '../components/upload-list'
import Section from '../components/section'
import UploadFile from '../components/upload-file'
import UploadFileList from '../components/upload-filelist'
import UploadBlob from '../components/upload-blob'
import Uploads from '../components/uploads'
import withData from '../providers/with-data'
const HomePage = () => (
@ -12,9 +14,18 @@ const HomePage = () => (
height="128"
alt="Apollo upload logo"
/>
<SingleUploader />
<MultipleUploader />
<UploadList />
<Section heading="Upload FileList">
<UploadFileList />
</Section>
<Section heading="Upload File">
<UploadFile />
</Section>
<Section heading="Upload Blob">
<UploadBlob />
</Section>
<Section heading="Uploads">
<Uploads />
</Section>
</Page>
)