Would this be the correct way to do this using just normal graphql types?
server.js
app.use(
'/graphql',
bodyParser.json(),
apolloUploadExpress({
uploadDir: '/tmp/uploads',
}),
expressGraphQL({
schema,
graphiql: true,
})
);
server-side input type:
const graphql = require('graphql');
const {
GraphQLInputObjectType,
GraphQLString,
GraphQLInt,
GraphQLNonNull,
} = graphql;
const UploadType = new GraphQLInputObjectType({
name: 'UploadType',
fields: () => ({
name: { type: new GraphQLNonNull(GraphQLString) },
type: { type: new GraphQLNonNull(GraphQLString) },
size: { type: new GraphQLNonNull(GraphQLInt) },
path: { type: new GraphQLNonNull(GraphQLString) },
}),
});
module.exports = UploadType;
server-side mutation:
...
uploadFile: {
type: PartType,
args: {
file: {
type: new GraphQLNonNull(UploadType),
},
owner: {
type: new GraphQLNonNull(GraphQLString),
},
},
resolve(rootValue, {
file,
owner,
}) {
console.log('uploading file...');
console.log('rootValue: ', rootValue);
console.log('file: ', file);
console.log('owner: ', owner);
return new Promise((resolve, reject) => {
resolve(null);
});
},
},
...
client side mutation:
import gpl from 'graphql-tag';
export default gpl`
mutation UploadFile($file:UploadType!,$owner:String!){
uploadFile(file:$file,owner:$owner){
id
}
}
`;
client-side component function:
...
handleUploadFile({ target }) {
const {
UploadFile,
owner,
} = this.props;
if (target.validity.valid) {
const { name, type, size, path } = target.files[0];
UploadFile({
variables: {
file: {
name,
type,
size,
path: path || '',
},
owner,
},
})
.then(({ data }) => {
console.log('ImagePartModal -> UploadFile', data);
});
}
}
...
According to the docs, I should be able to send over the File object from the client side, but I'm unable to do that.
When I do this:
...
UploadFile({
variables: {
file: target.files[0],
owner,
},
})
...
I get an error message:
{"errors":[{"message":"Variable \"$file\" got invalid value {}.\nIn field \"name\": Expected \"String!\", found null.\nIn field \"type\": Expected \"String!\", found null.\nIn field \"size\": Expected \"Int!\", found null.\nIn field \"path\": Expected \"String!\", found null.","locations":[{"line":1,"column":21}]}]}