Hello, I am really having hard times since more than a week trying to upload files with Apollo in a Meteor-React project. I have read tons of article and documentation but still not getting the solution.
This is my React Component to Upload Files
import React from 'react';
import { Mutation } from 'react-apollo'
import gql from 'graphql-tag';
const SINGLE_UPLOAD_MUTATION = gql`
mutation singleUpload($file: Upload!) {
singleUpload(file: $file) {
_id
location
}
}
`;
export const SingleUpload = () => {
return (
<Mutation mutation={SINGLE_UPLOAD_MUTATION}>
{uploadFile => (
<input
type="file"
required
onChange={({ target: { validity, files: [file] } }) =>
validity.valid && uploadFile({ variables: { file: file}})
}
/>
)}
</Mutation>
);
};
On the server these are my Mutation Schema:
type Mutation {
singleUpload(file: Upload!): File!
}
And my resolvers, I just try to console.log the uploaded file:
singleUpload: (parent, args) => {
console.log("REACHED SERVER");
console.log(args);
return args.file.then(file => {
const {createReadStream, filename, mimetype} = file
const fileStream = createReadStream()
fileStream.pipe(fs.createWriteStream(`./uploadedFiles/${filename}`))
return file;
});
}
This is the Apollo configuration:
const queryOrMutationLink = () =>
createUploadLink({
uri: Meteor.settings.public.graphQL.httpUri,
credentials: 'same-origin',
});
const subscriptionLink = () =>
new WebSocketLink({
uri: Meteor.settings.public.graphQL.wsUri,
options: { reconnect: true },
});
const apolloClient = new ApolloClient({
connectToDevTools: true,
link: ApolloLink.from([
MeteorAccountsLink(),
errorLink,
ApolloLink.split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
subscriptionLink(),
queryOrMutationLink(),
),
]),
cache: new InMemoryCache().restore(window.__APOLLO_STATE__),
});
export default apolloClient;
When I try uploading a file, I get a grapghql Network Error (Status 500) with this message
POST body missing. Did you forget use body-parser middleware?
Please anyone could help?
I am really lost by that.
Thank in advance.