Skip to content
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

implemented file upload capability #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
52 changes: 44 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ public function __construct(
* @param Query|QueryBuilderInterface $query
* @param bool $resultsAsArray
* @param array $variables
* @param array $variables
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we have duplicate $variable definition in docblock?

* @param array $files
*
* @return Results
* @throws QueryError
*/
public function runQuery($query, bool $resultsAsArray = false, array $variables = []): Results
public function runQuery($query, bool $resultsAsArray = false, array $variables = [], array $files = []): Results
{
if ($query instanceof QueryBuilderInterface) {
$query = $query->getQuery();
Expand All @@ -95,31 +97,65 @@ public function runQuery($query, bool $resultsAsArray = false, array $variables
throw new TypeError('Client::runQuery accepts the first argument of type Query or QueryBuilderInterface');
}

return $this->runRawQuery((string) $query, $resultsAsArray, $variables);
return $this->runRawQuery((string) $query, $resultsAsArray, $variables, $files);
}

/**
* @param string $queryString
* @param bool $resultsAsArray
* @param array $variables
* @param array $files
* @param
*
* @return Results
* @throws QueryError
*/
public function runRawQuery(string $queryString, $resultsAsArray = false, array $variables = []): Results
public function runRawQuery(string $queryString, $resultsAsArray = false, array $variables = [], array $files = []): Results
{
$request = new Request($this->requestMethod, $this->endpointUrl);

foreach($this->httpHeaders as $header => $value) {
$request = $request->withHeader($header, $value);
}

// Convert empty variables array to empty json object
if (empty($variables)) $variables = (object) null;
// Set query in the request body
$bodyArray = ['query' => (string) $queryString, 'variables' => $variables];
$request = $request->withBody(Psr7\stream_for(json_encode($bodyArray)));

if (!$files) {
$request = $request->withBody(Psr7\stream_for(json_encode($bodyArray)));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This couples Guzzle tightly to the client. I think stream discovery should be used here. For example with php-http/discovery's \Http\Discovery\Psr17FactoryDiscovery:

\Http\Discovery\Psr17FactoryDiscovery::findStreamFactory()->createStream(\json_encode($bodyArray));

} else {
unset($this->httpHeaders['Content-Type']);

$postDataFields = [];
$map = new \StdClass();

foreach ($files as $key => $content) {
$map->{$key} = [
$content['path']
];

$postDataFields[$key] = [
'name' => $key,
'contents' => fopen($content['file'], 'r'),
];
}
Comment on lines +130 to +139
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please define a new class File to be used to pass file data instead of passing them as array fields.


array_unshift($postDataFields, [
'name' => 'map',
'contents' => json_encode($map),
]);

array_unshift($postDataFields, [
'name' => 'operations',
'contents' => json_encode($bodyArray),
]);

$request = $request->withBody(new Psr7\MultipartStream(
$postDataFields
));
}

foreach($this->httpHeaders as $header => $value) {
$request = $request->withHeader($header, $value);
}

// Send api request and get response
try {
Expand Down