Skip to content

Commit

Permalink
Merge 8dbc46e into c5ddb38
Browse files Browse the repository at this point in the history
  • Loading branch information
moufmouf committed May 3, 2019
2 parents c5ddb38 + 8dbc46e commit b22ca4e
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 23 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -17,7 +17,7 @@
],
"require" : {
"php" : ">=7.1",
"thecodingmachine/graphqlite" : "~3.1.0",
"thecodingmachine/graphqlite" : "~4.0.0",
"webonyx/graphql-php": "^0.13.1"
},
"require-dev": {
Expand Down
28 changes: 10 additions & 18 deletions src/AnyScalar/AnyScalarType.php
Expand Up @@ -16,14 +16,7 @@

final class AnyScalarType extends ScalarType
{
const NAME = 'AnyScalar';

const SCALAR_NODES = [
StringValueNode::class,
BooleanValueNode::class,
IntValueNode::class,
FloatValueNode::class
];
public const NAME = 'AnyScalar';

/**
* @var self
Expand Down Expand Up @@ -86,17 +79,16 @@ public function parseValue($value)
*/
public function parseLiteral($valueNode, ?array $variables = null)
{
$isScalar = false;
foreach (self::SCALAR_NODES as $nodeClass) {
if ($valueNode instanceof $nodeClass) {
$isScalar = true;
break;
}
if ($valueNode instanceof StringValueNode || $valueNode instanceof BooleanValueNode) {
return $valueNode->value;
}

if (!$isScalar) {
throw new Error('Not a valid scalar', $valueNode);
if ($valueNode instanceof IntValueNode) {
return (int) $valueNode->value;
}
return $valueNode->value;
if ($valueNode instanceof FloatValueNode) {
return (float) $valueNode->value;
}

throw new Error('Not a valid scalar', $valueNode);
}
}
19 changes: 19 additions & 0 deletions src/AnyScalar/AnyScalarTypeMapper.php
Expand Up @@ -5,6 +5,7 @@


use GraphQL\Type\Definition\InputType;
use GraphQL\Type\Definition\NamedType;
use GraphQL\Type\Definition\OutputType;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\Type;
Expand All @@ -21,6 +22,7 @@ public function toGraphQLOutputType(Type $type, ?OutputType $subType, Reflection
// AnyScalarType is a class implementing the Webonyx ScalarType type.
return AnyScalarType::getInstance();
}
return null;
}

public function toGraphQLInputType(Type $type, ?InputType $subType, string $argumentName, ReflectionMethod $refMethod, DocBlock $docBlockObj): ?InputType
Expand All @@ -29,5 +31,22 @@ public function toGraphQLInputType(Type $type, ?InputType $subType, string $argu
// AnyScalarType is a class implementing the Webonyx ScalarType type.
return AnyScalarType::getInstance();
}
return null;
}

/**
* Returns a GraphQL type by name.
* If this root type mapper can return this type in "toGraphQLOutputType" or "toGraphQLInputType", it should
* also map these types by name in the "mapNameToType" method.
*
* @param string $typeName The name of the GraphQL type
* @return NamedType|null
*/
public function mapNameToType(string $typeName): ?NamedType
{
if ($typeName === AnyScalarType::NAME) {
return AnyScalarType::getInstance();
}
return null;
}
}
14 changes: 13 additions & 1 deletion tests/AnyScalar/AnyScalarTypeMapperTest.php
Expand Up @@ -2,13 +2,25 @@

namespace TheCodingMachine\GraphQLite\Types\AnyScalar;

use GraphQL\Error\Error;
use GraphQL\Language\AST\SelectionSetNode;
use PHPUnit\Framework\TestCase;

class AnyScalarTypeMapperTest extends TestCase
{
public function testErrorsParseValue()
{
$anyScalarType = new AnyScalarType();

$this->expectException(Error::class);
$anyScalarType->parseValue([]);
}

public function testToGraphQLInputType()
public function testErrorsParseLiterral()
{
$anyScalarType = new AnyScalarType();

$this->expectException(Error::class);
$anyScalarType->parseLiteral(new SelectionSetNode([]));
}
}
17 changes: 17 additions & 0 deletions tests/AnyScalar/AnyScalarTypeTest.php
@@ -0,0 +1,17 @@
<?php

namespace TheCodingMachine\GraphQLite\Types\AnyScalar;

use GraphQL\Error\Error;
use GraphQL\Language\AST\SelectionSetNode;
use PHPUnit\Framework\TestCase;

class AnyScalarTypeTest extends TestCase
{
public function testMapNameToType()
{
$mapper = new AnyScalarTypeMapper();

$this->assertNull($mapper->mapNameToType('foo'));
}
}
8 changes: 8 additions & 0 deletions tests/Fixtures/SomeTestController.php
Expand Up @@ -17,4 +17,12 @@ public function echoScalar($scalar)
{
return $scalar;
}

/**
* @Query()
*/
public function testIgnore(string $foo): string
{
return $foo;
}
}
60 changes: 57 additions & 3 deletions tests/Integration/IntegrationTest.php
Expand Up @@ -9,8 +9,11 @@
use Symfony\Component\Cache\Simple\ArrayCache;
use TheCodingMachine\GraphQLite\Containers\BasicAutoWiringContainer;
use TheCodingMachine\GraphQLite\Containers\EmptyContainer;
use TheCodingMachine\GraphQLite\Mappers\StaticTypeMapper;
use TheCodingMachine\GraphQLite\Schema;
use TheCodingMachine\GraphQLite\SchemaFactory;
use TheCodingMachine\GraphQLite\Types\AnyScalar\AnyScalarType;
use TheCodingMachine\GraphQLite\Types\AnyScalar\AnyScalarTypeMapper;

class IntegrationTest extends TestCase
{
Expand All @@ -25,13 +28,18 @@ public function setUp()
$schemaFactory->addControllerNamespace('TheCodingMachine\GraphQLite\Types\Fixtures');
$schemaFactory->addTypeNamespace('TheCodingMachine\GraphQLite\Types\Fixtures');


$schemaFactory->addRootTypeMapper(new AnyScalarTypeMapper());


$this->schema = $schemaFactory->createSchema();
}

public function testEndToEnd()
{
$this->schema->assertValid();

// Test string
$queryString = '
query {
echoScalar(scalar:"foo")
Expand All @@ -43,11 +51,57 @@ public function testEndToEnd()
$queryString
);

var_dump($result);

$this->assertSame([
'echoScalar' => 'foo'
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);

// Test int
$queryString = '
query {
echoScalar(scalar:42)
}
';

$result = GraphQL::executeQuery(
$this->schema,
$queryString
);

$this->assertSame([
'echoScalar' => 42
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);

// Test float
$queryString = '
query {
echoScalar(scalar:42.42)
}
';

$result = GraphQL::executeQuery(
$this->schema,
$queryString
);

$this->assertSame([
'echoScalar' => 42.42
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);

// Test bool
$queryString = '
query {
echoScalar(scalar:true)
}
';

$result = GraphQL::executeQuery(
$this->schema,
$queryString
);

$this->assertSame([
'echoScalar' => true
], $result->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS)['data']);

}
}
}

0 comments on commit b22ca4e

Please sign in to comment.