Skip to content

Add parameter $variables to the GraphQL::post() #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"type": "library",
"require": {
"php": ">=5.6",
"ext-curl": "*"
"ext-curl": "*",
"ext-json": "*"
},
"license": "Apache-2.0",
"authors": [
Expand Down
5 changes: 3 additions & 2 deletions lib/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ protected function getResourcePath()
* @param string $graphQL A valid GraphQL String. @see https://help.shopify.com/en/api/graphql-admin-api/graphiql-builder GraphiQL builder - you can build your graphql string from here.
* @param string $url
* @param bool $wrapData
* @param array|null $variables
*
* @uses HttpRequestGraphQL::post() to send the HTTP request
*
* @return array
*/
public function post($graphQL, $url = null, $wrapData = false)
public function post($graphQL, $url = null, $wrapData = false, $variables = null)
{
if (!$url) $url = $this->generateUrl();

$response = HttpRequestGraphQL::post($url, $graphQL, $this->httpHeaders);
$response = HttpRequestGraphQL::post($url, $graphQL, $this->httpHeaders, $variables);

return $this->processResponse($response);
}
Expand Down
17 changes: 12 additions & 5 deletions lib/HttpRequestGraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ class HttpRequestGraphQL extends HttpRequestJson
/**
* Prepare the data and request headers before making the call
*
* @param mixed $data
* @param array $httpHeaders
* @param mixed $data
* @param array|null $variables
*
* @return void
*
* @throws SdkException if $data is not a string
*/
protected static function prepareRequest($httpHeaders = array(), $data = array())
protected static function prepareRequest($httpHeaders = array(), $data = array(), $variables = null)
{

if (is_string($data)) {
Expand All @@ -44,8 +45,13 @@ protected static function prepareRequest($httpHeaders = array(), $data = array()
}

self::$httpHeaders = $httpHeaders;
self::$httpHeaders['Content-type'] = 'application/graphql';

if (is_array($variables)) {
self::$postDataGraphQL = json_encode(['query' => $data, 'variables' => $variables]);
self::$httpHeaders['Content-type'] = 'application/json';
} else {
self::$httpHeaders['Content-type'] = 'application/graphql';
}
}

/**
Expand All @@ -54,12 +60,13 @@ protected static function prepareRequest($httpHeaders = array(), $data = array()
* @param string $url
* @param mixed $data
* @param array $httpHeaders
* @param array|null $variables
*
* @return string
*/
public static function post($url, $data, $httpHeaders = array())
public static function post($url, $data, $httpHeaders = array(), $variables = null)
{
self::prepareRequest($httpHeaders, $data);
self::prepareRequest($httpHeaders, $data, $variables);

$response = CurlRequest::post($url, self::$postDataGraphQL, self::$httpHeaders);

Expand Down
2 changes: 1 addition & 1 deletion lib/HttpRequestJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class HttpRequestJson
/**
* Prepare the data and request headers before making the call
*
* @param array $dataArray
* @param array $httpHeaders
* @param array $dataArray
*
* @return void
*/
Expand Down