I am trying to upload a file from Angular using apollo-angular to apollo server but the documentation is confusing and the information i found in genera is not updated to latest versions of apollo angular and apollo server.
In the front end i have:
Mutation
export const Save_UserMutation = gql`
mutation SaveUserMutation ($data: UsersInput!, $file: Upload) {
createUser(data: $data, file: $file) {
user
}
}
`
Service
save(_userData: User, emitMessage?: boolean) {
let file = _userData.profilePicture
_userData.profilePicture = (_userData.profilePicture as File).name
const saveUser$ = this.apollo.mutate({
mutation: Save_UserMutation,
variables: {
data: _dataUsuario,
file
},
context: {
useMultipart: true
}
}).subscribe()
}
In the backend:
Server
const server = new ApolloServer({
schema,
context: createContext,
uploads: false
})
Uploads set to false just like docs says https://www.apollographql.com/docs/apollo-server/data/file-uploads/#gatsby-focus-wrapper
Mutation written with nexus
export const UsersMutation = extendType({
type: 'Mutation',
definition(t) {
t.nonNull.field('createUser', {
type: 'Users',
args: {
data: nonNull(arg({ type: 'UsersInput' })),
file: arg({
type: GraphQLUpload
})
},
async resolve(parent, args, ctx) {
if (args.file) {
const { createReadStream, filename, mimetype, encoding } = await args.file
const stream = createReadStream()
const pathName = path.join(__dirname, 'public', 'profilePictures') + `/${filename}`
await stream.pipe(fs.createWriteStream(pathName))
args.data.profilePicture == pathName
}
args.data.password = await bcrypt.hash(args.data.password, 10)
return ctx.prisma.users.create({
data: args.data
})
}
}),
})
When i try to upload the file I get the following error.
POST body missing. Did you forget use body-parser middleware?