A space to discuss GraphQL queries, mutations, troubleshooting, throttling, and best practices.
We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more
Hi everyone,
I'm new to GraphQL and PHP and could use some help.
Trying to create a product with a couple of variants and images. it works right in the GraphQL explorer, but I can't get it to work with PHP.
this is the error that I'm getting: {"errors":[{"message":"Parse error on \"{\" (LCURLY) at [1, 24]","locations":[{"line":1,"column":24}]}]}
here's my php code:
<?php
$media = '{
"media": [{
"mediaContentType": "IMAGE",
"originalSource": "https://img.freepik.com/free-photo/bacon-burger_1339-1384.jpg"
},
{
"mediaContentType": "IMAGE",
"originalSource": "https://img.freepik.com/free-photo/bacon-burger_1339-1384.jpg"
}
]
}';
$input = '{
"input": {
"title": "Pictures",
"options": [
"Color",
"Size"
],
"variants": [{
"sku": "999912",
"barcode": "90909090909012",
"mediaSrc": [
"https://img.freepik.com/free-photo/bacon-burger_1339-1384.jpg"
],
"options": [
"Red",
"M"
]
},
{
"mediaSrc": [
"https://img.freepik.com/free-photo/bacon-burger_1339-1384.jpg"
],
"sku": "999113",
"barcode": "90909090909113",
"options": [
"Red",
"L"
]
},
{
"sku": "999214",
"barcode": "90909090909214",
"mediaSrc": [
"https://img.freepik.com/free-photo/bacon-burger_1339-1384.jpg"
],
"options": [
"Yellow",
"M"
]
},
{
"sku": "999315",
"barcode": "90909090909315",
"mediaSrc": [
"https://img.freepik.com/free-photo/bacon-burger_1339-1384.jpg"
],
"options": [
"Yellow",
"L"
]
}
]
}
}';
$query = "mutation productCreate($input: ProductInput!, $media: [CreateMediaInput!]) {
productCreate(input: '$input', media: '$media') {
product {
id
variants(first: 10) {
edges {
node {
selectedOptions {
name
value
}
media(first: 10) {
edges {
node {
... on MediaImage {
id
alt
image {
url
}
}
}
}
}
}
}
}
}
userErrors {
field
message
}
}
}";
$url = 'https://mysite.myshopify.com/admin/api/2022-10/graphql.json';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/graphql',
'X-Shopify-Access-Token: xxxxxxxxxxxxxx'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
$data = curl_exec($ch);
print_r($data);
?>
This is the mutation with the varialbles in GraphQL Explorer:
{
"media": [
{
"mediaContentType": "IMAGE",
"originalSource": "https://img.freepik.com/free-photo/bacon-burger_1339-1384.jpg"
},
{
"mediaContentType": "IMAGE",
"originalSource": "https://img.freepik.com/free-photo/bacon-burger_1339-1384.jpg"
}
],
"input": {
"title": "Pictures",
"options": [
"Color",
"Size"
],
"variants": [
{
"sku": "999912",
"barcode": "90909090909012",
"mediaSrc": [
"https://img.freepik.com/free-photo/bacon-burger_1339-1384.jpg"
],
"options": [
"Red",
"M"
]
},
{
"mediaSrc": [
"https://img.freepik.com/free-photo/bacon-burger_1339-1384.jpg"
],
"sku": "999113",
"barcode": "90909090909113",
"options": [
"Red",
"L"
]
},
{
"sku": "999214",
"barcode": "90909090909214",
"mediaSrc": [
"https://img.freepik.com/free-photo/bacon-burger_1339-1384.jpg"
],
"options": [
"Yellow",
"M"
]
},
{
"sku": "999315",
"barcode": "90909090909315",
"mediaSrc": [
"https://img.freepik.com/free-photo/bacon-burger_1339-1384.jpg"
],
"options": [
"Yellow",
"L"
]
}
]
}
}
mutation productCreate($input: ProductInput!, $media: [CreateMediaInput!]) {
productCreate(input: $input, media: $media) {
product {
id
variants(first: 10) {
edges {
node {
selectedOptions {
name
value
}
media(first: 10) {
edges {
node {
... on MediaImage {
id
alt
image {
url
}
}
}
}
}
}
}
}
}
userErrors {
field
message
}
}
}
Any help would be greatly appreciated.
Thanks