Skip to content

Media

This article contains all actions for retrieving and managing media.

Tip

The media domain supports fetching related resources.

Get media by ID#

query {
  getMedia(id: "1") {
    id
    siteid
    type
    label
    domain
    languageid
    mimetype
    url
    previews
    filesystem
    status
    mtime
    ctime
    editor
  }
}
const body = JSON.stringify({'query':
`query {
  getMedia(id: "1") {
    id
    siteid
    type
    label
    domain
    languageid
    mimetype
    url
    previews
    filesystem
    status
    mtime
    ctime
    editor
  }
}`});

fetch($('.aimeos').data('graphql'), {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    body: body
}).then(response => {
    return response.json();
}).then(data => {
    console.log(data);
});

Response:

{
  "data": {
    "getMedia": {
      "id": "1",
      "siteid": "1.",
      "type": "default",
      "label": "Demo: Article 1.webp",
      "domain": "product",
      "languageid": null,
      "mimetype": "image/webp",
      "url": "https://aimeos.org/media/default/product_01_A-big.webp",
      "previews": "{\"240\":\"https:\\/\\/aimeos.org\\/media\\/default\\/product_01_A-low.webp\",\"720\":\"https:\\/\\/aimeos.org\\/media\\/default\\/product_01_A-med.webp\",\"1350\":\"https:\\/\\/aimeos.org\\/media\\/default\\/product_01_A-big.webp\"}",
      "filesystem": "fs-media",
      "status": 1,
      "mtime": "2022-12-01 11:59:05",
      "ctime": "2022-12-01 11:59:05",
      "editor": "core"
    }
  }
}

Search media#

The filter parameter is explained in the filter section of the GraphQL basics article.

query {
  searchMedias(filter: "{\"=~\": {\"media.code\":\"demo-\"}}") {
    id
    siteid
    type
    label
    domain
    languageid
    mimetype
    url
    previews
    filesystem
    status
    mtime
    ctime
    editor
  }
}
let filter = {
    "=~": {"media.code":"demo-"}
};
const fstr = JSON.stringify(JSON.stringify(filter));
const body = JSON.stringify({'query':
`query {
  searchMedias(filter: ` + fstr + `) {
    id
    siteid
    type
    label
    domain
    languageid
    mimetype
    url
    previews
    filesystem
    status
    mtime
    ctime
    editor
  }
}`});

fetch($('.aimeos').data('graphql'), {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    body: body
}).then(response => {
    return response.json();
}).then(data => {
    console.log(data);
});

Response:

{
  "data": {
    "searchMedias": [
      {
        "id": "1",
        "siteid": "1.",
        "type": "default",
        "label": "Demo: Article 1.jpg",
        "domain": "supplier",
        "languageid": null,
        "mimetype": "image/jpeg",
        "url": "https://aimeos.org/media/default/logo-1.png",
        "previews": "{\"240\":\"https:\\/\\/aimeos.org\\/media\\/default\\/logo-1.png\"}",
        "filesystem": "fs-media",
        "status": 1,
        "mtime": "2022-05-28 06:26:38",
        "ctime": "2022-05-28 06:26:38",
        "editor": "core:setup"
      },
      {
        "id": "20",
        "siteid": "1.",
        "type": "stage",
        "label": "Demo: Best seller stage",
        "domain": "catalog",
        "languageid": "de",
        "mimetype": "image/webp",
        "url": "https://aimeos.org/media/default/main-banner-1-big.webp",
        "previews": "{\"480\":\"https:\\/\\/aimeos.org\\/media\\/default\\/main-banner-1-low.webp\",\"960\":\"https:\\/\\/aimeos.org\\/media\\/default\\/main-banner-1-med.webp\",\"1920\":\"https:\\/\\/aimeos.org\\/media\\/default\\/main-banner-1-big.webp\"}",
        "filesystem": "fs-media",
        "status": 1,
        "mtime": "2022-12-01 11:59:02",
        "ctime": "2022-12-01 11:59:02",
        "editor": "core"
      }
    ]
  }
}

Save single media#

mutation {
  saveMedia(input: {
    domain: "product"
    type: "default"
    label: "Test image"
    url: "https://myshop.com/images/test.jpg"
  }) {
    id
  }
}
const body = JSON.stringify({'query':
`mutation {
  saveMedia(input: {
    domain: "product"
    type: "default"
    label: "Test image"
    url: "https://myshop.com/images/test.jpg"
  }) {
    id
  }
}`});

fetch($('.aimeos').data('graphql'), {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    body: body
}).then(response => {
    return response.json();
}).then(data => {
    console.log(data);
});

Response:

{
  "data": {
    "saveMedia": {
      "id": "21"
    }
  }
}

Save multiple media#

mutation {
  saveMedias(input: [{
    domain: "product"
    type: "default"
    label: "Test 2 image"
    url: "https://myshop.com/images/test2.jpg"
  },{
    domain: "catalog"
    type: "stage"
    label: "Test 3 image"
    url: "https://myshop.com/images/test3.jpg"
  }]) {
    id
  }
}
const body = JSON.stringify({'query':
`mutation {
  saveMedias(input: [{
    domain: "product"
    type: "default"
    label: "Test 2 image"
    url: "https://myshop.com/images/test2.jpg"
  },{
    domain: "catalog"
    type: "stage"
    label: "Test 3 image"
    url: "https://myshop.com/images/test3.jpg"
  }]) {
    id
  }
}`});

fetch($('.aimeos').data('graphql'), {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    body: body
}).then(response => {
    return response.json();
}).then(data => {
    console.log(data);
});

Response:

{
  "data": {
    "saveMedias": [
      {
        "id": "22"
      },
      {
        "id": "23"
      }
    ]
  }
}

Delete single media#

mutation {
  deleteMedia(id: "20")
}
const body = JSON.stringify({'query':
`mutation {
  deleteMedia(id: "20")
}`});

fetch($('.aimeos').data('graphql'), {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    body: body
}).then(response => {
    return response.json();
}).then(data => {
    console.log(data);
});

Response:

{
  "data": {
    "deleteMedia": "20"
  }
}

Delete multiple media#

mutation {
  deleteMedias(id: ["21", "22"])
}
const body = JSON.stringify({'query':
`mutation {
  deleteMedias(id: ["21", "22"])
}`});

fetch($('.aimeos').data('graphql'), {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    body: body
}).then(response => {
    return response.json();
}).then(data => {
    console.log(data);
});

Response:

{
  "data": {
    "deleteMedias": [
      "21",
      "22"
    ]
  }
}

Comments

Become an Aimeos Partner

Aimeos partners are first-class specialists in creating or hosting your Aimeos e-commerce project. They have proven their expertise by building top level e-commerce applications using Aimeos.