Skip to content

Instantly share code, notes, and snippets.

@Tonyce
Forked from unicodeveloper/single-file-upload.js
Created November 24, 2018 04:20
Show Gist options
  • Save Tonyce/4b6e080bb336334a4344ed33ead7b228 to your computer and use it in GitHub Desktop.
Save Tonyce/4b6e080bb336334a4344ed33ead7b228 to your computer and use it in GitHub Desktop.
Single file upload
import gql from 'graphql-tag'
import { Mutation } from 'react-apollo'
export const UPLOAD_FILE = gql`
mutation uploadFile($file: Upload!) {
uploadFile(file: $file) {
filename
}
}
`;
const uploadOneFile = () => {
return (
<Mutation mutation={UPLOAD_FILE}>
{uploadFile => (
<input
type="file"
required
onChange={({ target: { validity, files: [file] } }) =>
validity.valid && uploadFile({ variables: { file } });
}
/>
)}
</Mutation>
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment