Shopify / shopify-api-php

MIT License
371 stars 171 forks source link

Return created/updated objects #175

Open MagicLegend opened 2 years ago

MagicLegend commented 2 years ago

Overview

The API itself will already return the full object if you do a create/update operation. However, the library swallows this response here: https://github.com/Shopify/shopify-php-api/blob/0556edf274ceb83346533bd22b662d7622e58c66/src/Rest/Base.php#L60-L74

Solution

Adding return $response->getDecodedBody(); at the end of the function should suffice. Note that the saveAndUpdate function would also need to be updated, with return $this->save(true);. Implementation can be found in my fork: https://github.com/MagicLegend/shopify-php-api/commit/d43bc94601dabae8135cd1c35b5f4af3e1fdac23

Type

Motivation

What inspired this feature request? What problems were you facing?

I would like to verify the saved object myself, without needing to query the API again for data that was already returned in the first place. This also allows immediate access to the ID of the newly created object.

shota commented 2 years ago

+1 I don't know why this has NOT been implemented yet.

shota commented 2 years ago

I have created PR for this. This is what should be (save return response, saveAndUpdate update object itself) https://github.com/Shopify/shopify-php-api/pull/185

Zelfapp commented 2 years ago

This is a pretty serious issues that the Base.php save() method returns void. E.g. the rest api order docs incorrectly state there will be a response. Getting the response with the new order's id is mission critical for our use case, but I don't see a way to access the updated object.

image

Any contributors able to post a workaround until this is resolved?

MagicLegend commented 2 years ago

@Zelfapp Yeah, as stated in the original message, I have fixed this (along with other improvements to the library) in my fork: https://github.com/MagicLegend/shopify-php-api/commit/d43bc94601dabae8135cd1c35b5f4af3e1fdac23

Zelfapp commented 2 years ago

@MagicLegend I saw your PR. This library has too many issues for a private app at this point and feels too hacky. It has great potential though.

For the sake of anyone else battling this alpha lib for a private app, likely you're better off simply using guzzle to make REST api endpoint requests. E.g. in just few lines of code you can dynamically pass in any endpoint and payload.

Again, this is a great lib, but I would not classify as ready for production.

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

/// Guzzle client
$client = new Client();
$request = new Request(
    'GET',
    "https://$this->shop/admin/api/2022-07/$endpoint",
    [
        'X-Shopify-Access-Token' => $this->permanentAccessToken,
    ],
    $endpointParams['payload'] ?? null,
);
$response = $client->sendAsync($request)->wait();
$response = $response->getBody();
MagicLegend commented 2 years ago

I respectfully disagree, and we have my fork running in production right now. It works fine, it just has some quirks that Shopify is not allocating resources to to solve. I prefer using a library that's providing typing over doing it manually in Guzzle, but both will of course work fine.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 90 days with no activity. It will be closed if no further action occurs in 14 days.

MagicLegend commented 1 year ago

Still waiting on Shopify...

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 60 days with no activity. It will be closed if no further action occurs in 14 days.

MagicLegend commented 1 year ago

Still waiting on Shopify...

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 60 days with no activity. It will be closed if no further action occurs in 14 days.

MagicLegend commented 1 year ago

Still waiting on Shopify...

brrrm commented 1 year ago

I fully agree on this issue. I would like to see PR #185 to be committed.

brrrm commented 1 year ago

On second thought. When you do for instance:


$product = new Product($session);
$product->title = "my awesome product"

echo $product->id // NULL as it's a fresh unsaved product

// now we save it and set the param $updateObject to true
$product->save($updateObject = true);

echo $product->id // 1234567876 !!!!
// our $product object has been replaced with the object from the response.
MagicLegend commented 1 year ago

@brrrm Well, you don't pass $updateObject if that's not what you intended to do, so the save() call will return the updated entity instead of overwriting the existing entity.

brrrm commented 1 year ago

the param $updateObject is not about updating an existing entity or creating a new entity. All it does is update the Object with the returned entity from Shopify's response. Or am I misunderstanding what you mean?

if ($updateObject) {
   $body = $response->getDecodedBody();
   self::createInstance($body[$this->getJsonBodyName()], $this->session, $this);
}
github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 60 days with no activity. It will be closed if no further action occurs in 14 days.

MagicLegend commented 1 year ago

Yeah, and why is it stale bot? Because Shopify doesn't reply ;)

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 60 days with no activity. It will be closed if no further action occurs in 14 days.

MagicLegend commented 1 year ago

Yeah, and why is it stale bot? Because Shopify doesn't reply ;)

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 60 days with no activity. It will be closed if no further action occurs in 14 days.

MagicLegend commented 1 year ago

Yeah, and why is it stale bot? Because Shopify doesn't reply ;)

github-actions[bot] commented 10 months ago

This issue is stale because it has been open for 60 days with no activity. It will be closed if no further action occurs in 14 days.

MagicLegend commented 10 months ago

Yeah, and why is it stale bot? Because Shopify doesn't reply ;)

robthompsonweb commented 10 months ago

FYI for anyone still struggling with this, instead of calling just save(), use saveAndUpdate() and then the original item is populated.

$newFulfillmentServiceResponse = new ShopifyFulfillmentService($session); $newFulfillmentServiceResponse->saveAndUpdate(); $id = $newFulfillmentServiceResponse->id; //this is now populated by using saveAndUpdate over save

github-actions[bot] commented 8 months ago

This issue is stale because it has been open for 60 days with no activity. It will be closed if no further action occurs in 14 days.

MagicLegend commented 8 months ago

Yeah, and why is it stale bot? Because Shopify doesn't reply ;)

github-actions[bot] commented 6 months ago

This issue is stale because it has been open for 60 days with no activity. It will be closed if no further action occurs in 14 days.

MagicLegend commented 6 months ago

Yeah, and why is it stale bot? Because Shopify doesn't reply ;)

github-actions[bot] commented 4 months ago

This issue is stale because it has been open for 60 days with no activity. It will be closed if no further action occurs in 14 days.

MagicLegend commented 4 months ago

Yeah, and why is it stale bot? Because Shopify doesn't reply ;)

github-actions[bot] commented 2 months ago

This issue is stale because it has been open for 60 days with no activity. It will be closed if no further action occurs in 14 days.

MagicLegend commented 2 months ago

Yeah, and why is it stale bot? Because Shopify doesn't reply ;)

paulomarg commented 2 weeks ago

Hey folks, thanks for your patience here. We've removed the stalebot from this repo, and I'm going to add this issue to our tracking.

Once again, thank you for your patience!

MagicLegend commented 1 week ago

No worries. You can find an implementation of this issue in my fork: https://github.com/MagicLegend/shopify-php-api/commit/d43bc94601dabae8135cd1c35b5f4af3e1fdac23