55namespace Shopify \Webhooks ;
66
77use Exception ;
8- use Shopify \Clients \Http ;
8+ use Shopify \Clients \Graphql ;
99use Shopify \Clients \HttpHeaders ;
1010use Shopify \Context ;
1111use Shopify \Exception \InvalidArgumentException ;
@@ -85,13 +85,11 @@ public static function register(
8585
8686 $ callbackAddress = $ method ->getCallbackAddress ($ path );
8787
88- // TODO Refactor this to use the GraphQL client once that's available
89- $ client = new Http (Utils::sanitizeShopDomain ($ shop ));
88+ $ client = new Graphql (Utils::sanitizeShopDomain ($ shop ), $ accessToken );
9089
9190 list ($ webhookId , $ mustRegister ) = self ::isWebhookRegistrationNeeded (
9291 $ client ,
9392 $ topic ,
94- $ accessToken ,
9593 $ callbackAddress ,
9694 $ method
9795 );
@@ -104,7 +102,6 @@ public static function register(
104102 $ topic ,
105103 $ callbackAddress ,
106104 $ method ,
107- $ accessToken ,
108105 $ webhookId
109106 );
110107 $ registered = $ method ->isSuccess ($ body , $ webhookId );
@@ -160,31 +157,24 @@ public static function process(array $rawHeaders, string $rawBody): ProcessRespo
160157 * Checks if Shopify already has a callback set for this webhook via a GraphQL check, and checks if we need to
161158 * update our subscription if one exists.
162159 *
163- * @param Http $client
160+ * @param \Shopify\Clients\Graphql $client
164161 * @param string $topic
165- * @param string $accessToken
166162 * @param string $callbackAddress
167163 * @param \Shopify\Webhooks\DeliveryMethod $method
168164 *
169165 * @return array
170166 *
171- * @throws \Psr\Http\Client\ClientExceptionInterface
172- * @throws \Shopify\Exception\UninitializedContextException
167+ * @throws \Shopify\Exception\HttpRequestException
168+ * @throws \Shopify\Exception\MissingArgumentException
173169 * @throws \Shopify\Exception\WebhookRegistrationException
174170 */
175171 private static function isWebhookRegistrationNeeded (
176- Http $ client ,
172+ Graphql $ client ,
177173 string $ topic ,
178- string $ accessToken ,
179174 string $ callbackAddress ,
180175 DeliveryMethod $ method
181176 ): array {
182- $ checkResponse = $ client ->post (
183- path: 'admin/api/ ' . Context::$ API_VERSION . '/graphql.json ' ,
184- body: $ method ->buildCheckQuery ($ topic ),
185- dataType: Http::DATA_TYPE_GRAPHQL ,
186- headers: ['X-Shopify-Access-Token ' => $ accessToken ],
187- );
177+ $ checkResponse = $ client ->query (data: $ method ->buildCheckQuery ($ topic ));
188178
189179 $ checkStatusCode = $ checkResponse ->getStatusCode ();
190180 $ checkBody = $ checkResponse ->getDecodedBody ();
@@ -208,32 +198,27 @@ private static function isWebhookRegistrationNeeded(
208198 /**
209199 * Creates or updates a webhook subscription in Shopify by firing the appropriate GraphQL query.
210200 *
211- * @param Http $client
201+ * @param \Shopify\Clients\Graphql $client
212202 * @param string $topic
213203 * @param string $callbackAddress
214204 * @param \Shopify\Webhooks\DeliveryMethod $deliveryMethod
215- * @param string $accessToken
216205 * @param string|null $webhookId
217206 *
218207 * @return array
219208 *
220- * @throws \Psr\Http\Client\ClientExceptionInterface
221- * @throws \Shopify\Exception\UninitializedContextException
209+ * @throws \Shopify\Exception\HttpRequestException
210+ * @throws \Shopify\Exception\MissingArgumentException
222211 * @throws \Shopify\Exception\WebhookRegistrationException
223212 */
224213 private static function sendRegisterRequest (
225- Http $ client ,
214+ Graphql $ client ,
226215 string $ topic ,
227216 string $ callbackAddress ,
228217 DeliveryMethod $ deliveryMethod ,
229- string $ accessToken ,
230218 ?string $ webhookId ,
231219 ): array {
232- $ registerResponse = $ client ->post (
233- path: 'admin/api/ ' . Context::$ API_VERSION . '/graphql.json ' ,
234- body: $ deliveryMethod ->buildRegisterQuery ($ topic , $ callbackAddress , $ webhookId ),
235- dataType: Http::DATA_TYPE_GRAPHQL ,
236- headers: ['X-Shopify-Access-Token ' => $ accessToken ],
220+ $ registerResponse = $ client ->query (
221+ data: $ deliveryMethod ->buildRegisterQuery ($ topic , $ callbackAddress , $ webhookId ),
237222 );
238223
239224 $ statusCode = $ registerResponse ->getStatusCode ();
0 commit comments