-
Notifications
You must be signed in to change notification settings - Fork 90
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,11 +81,13 @@ public function __construct( | |
* @param Query|QueryBuilderInterface $query | ||
* @param bool $resultsAsArray | ||
* @param array $variables | ||
* @param array $variables | ||
* @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(); | ||
|
@@ -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))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 \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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please define a new class |
||
|
||
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 { | ||
|
There was a problem hiding this comment.
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?