How to get a story with assets

Now that we know how to get a story, let's dig a bit deeper, and fetch the assets (attached files, urls, ...) that are used in the current version of the story.

We have to use the asset resolver on story.

graphql
query {
story(slug: 123) {
id
slug
version {
title
assets {
__typename
uuid
created
}
}
user {
name
}
}
}

Notice the __typename here, the value is probably "FileAsset", or "UrlAsset".

That will give us an array of assets. But what we want is having the URLs, thumbnails, extracts,... of the assets.

As an asset can have multiple forms (FileAsset, UrlAsset, StoryAsset, ...) they can also have different fields attached. To fetch those specific fields we need to "cast" and use fragments exactly as we did with ... on Story previously .

The __typename indicates the type of a specific asset, and by so the cast we need to use to fetch those specific fields.

graphql
query {
story(slug: 123) {
id
slug
version {
title
assets {
__typename
uuid
created
... on FileAsset {
embed
filename
downloadUrl
}
... on URLAsset {
embed
url
}
}
}
user {
name
}
}
}

now that will give us something like this:

response
{
"data": {
"story": {
"id": "TWU6MTIz",
"slug": 123,
"version": {
"title": "Hello world!",
"assets": [
{
"__typename": "FileAsset",
"uuid": "ef022818c-88dc-432c-975d-74bc4575a251",
"created": "2020-03-26T10:21:10.067170Z",
"embed": {
"url": "https://mycompany.elium.com/api/story/asset/12/download/13/image001.jpg",
"thumbnail_url": "https://mycompany.elium.com/api/story/asset/12/thumbnail/13?fallback=false&tclass=xxlarge",
"thumbnail_fallback_url": "https://mycompany.elium.com/api/story/asset/12/thumbnail/13?fallback=false&tclass=xxlarge",
"image_url": "https://mycompany.elium.com/api/story/asset/12/thumbnail/13?fallback=false&tclass=xxlarge",
"filename": "image001.jpg",
"cover_url": "https://mycompany.elium.com/api/story/asset/12/thumbnail/13?fallback=false&tclass=cover",
"digest": "b6b6783c4b5445249b30e17d969515a1f5d8b82bf05a1c5ca97211e244ad9a1a",
"filesize": 53223,
"mimetype": "image/jpeg"
},
"filename": "image001.jpg",
"downloadUrl": "https://mycompany.elium.com/api/story/asset/12/download/13/image001.jpg"
}
]
},
"user": {
"name": "John Doe"
}
}
}
}

The embed will give us informations like the thumbnails for a FileAsset or the extract (structured summary of the website) for an URLAsset

Was this page helpful?