What is apollo-upload-client?
The apollo-upload-client package is a client for Apollo GraphQL that enables file uploads via GraphQL mutations. It extends Apollo Client to support file uploads using the GraphQL multipart request specification.
What are apollo-upload-client's main functionalities?
File Upload
This feature allows you to upload files via GraphQL mutations. The code sample demonstrates how to set up an Apollo Client with the upload link and perform a file upload mutation.
const { ApolloClient, InMemoryCache, HttpLink } = require('@apollo/client');
const { createUploadLink } = require('apollo-upload-client');
const link = createUploadLink({ uri: 'http://localhost:4000/graphql' });
const client = new ApolloClient({
link,
cache: new InMemoryCache()
});
const UPLOAD_FILE = gql`
mutation($file: Upload!) {
uploadFile(file: $file) {
url
}
}
`;
const file = new File(['foo'], 'foo.txt', {
type: 'text/plain',
});
client.mutate({
mutation: UPLOAD_FILE,
variables: { file }
}).then(response => {
console.log(response.data.uploadFile.url);
});
Other packages similar to apollo-upload-client
apollo-link-rest
This package allows you to call REST endpoints from Apollo Client. While it does not specifically handle file uploads, it can be used to integrate RESTful file upload endpoints with Apollo Client.
graphql-request
A minimal GraphQL client that supports file uploads via the multipart request specification. It is more lightweight compared to apollo-upload-client and can be used in environments where a full Apollo Client setup is not required.
apollo-upload-client
Enhances Apollo for intuitive file uploads via GraphQL queries or mutations. Use with apollo-upload-server.
Setup
Install with peer dependencies using npm:
npm install apollo-upload-client apollo-link graphql
Initialize Apollo Client with this terminating link:
import { createUploadLink } from 'apollo-upload-client'
const link = createUploadLink()
See also the setup instructions for the apollo-upload-server
middleware.
Options
createUploadLink
options match createHttpLink
options:
includeExtensions
(boolean): Toggles sending extensions
fields to the GraphQL server. (default: false
).uri
(string): GraphQL endpoint URI (default: /graphql
).credentials
(string): Overrides fetchOptions.credentials
.headers
(object): Merges with and overrides fetchOptions.headers
.fetchOptions
(object): fetch
init; overridden by upload requirements.fetch
(function): Fetch API to use (default: Global fetch
).
Usage
Use File
, FileList
or ReactNativeFile
instances anywhere within query or mutation input variables. For server instructions see apollo-upload-server
. See the example API and client.
import gql from 'graphql-tag'
import { graphql } from 'react-apollo'
export default graphql(gql`
mutation($file: Upload!) {
uploadFile(file: $file) {
id
}
}
`)(({ mutate }) => (
<input
type="file"
required
onChange={({ target }) =>
target.validity.valid && mutate({ variables: { file: target.files[0] } })
}
/>
))
import gql from 'graphql-tag'
import { graphql } from 'react-apollo'
export default graphql(gql`
mutation($files: [Upload!]!) {
uploadFiles(files: $files) {
id
}
}
`)(({ mutate }) => (
<input
type="file"
multiple
required
onChange={({ target }) =>
target.validity.valid && mutate({ variables: { files: target.files } })
}
/>
))
React Native
Substitute File
with ReactNativeFile
from extract-files
:
import { ReactNativeFile } from 'apollo-upload-client'
const file = new ReactNativeFile({
uri: '…',
type: 'image/jpeg',
name: 'photo.jpg'
})
const files = ReactNativeFile.list([
{
uri: '…',
type: 'image/jpeg',
name: 'photo-1.jpg'
},
{
uri: '…',
type: 'image/jpeg',
name: 'photo-2.jpg'
}
])
Support
- > 2% market share browsers.
- React Native.