Attributes
This article contains all actions for retrieving and managing attributes.
Tip
The attribute domain supports fetching related resources.
Get attribute by ID#
query {
getAttribute(id: "1") {
id
type
siteid
domain
code
label
position
status
mtime
ctime
editor
}
}
const body = JSON.stringify({'query':
`query {
getAttribute(id: "1") {
id
type
siteid
domain
code
label
position
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": {
"getAttribute": {
"id": "1",
"siteid": "1.",
"domain": "product",
"code": "demo-black",
"label": "Demo: Black",
"position": 1,
"status": 1,
"mtime": "2022-12-01 11:59:05",
"ctime": "2022-12-01 11:59:05",
"editor": "core"
}
}
}
Find attribute by code#
query {
findAttribute(code: "demo-black", domain: "product", type: "color") {
id
type
siteid
domain
code
label
position
status
mtime
ctime
editor
}
}
const body = JSON.stringify({'query':
`query {
findAttribute(code: "demo-black", domain: "product", type: "color") {
id
type
siteid
domain
code
label
position
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": {
"findAttribute": {
"id": "1",
"siteid": "1.",
"domain": "product",
"code": "demo-black",
"label": "Demo: Black",
"position": 1,
"status": 1,
"mtime": "2022-12-01 11:59:05",
"ctime": "2022-12-01 11:59:05",
"editor": "core"
}
}
}
Search attributes#
The filter parameter is explained in the filter section of the GraphQL basics article.
query {
searchAttributes(filter: "{\"=~\": {\"attribute.code\":\"demo-\"}}") {
id
type
siteid
domain
code
label
position
status
mtime
ctime
editor
}
}
let filter = {
"=~": {"attribute.code":"demo-"}
};
const fstr = JSON.stringify(filter).replace(/"/g, '\\"');
const body = JSON.stringify({'query':
`query {
searchAttributes(filter: "` + fstr + `") {
id
type
siteid
domain
code
label
position
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": {
"searchAttributes": [
{
"id": "1",
"siteid": "1.",
"domain": "product",
"code": "demo-black",
"label": "Demo: Black",
"position": 1,
"status": 1,
"mtime": "2022-12-01 11:59:05",
"ctime": "2022-12-01 11:59:05",
"editor": "core"
},
{
"id": "2",
"siteid": "1.",
"domain": "product",
"code": "demo-blue",
"label": "Demo: Blue",
"position": 2,
"status": 1,
"mtime": "2022-12-01 11:59:05",
"ctime": "2022-12-01 11:59:05",
"editor": "core"
}
]
}
}
Save single attribute#
mutation {
saveAttribute(input: {
code: "test"
type: "color"
domain: "product"
label: "Test attribute"
}) {
id
}
}
const body = JSON.stringify({'query':
`mutation {
saveAttribute(input: {
code: "test"
type: "color"
domain: "product"
label: "Test attribute"
}) {
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": {
"saveAttribute": {
"id": "18"
}
}
}
Save multiple attributes#
mutation {
saveAttributes(input: [{
code: "test-2"
type: "color"
domain: "product"
label: "Test 2 attribute"
},{
code: "test-3"
type: "color"
domain: "product"
label: "Test 3 attribute"
}]) {
id
}
}
const body = JSON.stringify({'query':
`mutation {
saveAttributes(input: [{
code: "test-2"
type: "color"
domain: "product"
label: "Test 2 attribute"
},{
code: "test-3"
type: "color"
domain: "product"
label: "Test 3 attribute"
}]) {
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": {
"saveAttributes": [
{
"id": "19"
},
{
"id": "20"
}
]
}
}
Delete single attribute#
mutation {
deleteAttribute(id: "18")
}
const body = JSON.stringify({'query':
`mutation {
deleteAttribute(id: "18")
}`});
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": {
"deleteAttribute": "18"
}
}
Delete multiple attributes#
mutation {
deleteAttributes(id: ["19", "20"])
}
const body = JSON.stringify({'query':
`mutation {
deleteAttributes(id: ["19", "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": {
"deleteAttributes": [
"19",
"20"
]
}
}