Query examplesMedia
Media
These are examples of queries to fetch and mutate media data.
Fetching media
A media item, with the image source in different sizes:
query {
mediaItem(by: { id: 1647 }) {
id
srcSet
src
thumbSizeSrc: src(size: "thumbnail")
largeSizeSrc: src(size: "large")
}
}
All media items (with mime type "image" by default), and all the image sizes:
query {
imageSizeNames
mediaItems {
id
srcSet
src(size: "medium")
sizes(size: "thumbnail")
width
height
slug
url
urlAbsolutePath
title
caption
altText
description
date
mimeType
}
}
The posts' featured image:
query {
posts {
id
hasFeaturedImage
featuredImage {
id
src
width
height
}
}
}
Filtering media items:
{
mediaItems(
pagination: { limit: 3 },
sort: { by: TITLE },
filter: { dateQuery: { after: "2012-01-02" } }
) {
id
src
height
width
}
}
Fetching video and audio items:
{
mediaItems(
filter: { mimeTypes: ["video", "audio"] }
) {
id
src
}
}
Fetching the logged-in user's media items
Fields myMediaItemCount
, myMediaItems
and myMediaItem
fetch the logged-in user's media items:
query GetMediaItems {
me {
slug
}
myMediaItemCount
myMediaItems(pagination: {
limit: 3
}) {
...MediaItemData
}
myMediaItem(by: { id: 1380 }) {
...MediaItemData
}
}
fragment MediaItemData on Media {
id
mimeType
src
author {
slug
}
}
Setting a post's featured image
We can reference any image existing in the Media library.
mutation {
setFeaturedImageOnCustomPost(
input: {
customPostID: 1499,
mediaItemID: 1505
}
) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
customPostID
customPost {
...on WithFeaturedImage {
featuredImage {
id
src
}
}
}
}
}
With nested mutations:
mutation {
post(by: { id: 1499 }) {
setFeaturedImage(input: { mediaItemID: 1647 }) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
customPostID
customPost {
...on WithFeaturedImage {
featuredImage {
id
src
}
}
}
}
}
}
Uploading an attachment to the Media Library
Passing the URL:
mutation CreateMediaItems {
fromURL: createMediaItem(input: {
from: {
url: {
source: "https://gatographql.com/assets/GatoGraphQL-logo.webp"
}
}
caption: "Gato GraphQL logo"
altText: "This is the Gato GraphQL logo"
}) {
mediaItemID
status
errors {
__typename
...on ErrorPayload {
message
}
}
mediaItem {
altText
caption
mimeType
slug
src
title
}
}
}
Passing directly the contents of the attachment:
mutation CreateMediaItems {
createMediaItem(input: {
from: {
contents: {
body: """
<html>
<body>
Hello world!
</body>
</html>
"""
filename: "hello-world.html"
}
}
title: "Hello world!"
}) {
mediaItemID
status
errors {
__typename
...on ErrorPayload {
message
}
}
mediaItem {
altText
caption
mimeType
slug
src
title
}
}
}
Creating a media item reusing the attached file from another media item:
mutation CreateMediaItems {
createMediaItem(input: {
from: {
mediaItemBy: {
id: 1361
}
}
title: "Hello world!"
}) {
mediaItemID
status
errors {
__typename
...on ErrorPayload {
message
}
}
mediaItem {
altText
caption
mimeType
slug
src
title
}
}
}
Updating a media item
Changing the alternative text and caption of an image:
mutation UpdateMediaItem {
updateMediaItem(input: {
id: 1380
caption: "Updated caption"
altText: "Updated alt text"
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
mediaItem {
altText
caption
}
}
}
Next