file upload to apollo server code example

Example: graphql file upload

//resolvers.js
export default {
	Query: {
		usersActivation() {
			return 'Hello Wordl'
		}
	},
	Mutation: {
		uploadFile: async (parent, { file }) => {
			const { filename, createReadStream } = await file.promise
			fileUpload({ filename, stream: createReadStream() })
			return true
		}
	}
}

//typedefs.js
export default gql(`
	 scalar Upload

	 type Query {
		helloWordl : String!
	 }

	type Mutation {
		uploadFile(file: Upload!): Boolean
	  }
	`)

// util.upload.js
const uploadFile = ({ filename, stream }) => {
	stream
		.pipe(createWriteStream(resolve(process.cwd(), `src/images/${filename}`)))
		.on('finish', () => Promise.resolve())
		.on('error', Promise.reject)
}

const validateFile = ({ filename, stream }) => {
	const extFile = filename.replace('.', '')
    
	const extPattern = /(jpg|jpeg|png|gif|svg)/gi.test(extFile)
	if (!extPattern) throw new TypeError('Image format is not valid')

	const fileExits = existsSync(resolve(process.cwd(), `src/images/${filename}`))
	if (!fileExits) return uploadFile({ filename, stream })

	unlink(resolve(process.cwd(), `src/images/${filename}`), (error) => {
		if (error) throw error
		return uploadFile({ filename, stream })
	})
}

export const fileUpload = ({ filename, stream }) =>
	filename ? validateFile({ filename, stream }) : new Error('Image is required')


// request query playground
mutation UPLOAD_FILE($file: Upload!) {
  uploadFile(file: $file)
}