apollo server 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)
}

0
7

                                    file upload

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
graphql-upload apollo server 3 apollo server 3 upload file uploading in apollo graphql apollo server file upload example file upload in graphql and ract apolloserver upload upload file graphql apollo client file upload in graphql v2 apollo-client file upload file upload in graphql with client server how to upload file in graphql altair graphiql upload file graphql file upload specification can you upload file with graphql api graphql server upload file uploadfile graphql graphql file upload node js graphql form upload linux graphql file upload Graphql-upload file react graphql upload file graphql file upload type upload file in graphql nide js graphql browser upload graphql file uplaod file upload axuios graphql graphql upload files streaem graphql upload files playground graphql upload files without client graphql file uploading locally upload files graphql upload files graphql query graphql file upload pr can we upload file using graphql uploads with "graphql" server file upload example in graphql is it okay to upload file in graphql upload with graphql upload files in graphql client graphql playground upload files how to upload files in graphql playground handling graphql file uploads upload files graphql input for graphql Upload File graphql type uploadfile alter graphql client upload file graphql uploads image upload to graphql graphql file upload example graphql-upload example graphql-request file upload graphql photo upload best graphql client file upload upload image graphql how to handle file upload with graphql graphql client upload file graphql js upload a file graphql video upload graphql file upload nodejs graphql upload client graphql fileuploads image upload option in graphql graphql upload files upload file in graphql and store different methods to upload file in graphql apollo graphql file upload example graphql file upload mi graphql file upload mihoj graphql file upload miroh file uploading graphql graphql playground upload file how to upload a file graphQL how to upload file in graphql playground graphql file and upload apollo graphql file upload type graphql file upload graphql.Upload reader apollo file uploads graphql worth of upload files using graphql igraphql upload file get graphql file upload path file upload with graphql graphql upload document graphql fetch upload file example graphql upload file frontend apollo graphql upload file file upload in graphql upload file via graphql upload file in graphql graphql apollo upload file using graphql-upload example how to upload file in graphql graphql image upload graphql server with file upload example upload file post api in graphql request graphql file uploads fileupload graphql file uploads apollo react graphql upload express how to upload file using graphql upload file graphql type-graphql upload file graphql Upload type client graphql upload image how to check image in apollo server graphql how to check image size in apollo server graphql upload image in react apollo graphql graphql s3 file uploader graphql uploar send file graphql upload image graphql server add upload scalar to apollo file upload to apollo server apollo server file upload file upload using graphql graphql FileUpload apollo-server upload Upload! graphql apollo mutation graphene file upload Upload type apollo server apollo server file upload mutation graphql upload upload scalar type file upload graphql node js graphql file upload graphql upload file graphql file upload
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source