-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from PerimeterX/dev
Dev
- Loading branch information
Showing
8 changed files
with
154 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Perimeterx; | ||
|
||
class GraphqlExtractor { | ||
/** | ||
* @return GraphqlFields | ||
*/ | ||
public static function ExtractGraphqlFields() { | ||
$postBody = json_decode(PerimeterxUtils::getPostRequestBody(), true); | ||
if (!isset($postBody)) { | ||
return null; | ||
} | ||
|
||
$query = GraphqlExtractor::extractGraphqlQuery($postBody); | ||
$queryArray = preg_split('/[^A-Za-z0-9_]/', $query); | ||
|
||
$operationType = GraphqlExtractor::extractOperationType($query, $queryArray); | ||
$operationName = GraphqlExtractor::extractOperationName($postBody, $queryArray); | ||
|
||
return new GraphqlFields($operationType, $operationName); | ||
} | ||
|
||
private static function extractGraphqlQuery(&$postRequestBody) { | ||
if (!array_key_exists('query', $postRequestBody)) { | ||
return ''; | ||
} | ||
|
||
return $postRequestBody['query']; | ||
} | ||
|
||
private static function extractOperationType(&$query, &$queryArray) { | ||
$isGraphqlShorthand = $query[0] === '{'; | ||
if ($isGraphqlShorthand) { | ||
return 'query'; | ||
} | ||
|
||
$operationType = $queryArray[0]; | ||
if (!in_array($operationType, ['query', 'mutation', 'subscription'])) { | ||
$operationType = 'query'; | ||
} | ||
|
||
return $operationType; | ||
} | ||
|
||
private static function extractOperationName(&$postBody, &$queryArray) { | ||
if (array_key_exists('operationName', $postBody)) { | ||
return $postBody['operationName']; | ||
} | ||
|
||
if (!in_array($queryArray[0], ['query', 'mutation', 'subscription'])) { | ||
return $queryArray[0]; | ||
} | ||
|
||
if ($queryArray[1]) { | ||
return $queryArray[1]; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Perimeterx; | ||
|
||
class GraphqlFields { | ||
private $operationType; | ||
private $operationName; | ||
|
||
/** | ||
* @param string $operationType | ||
* @param string $operationName | ||
*/ | ||
public function __construct($operationType, $operationName) { | ||
$this->operationType = $operationType; | ||
$this->operationName = $operationName; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getOperationType() { | ||
return $this->operationType; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getOperationName() { | ||
return $this->operationName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters