diff --git a/examples/01-blog/Blog/Type/Scalar/EmailType.php b/examples/01-blog/Blog/Type/Scalar/EmailType.php index 9800ba4..d7541ea 100644 --- a/examples/01-blog/Blog/Type/Scalar/EmailType.php +++ b/examples/01-blog/Blog/Type/Scalar/EmailType.php @@ -1,6 +1,7 @@ coerceEmail($value); + // Assuming internal representation of email is always correct: + return $value; + + // If it might be incorrect and you want to make sure that only correct values are included in response - + // use following line instead: + // return $this->parseValue($value); } /** - * Parses an externally provided value to use as an input + * Parses an externally provided value (query variable) to use as an input * * @param mixed $value * @return mixed */ public function parseValue($value) - { - return $this->coerceEmail($value); - } - - private function coerceEmail($value) { if (!filter_var($value, FILTER_VALIDATE_EMAIL)) { throw new \UnexpectedValueException("Cannot represent value as email: " . Utils::printSafe($value)); @@ -50,16 +51,22 @@ class EmailType extends BaseType } /** - * Parses an externally provided literal value to use as an input + * Parses an externally provided literal value (hardcoded in GraphQL query) to use as an input * - * @param \GraphQL\Language\AST\Value $valueAST - * @return mixed + * @param \GraphQL\Language\AST\Node $valueAST + * @return string + * @throws Error */ public function parseLiteral($valueAST) { - if ($valueAST instanceof StringValue) { - return $valueAST->value; + // Note: throwing GraphQL\Error\Error vs \UnexpectedValueException to benefit from GraphQL + // error location in query: + if (!$valueAST instanceof StringValue) { + throw new Error('Query error: Can only parse strings got: ' . $valueAST->kind, [$valueAST]); } - return null; + if (!filter_var($valueAST->value, FILTER_VALIDATE_EMAIL)) { + throw new Error("Not a valid email", [$valueAST]); + } + return $valueAST->value; } } diff --git a/examples/01-blog/Blog/Type/Scalar/UrlType.php b/examples/01-blog/Blog/Type/Scalar/UrlType.php index 675e38b..46c282e 100644 --- a/examples/01-blog/Blog/Type/Scalar/UrlType.php +++ b/examples/01-blog/Blog/Type/Scalar/UrlType.php @@ -1,6 +1,8 @@ coerceUrl($value); + // Assuming internal representation of url is always correct: + return $value; + + // If it might be incorrect and you want to make sure that only correct values are included in response - + // use following line instead: + // return $this->parseValue($value); } /** - * Parses an externally provided value to use as an input + * Parses an externally provided value (query variable) to use as an input * * @param mixed $value * @return mixed */ public function parseValue($value) - { - return $this->coerceUrl($value); - } - - /** - * @param $value - * @return float|null - */ - private function coerceUrl($value) { if (!is_string($value) || !filter_var($value, FILTER_VALIDATE_URL)) { // quite naive, but after all this is example throw new \UnexpectedValueException("Cannot represent value as URL: " . Utils::printSafe($value)); @@ -44,13 +42,22 @@ class UrlType extends ScalarType } /** + * Parses an externally provided literal value to use as an input (e.g. in Query AST) + * + * @param $ast Node * @return null|string + * @throws Error */ public function parseLiteral($ast) { - if ($ast instanceof StringValue) { - return $ast->value; + // Note: throwing GraphQL\Error\Error vs \UnexpectedValueException to benefit from GraphQL + // error location in query: + if (!($ast instanceof StringValue)) { + throw new Error('Query error: Can only parse strings got: ' . $ast->kind, [$ast]); } - return null; + if (!is_string($ast->value) || !filter_var($ast->value, FILTER_VALIDATE_URL)) { + throw new Error('Query error: Not a valid URL', [$ast]); + } + return $ast->value; } }