Assets
The Asset model is included with every project, can be modified with custom Field Types, and are localized by default, but cannot be deleted.
While most commonly used for photos, can support any file type including audio files, zip files, and more.
The Asset type extends the system fields.
#Fetching assets
Hygraph automatically generates 4 query types for assets. These are:
asset
assets
assetVersion
assetsConnection
These queries work just like regular queries, and can be filtered by their fields.
#Referencing assets
While you can query for individuals assets, or fetch all and filter, order, and paginate, assets are best used when related to another model.
When extending the Hygraph schema with your own models, the Asset field type exposes all of the available transformations below.
For example, imagine you have a Product
model with a one to many reference to assets, as images
, you can query any of the system fields here also.
{product(where: { slug: "..." }) {images {urlheightwidth}}}
#Transformations
When fetching assets, you can pass an optional transformation
argument to the url
field.
#Resize images
The image
takes the following arguments:
Arg | Type | Description |
---|---|---|
width | Int | The width in pixels to resize the image to. The value must be an integer from 1 to 10000 . |
height | Int | The height in pixels to resize the image to. The value must be an integer from 1 to 10000 . |
fit | ImageFit | The default value for the fit parameter is clip . |
The ImageFit
takes one of the following values:
Value | Description |
---|---|
clip | Resizes the image to fit within the specified parameters without distorting, cropping, or changing the aspect ratio. |
crop | Resizes the image to fit the specified parameters exactly by removing any parts of the image that don't fit within the boundaries. |
scale | Resizes the image to fit the specified parameters exactly by scaling the image to the desired size. The aspect ratio of the image is not respected and the image can be distorted using this method. |
max | Resizes the image to fit within the parameters, but as opposed to fit:clip will not scale the image if the image is smaller than the output size. |
For example, we can query all assets, and resize images:
{assets {url(transformation: {image: { resize: { width: 50, height: 50, fit: clip } }})}}
#Convert file type
Depending on the asset type you're dealing with, it's possible to transform the output
to another file type by passing a format
value.
Current file type | Available output formats |
---|---|
jpg , odp , ods , odt , png , svg , txt , webp | |
DOC | docx , html , jpg , odt , pdf , png , svg , txt , webp |
DOCX | doc , html , jpg , odt , pdf , png , svg , txt , webp |
ODT | doc , docx , html , jpg , pdf , png , svg , txt , webp |
XLS | jpg , pdf , ods , png , svg , xlsx , webp |
XLSX | jpg , pdf , ods , png , svg , xls , webp |
ODS | jpg , pdf , png , xls , svg , xlsx , webp |
PPT | jpg , odp , pdf , png , svg , pptx , webp |
PPTX | jpg , odp , pdf , png , svg , ppt , webp |
ODP | jpg , pdf , png , ppt , svg , pptx , webp |
BMP | jpg , odp , ods , odt , pdf , png , svg , webp |
GIF | jpg , odp , ods , odt , pdf , png , svg , webp |
JPG | jpg , odp , ods , odt , pdf , png , svg , webp |
PNG | jpg , odp , ods , odt , pdf , png , svg , webp |
WEBP | jpg , odp , ods , odt , pdf , png , svg , webp |
TIFF | jpg , odp , ods , odt , pdf , png , svg , webp |
AI | jpg , odp , ods , odt , pdf , png , svg , webp |
PSD | jpg , odp , ods , odt , pdf , png , svg , webp |
SVG | jpg , odp , ods , odt , pdf , png , webp |
HTML | jpg , odt , pdf , svg , txt , webp |
TXT | jpg , html , odt , pdf , svg , webp |
For example, let's transform all assets to PDFs:
{assets {url(transformation: { document: { output: { format: pdf } } })}}
#Validating transforms
We provide a validation field you can enable to check the combination of transform arguments are valid.
For example, you may query a video, and request to change the file type to pdf. validateOptions: true
will warn you that this is not permitted.
{assets {url(transformation: {document: { output: { format: pdf } }validateOptions: true})}}
#Combining transforms
It is possible to combine both transformation arguments:
{assets {url(transformation: {image: { resize: { width: 50, height: 50, fit: clip } }document: { output: { format: png } }validateOptions: true})}}
#Alias transforms
GraphQL aliases are great for querying the same asset url with multiple transformations.
For example, you could transform product images to include a thumbnail
.
{products {images {thumbnail: url(transformation: {image: { resize: { width: 50, height: 50, fit: clip } }document: { output: { format: png } }})url(transformation: { document: { output: { format: png } } })}}}
#Uploading assets
Hygraph supports uploading assets via HTTP. You'll need a Permanement Auth Token with Mutations API access enabled to upload by file, or URL.
Assets are treated just like any other content entry, so they are automatically bound to the environment, and authorization settings of your project.
- You must append
/upload
to your project API endpoint when uploading assets. For example,https://[region].hygraph.com/v2/[projectId]/[environment]/upload
. - You can upload assets to your project without the need of a Permanement Auth Token by adding Read and Create permissions to the Public Content API. This is however unsafe and not advised, as exposing the endpoint anywhere - like your website - while having any write access on the Public API, would essentially allow anyone to modify your data.
#Upload by file
Uploaded files must be no bigger than 100mb.
Here's also a standard JavaScript example:
const HYGRAPH_URL = '';const HYGRAPH_ASSET_TOKEN = '';async function upload() {const input = document.getElementById('fileUpload');const file = input.files[0];const form = new FormData();form.append('fileUpload', file);// It is not recommended to use the HYGRAPH_ASSET_TOKEN in the Front-End.// In this example we're using it, but in a real application you should// use a backend to upload the file and use the HYGRAPH_ASSET_TOKEN there.const response = await fetch(`${HYGRAPH_URL}/upload`, {method: 'POST',headers: {Authorization: `Bearer ${HYGRAPH_ASSET_TOKEN}`,},body: form,});const data = await response.json();console.log(JSON.stringify(data, null, 2));}
#Upload by remote URL
You can also upload files by providing a remote URL, instead of a file.
#Updating assets
Since assets are a system model, and automatically added to every project, you can extend them with your own custom fields.
These fields can be updated using GraphQL mutations. For example, some users add a caption
field to assets:
mutation {updateAsset(where: { id: "..." }, data: { caption: "..." }) {id}}
Learn more about Mutations.
#Deleting assets
Hygraph exposes a deleteAsset
mutation that you can use to delete any unwanted assets.
Simply pass the id
of the asset you want deleted:
mutation {deleteAsset(where: { id: "..." }) {id}}
Learn more about Mutations.
#Publishing assets
Since Assets are a system model, they also come with publishing capabilities. You can use GraphQL mutations to publish or unpublish your assets, to and from content stages.
Learn more about publishing to content stages.
#Localized assets
Since assets are localized by default, you can upload a file for your project locales.
Learn more about mutating localized content.
#Embedded types
Assets can be embedded into the Rich Text Field Type via a configuration setting. On the API side, we create a union relation that references the selected model.