Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/Webhooks/DeliveryMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,21 @@ abstract public function parseCheckQueryResult(array $body): array;
*
* @return string
*/
public function buildRegisterQuery(
string $topic,
string $callbackAddress,
?string $webhookId = null
): string {
$mutationName = $this->getMutationName($webhookId);
$identifier = $webhookId ? "id: \"$webhookId\"" : "topic: $topic";
$webhookSubscriptionArgs = $this->queryEndpoint($callbackAddress);
public function buildRegisterQuery(string $topic, string $callbackAddress, ?string $webhookId, array $fields = [], array $metafieldNamespaces = []): string
{
$fieldsQuery = !empty($fields) ? 'fields: [' . implode(',', $fields) . ']' : '';
$metafieldNamespacesQuery = !empty($metafieldNamespaces) ? 'metafieldNamespaces: [' . implode(',', $metafieldNamespaces) . ']' : '';

return <<<QUERY
mutation webhookSubscription {
$mutationName($identifier, webhookSubscription: $webhookSubscriptionArgs) {
mutation {
webhookSubscriptionCreate(
Comment thread
admirsaheta marked this conversation as resolved.
Outdated
topic: "$topic",
webhookSubscription: {
callbackUrl: "$callbackAddress"
$fieldsQuery
$metafieldNamespacesQuery
}
) {
userErrors {
field
message
Expand All @@ -77,9 +80,10 @@ public function buildRegisterQuery(
}
}
}
QUERY;
QUERY;
}


/**
* Checks if the given result was successful.
*
Expand Down
16 changes: 12 additions & 4 deletions src/Webhooks/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public static function register(
string $topic,
string $shop,
string $accessToken,
?string $deliveryMethod = self::DELIVERY_METHOD_HTTP
?string $deliveryMethod = self::DELIVERY_METHOD_HTTP,
array $fields = [],
Comment thread
admirsaheta marked this conversation as resolved.
array $metafieldNamespaces = []
): RegisterResponse {
/** @var DeliveryMethod */
$method = null;
Expand Down Expand Up @@ -114,7 +116,9 @@ public static function register(
$topic,
$callbackAddress,
$method,
$webhookId
$webhookId,
$fields,
$metafieldNamespaces
);
$registered = $method->isSuccess($body, $webhookId);
}
Expand Down Expand Up @@ -228,10 +232,14 @@ private static function sendRegisterRequest(
string $topic,
string $callbackAddress,
DeliveryMethod $deliveryMethod,
?string $webhookId
?string $webhookId,
array $fields = [],
array $metafieldNamespaces = []
): array {
$registerQuery = $deliveryMethod->buildRegisterQuery($topic, $callbackAddress, $webhookId, $fields, $metafieldNamespaces);

$registerResponse = $client->query(
data: $deliveryMethod->buildRegisterQuery($topic, $callbackAddress, $webhookId),
data: $registerQuery,
);

$statusCode = $registerResponse->getStatusCode();
Expand Down
7 changes: 4 additions & 3 deletions tests/Clients/GraphqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,12 @@ public function testCanQueryWithExtraHeaders()
public function testProxyForwardsBodyAsJsonType()
{
$queryToProxy = <<<QUERY
<<<QUERY
{
"variables": {},
"query": "{\nshop {\n name\n __typename\n }\n}"
"variables": {},
"query": "{\nshop {\n name\n __typename\n }\n}"
}
QUERY;
QUERY; // phpcs:ignore

$extraHeaders = ['Extra-Extra' => 'hear_all_about_it'];
$client = new Graphql($this->domain, 'token');
Expand Down