英文:
How to get the url of a file uploaded using fileCreate?
问题
已按照文档中所示的示例进行了mutator的跟随。
这只返回文件上传时间(createdAt time)。如何获取图像的CDN URL?
英文:
Have followed the example mutator as shown in the documentation
$query = <<<QUERY
mutation fileCreate($files: [FileCreateInput!]!) {
fileCreate(files: $files) {
files {
alt
createdAt
}
userErrors {
field
message
}
}
}
QUERY;
This is just returning the createdAt time that the file was uploaded. How do I get the cdn url of the image?
答案1
得分: 1
这需要一个两步的方法。首先是修改原始突变以检索从Shopify生成的文件ID:
$query = <<<QUERY
mutation fileCreate($files: [FileCreateInput!]!) {
fileCreate(files: $files) {
files {
alt
createdAt
... on GenericFile {
id
}
... on MediaImage {
id
}
... on Video {
id
}
}
userErrors {
field
message
}
}
}
QUERY;
这将返回一个类似于gid://shopify/MediaImage/123456789012345
的ID。
文件上传后,我们需要使用我们的ID调用一个files
查询:
$getFileQuery = <<<QUERY
query {
files(first: 1, query: "id:{{ id }}") {
edges {
node {
fileStatus
... on MediaImage {
image {
url
}
}
}
}
}
}
QUERY;
当您上传文件时,Shopify 必须对其进行处理,因此状态为 "PROCESSING",直到处理完成,您才能获得 URL。因此,我将查询函数包装在一个 do/while 循环中:
//Ping shopify for the image. Make sure it has finished being processed.
$process = false;
do {
//Find my file by id
$response = $api->query(["query" => str_replace('{{ id }}', str_replace('gid://shopify/MediaImage/', '', $data['data']['fileCreate']['files'][0]['id']), $getFileQuery)]);
$data = $response->getDecodedBody();
//File still processing. Wait a moment and try again.
if (
!array_key_exists(0, $data['data']['files']['edges']) ||
$data['data']['files']['edges'][0]['node']['fileStatus'] != 'READY'
) {
sleep(1);
continue;
}
//Found our file. Extract the image url
$newFileName = $data['data']['files']['edges'][0]['node']['image']['url'];
$process = true; //Break the loop.
} while (!$process);
//Use $newFileName to take over the world.
英文:
This requires a two-step approach. The first is to amend the original mutation to retrieve the generated file id from shopify:
$query = <<<QUERY
mutation fileCreate($files: [FileCreateInput!]!) {
fileCreate(files: $files) {
files {
alt
createdAt
... on GenericFile {
id
}
... on MediaImage {
id
}
... on Video {
id
}
}
userErrors {
field
message
}
}
}
QUERY;
This will return an id similar to gid://shopify/MediaImage/123456789012345
Once the file has been uploaded we need to call a files
query with our ID:
$getFileQuery = <<<QUERY
query {
files(first: 1, query: "id:{{ id }}") {
edges {
node {
fileStatus
... on MediaImage {
image {
url
}
}
}
}
}
}
QUERY;
When you upload a file Shopify has to process it so the status is "PROCESSING" and you won't get the url out until it's finished. So I wrap the query function in a do/while loop:
//Ping shopify for the image. Make sure it has finished being processed.
$process = false;
do {
//Find my file by id
$response = $api->query(["query" => str_replace('{{ id }}', str_replace('gid://shopify/MediaImage/', '', $data['data']['fileCreate']['files'][0]['id']), $getFileQuery)]);
$data = $response->getDecodedBody();
//File still processing. Wait a moment and try again.
if (
!array_key_exists(0, $data['data']['files']['edges']) ||
$data['data']['files']['edges'][0]['node']['fileStatus'] != 'READY'
) {
sleep(1);
continue;
}
//Found our file. Extract the image url
$newFileName = $data['data']['files']['edges'][0]['node']['image']['url'];
$process = true; //Break the loop.
} while (!$process);
//Use $newFileName to take over the world.
Once Shopify has finished processing the file it sets the status as "READY" so you can grab the file url.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论