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
GraphCMS 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 GraphCMS 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
GraphCMS supports uploading assets via HTTP.
Assets are treated just like any other content entry, so they are automatically bound to the environment, and authorization settings of your project.
Simply append /upload
to your project API endpoint to upload a new asset.
https://[region].graphcms.com/v2/[projectId]/[environment]/upload
Upload by file
Uploaded files must be no bigger than 100mb.
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
GraphCMS 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.