Created
August 13, 2019 07:44
-
-
Save epalaz/f83077f37f2cdd05c6923e322d3f43cc to your computer and use it in GitHub Desktop.
App.js File for Sample Application
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
import React from 'react'; | |
import logo from './logo.svg'; | |
import './App.css'; | |
import { InMemoryCache } from 'apollo-cache-inmemory' | |
import { createUploadLink } from 'apollo-upload-client' | |
import {ApolloClient} from "apollo-client" | |
import {ApolloProvider, Mutation} from "react-apollo" | |
import gql from "graphql-tag" | |
const apolloCache = new InMemoryCache() | |
const uploadLink = createUploadLink({ | |
uri: 'http://localhost:4000', // Apollo Server is served from port 4000 | |
headers: { | |
"keep-alive": "true" | |
} | |
}) | |
const client = new ApolloClient({ | |
cache: apolloCache, | |
link: uploadLink | |
}) | |
const UPLOAD_FILE = gql` | |
mutation SingleUpload($file: Upload!) { | |
singleUpload(file: $file) { | |
filename | |
mimetype | |
encoding | |
} | |
} | |
`; | |
const UPLOAD_FILE_STREAM = gql` | |
mutation SingleUploadStream($file: Upload!) { | |
singleUploadStream(file: $file) { | |
filename | |
mimetype | |
encoding | |
} | |
} | |
`; | |
function App() { | |
return ( | |
<div className="App"> | |
<ApolloProvider client={client}> | |
<header className="App-header"> | |
<img src={logo} className="App-logo" alt="logo" /> | |
<h2>Save Local</h2> | |
<Mutation mutation={UPLOAD_FILE}> | |
{(singleUpload, { data, loading }) => { | |
console.log(data) | |
return (<form onSubmit={() => {console.log("Submitted")}} encType={'multipart/form-data'}> | |
<input name={'document'} type={'file'} onChange={({target: { files }}) => { | |
const file = files[0] | |
file && singleUpload({ variables: { file: file } }) | |
}}/>{loading && <p>Loading.....</p>}</form>)} | |
} | |
</Mutation> | |
<h2>Stream to Server</h2> | |
<Mutation mutation={UPLOAD_FILE_STREAM}> | |
{(singleUploadStream, { data, loading }) => { | |
console.log(data) | |
return (<form onSubmit={() => {console.log("Submitted")}} encType={'multipart/form-data'}> | |
<input name={'document'} type={'file'} onChange={({target: { files }}) => { | |
const file = files[0] | |
file && singleUploadStream({ variables: { file: file } }) | |
}}/>{loading && <p>Loading.....</p>}</form>)} | |
} | |
</Mutation> | |
</header> | |
</ApolloProvider> | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment