You can transfer files between the client and the server using GraphQL. It is really simple as in REST.
If you're using
express-graphql as your GraphQL Server, you should install the graphql-upload package and add some code.
You should extend your existing server to handle file uploads. Check the following recipes for GraphQL Express and Apollo Server.
Let's assume we have something like the following at the beginning.
As you can seem we add the
graphqlUploadExpress middleware into
/graphql route to handle file uploads and pass data through the resolvers.
If you are using apollo-server, you don't have to add the express middleware.
You just need to add the
uploads field to your Apollo Server configuration:
You can read more about upload options in graphql-upload docs.
Upload scalar into the schema#
graphql-upload provides you the necessary scalar implementation, so you should simply put it inside your resolvers.
In the example, we create a new module called
UploadModule and put everything related to file uploads in it.
Let's assume we have
ImageModule and we want to add a mutation to it for uploading an image to our server: