mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-22 21:06:05 +03:00
Fix astFromValue to correctly handle integers and strings
This commit is contained in:
parent
49ec89b28f
commit
fcb9c24bb5
@ -39,6 +39,8 @@ use GraphQL\Type\Schema;
|
|||||||
*/
|
*/
|
||||||
class AST
|
class AST
|
||||||
{
|
{
|
||||||
|
public static $integerStringRegExp = '/^-?(0|[1-9][0-9]*)$/';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert representation of AST as an associative array to instance of GraphQL\Language\AST\Node.
|
* Convert representation of AST as an associative array to instance of GraphQL\Language\AST\Node.
|
||||||
*
|
*
|
||||||
@ -250,7 +252,7 @@ class AST
|
|||||||
// Use json_encode, which uses the same string encoding as GraphQL,
|
// Use json_encode, which uses the same string encoding as GraphQL,
|
||||||
// then remove the quotes.
|
// then remove the quotes.
|
||||||
return new StringValueNode([
|
return new StringValueNode([
|
||||||
'value' => substr(json_encode($serialized), 1, -1)
|
'value' => $serialized
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,6 +40,7 @@ class AstFromValueTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testConvertsIntValuesToASTs()
|
public function testConvertsIntValuesToASTs()
|
||||||
{
|
{
|
||||||
|
$this->assertEquals(new IntValueNode(['value' => '-1']), AST::astFromValue(-1, Type::int()));
|
||||||
$this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123.0, Type::int()));
|
$this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123.0, Type::int()));
|
||||||
$this->assertEquals(new IntValueNode(['value' => '10000']), AST::astFromValue(1e4, Type::int()));
|
$this->assertEquals(new IntValueNode(['value' => '10000']), AST::astFromValue(1e4, Type::int()));
|
||||||
$this->assertEquals(new IntValueNode(['value' => '0']), AST::astFromValue(0e4, Type::int()));
|
$this->assertEquals(new IntValueNode(['value' => '0']), AST::astFromValue(0e4, Type::int()));
|
||||||
@ -66,6 +67,7 @@ class AstFromValueTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testConvertsFloatValuesToIntOrFloatASTs()
|
public function testConvertsFloatValuesToIntOrFloatASTs()
|
||||||
{
|
{
|
||||||
|
$this->assertEquals(new IntValueNode(['value' => '-1']), AST::astFromValue(-1, Type::float()));
|
||||||
$this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123, Type::float()));
|
$this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123, Type::float()));
|
||||||
$this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123.0, Type::float()));
|
$this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123.0, Type::float()));
|
||||||
$this->assertEquals(new FloatValueNode(['value' => '123.5']), AST::astFromValue(123.5, Type::float()));
|
$this->assertEquals(new FloatValueNode(['value' => '123.5']), AST::astFromValue(123.5, Type::float()));
|
||||||
@ -81,7 +83,7 @@ class AstFromValueTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->assertEquals(new StringValueNode(['value' => 'hello']), AST::astFromValue('hello', Type::string()));
|
$this->assertEquals(new StringValueNode(['value' => 'hello']), AST::astFromValue('hello', Type::string()));
|
||||||
$this->assertEquals(new StringValueNode(['value' => 'VALUE']), AST::astFromValue('VALUE', Type::string()));
|
$this->assertEquals(new StringValueNode(['value' => 'VALUE']), AST::astFromValue('VALUE', Type::string()));
|
||||||
$this->assertEquals(new StringValueNode(['value' => 'VA\\nLUE']), AST::astFromValue("VA\nLUE", Type::string()));
|
$this->assertEquals(new StringValueNode(['value' => "VA\nLUE"]), AST::astFromValue("VA\nLUE", Type::string()));
|
||||||
$this->assertEquals(new StringValueNode(['value' => '123']), AST::astFromValue(123, Type::string()));
|
$this->assertEquals(new StringValueNode(['value' => '123']), AST::astFromValue(123, Type::string()));
|
||||||
$this->assertEquals(new StringValueNode(['value' => 'false']), AST::astFromValue(false, Type::string()));
|
$this->assertEquals(new StringValueNode(['value' => 'false']), AST::astFromValue(false, Type::string()));
|
||||||
$this->assertEquals(new NullValueNode([]), AST::astFromValue(null, Type::string()));
|
$this->assertEquals(new NullValueNode([]), AST::astFromValue(null, Type::string()));
|
||||||
@ -95,8 +97,11 @@ class AstFromValueTest extends TestCase
|
|||||||
{
|
{
|
||||||
$this->assertEquals(new StringValueNode(['value' => 'hello']), AST::astFromValue('hello', Type::id()));
|
$this->assertEquals(new StringValueNode(['value' => 'hello']), AST::astFromValue('hello', Type::id()));
|
||||||
$this->assertEquals(new StringValueNode(['value' => 'VALUE']), AST::astFromValue('VALUE', Type::id()));
|
$this->assertEquals(new StringValueNode(['value' => 'VALUE']), AST::astFromValue('VALUE', Type::id()));
|
||||||
$this->assertEquals(new StringValueNode(['value' => 'VA\\nLUE']), AST::astFromValue("VA\nLUE", Type::id()));
|
$this->assertEquals(new StringValueNode(['value' => "VA\nLUE"]), AST::astFromValue("VA\nLUE", Type::id()));
|
||||||
|
$this->assertEquals(new IntValueNode(['value' => '-1']), AST::astFromValue(-1, Type::id()));
|
||||||
$this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123, Type::id()));
|
$this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue(123, Type::id()));
|
||||||
|
$this->assertEquals(new IntValueNode(['value' => '123']), AST::astFromValue('123', Type::id()));
|
||||||
|
$this->assertEquals(new StringValueNode(['value' => '01']), AST::astFromValue('01', Type::id()));
|
||||||
$this->assertEquals(new StringValueNode(['value' => 'false']), AST::astFromValue(false, Type::id()));
|
$this->assertEquals(new StringValueNode(['value' => 'false']), AST::astFromValue(false, Type::id()));
|
||||||
$this->assertEquals(new NullValueNode([]), AST::astFromValue(null, Type::id()));
|
$this->assertEquals(new NullValueNode([]), AST::astFromValue(null, Type::id()));
|
||||||
$this->assertEquals(null, AST::astFromValue(null, Type::nonNull(Type::id())));
|
$this->assertEquals(null, AST::astFromValue(null, Type::nonNull(Type::id())));
|
||||||
|
@ -27,7 +27,7 @@ class SchemaPrinterTest extends TestCase
|
|||||||
private function printSingleFieldSchema($fieldConfig)
|
private function printSingleFieldSchema($fieldConfig)
|
||||||
{
|
{
|
||||||
$query = new ObjectType([
|
$query = new ObjectType([
|
||||||
'name' => 'Query',
|
'name' => 'Root',
|
||||||
'fields' => [
|
'fields' => [
|
||||||
'singleField' => $fieldConfig
|
'singleField' => $fieldConfig
|
||||||
]
|
]
|
||||||
@ -44,7 +44,11 @@ class SchemaPrinterTest extends TestCase
|
|||||||
'type' => Type::string()
|
'type' => Type::string()
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
singleField: String
|
singleField: String
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
@ -59,7 +63,11 @@ type Query {
|
|||||||
'type' => Type::listOf(Type::string())
|
'type' => Type::listOf(Type::string())
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
singleField: [String]
|
singleField: [String]
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
@ -74,7 +82,11 @@ type Query {
|
|||||||
'type' => Type::nonNull(Type::string())
|
'type' => Type::nonNull(Type::string())
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
singleField: String!
|
singleField: String!
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
@ -89,7 +101,11 @@ type Query {
|
|||||||
'type' => Type::nonNull(Type::listOf(Type::string()))
|
'type' => Type::nonNull(Type::listOf(Type::string()))
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
singleField: [String]!
|
singleField: [String]!
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
@ -104,7 +120,11 @@ type Query {
|
|||||||
'type' => Type::listOf(Type::nonNull(Type::string()))
|
'type' => Type::listOf(Type::nonNull(Type::string()))
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
singleField: [String!]
|
singleField: [String!]
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
@ -119,7 +139,11 @@ type Query {
|
|||||||
'type' => Type::nonNull(Type::listOf(Type::nonNull(Type::string())))
|
'type' => Type::nonNull(Type::listOf(Type::nonNull(Type::string())))
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
singleField: [String!]!
|
singleField: [String!]!
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
@ -167,7 +191,11 @@ type Root {
|
|||||||
'args' => ['argOne' => ['type' => Type::int()]]
|
'args' => ['argOne' => ['type' => Type::int()]]
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
singleField(argOne: Int): String
|
singleField(argOne: Int): String
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
@ -183,12 +211,36 @@ type Query {
|
|||||||
'args' => ['argOne' => ['type' => Type::int(), 'defaultValue' => 2]]
|
'args' => ['argOne' => ['type' => Type::int(), 'defaultValue' => 2]]
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
singleField(argOne: Int = 2): String
|
singleField(argOne: Int = 2): String
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @it Prints String Field With String Arg With Default
|
||||||
|
*/
|
||||||
|
public function testPrintsStringFieldWithStringArgWithDefault()
|
||||||
|
{
|
||||||
|
$output = $this->printSingleFieldSchema([
|
||||||
|
'type' => Type::string(),
|
||||||
|
'args' => ['argOne' => ['type' => Type::string(), 'defaultValue' => "tes\t de\fault"]],
|
||||||
|
]);
|
||||||
|
$this->assertEquals('
|
||||||
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
|
singleField(argOne: String = "tes\t de\fault"): String
|
||||||
|
}
|
||||||
|
', $output);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @it Prints String Field With Int Arg With Default Null
|
* @it Prints String Field With Int Arg With Default Null
|
||||||
*/
|
*/
|
||||||
@ -199,7 +251,11 @@ type Query {
|
|||||||
'args' => ['argOne' => ['type' => Type::int(), 'defaultValue' => null]]
|
'args' => ['argOne' => ['type' => Type::int(), 'defaultValue' => null]]
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
singleField(argOne: Int = null): String
|
singleField(argOne: Int = null): String
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
@ -215,7 +271,11 @@ type Query {
|
|||||||
'args' => ['argOne' => ['type' => Type::nonNull(Type::int())]]
|
'args' => ['argOne' => ['type' => Type::nonNull(Type::int())]]
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
singleField(argOne: Int!): String
|
singleField(argOne: Int!): String
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
@ -234,7 +294,11 @@ type Query {
|
|||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
singleField(argOne: Int, argTwo: String): String
|
singleField(argOne: Int, argTwo: String): String
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
@ -254,7 +318,11 @@ type Query {
|
|||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
singleField(argOne: Int = 1, argTwo: String, argThree: Boolean): String
|
singleField(argOne: Int = 1, argTwo: String, argThree: Boolean): String
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
@ -274,7 +342,11 @@ type Query {
|
|||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
singleField(argOne: Int, argTwo: String = "foo", argThree: Boolean): String
|
singleField(argOne: Int, argTwo: String = "foo", argThree: Boolean): String
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
@ -294,7 +366,11 @@ type Query {
|
|||||||
]
|
]
|
||||||
]);
|
]);
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
singleField(argOne: Int, argTwo: String, argThree: Boolean = false): String
|
singleField(argOne: Int, argTwo: String, argThree: Boolean = false): String
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
@ -619,13 +695,17 @@ type Query {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
"""This field is awesome"""
|
"""This field is awesome"""
|
||||||
singleField: String
|
singleField: String
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
|
|
||||||
$recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query'];
|
$recreatedRoot = BuildSchema::build($output)->getTypeMap()['Root'];
|
||||||
$recreatedField = $recreatedRoot->getFields()['singleField'];
|
$recreatedField = $recreatedRoot->getFields()['singleField'];
|
||||||
$this->assertEquals($description, $recreatedField->description);
|
$this->assertEquals($description, $recreatedField->description);
|
||||||
}
|
}
|
||||||
@ -642,7 +722,11 @@ type Query {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
"""
|
"""
|
||||||
This field is "awesome"
|
This field is "awesome"
|
||||||
"""
|
"""
|
||||||
@ -650,7 +734,7 @@ type Query {
|
|||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
|
|
||||||
$recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query'];
|
$recreatedRoot = BuildSchema::build($output)->getTypeMap()['Root'];
|
||||||
$recreatedField = $recreatedRoot->getFields()['singleField'];
|
$recreatedField = $recreatedRoot->getFields()['singleField'];
|
||||||
$this->assertEquals($description, $recreatedField->description);
|
$this->assertEquals($description, $recreatedField->description);
|
||||||
}
|
}
|
||||||
@ -667,14 +751,18 @@ type Query {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertEquals('
|
$this->assertEquals('
|
||||||
type Query {
|
schema {
|
||||||
|
query: Root
|
||||||
|
}
|
||||||
|
|
||||||
|
type Root {
|
||||||
""" This field is "awesome"
|
""" This field is "awesome"
|
||||||
"""
|
"""
|
||||||
singleField: String
|
singleField: String
|
||||||
}
|
}
|
||||||
', $output);
|
', $output);
|
||||||
|
|
||||||
$recreatedRoot = BuildSchema::build($output)->getTypeMap()['Query'];
|
$recreatedRoot = BuildSchema::build($output)->getTypeMap()['Root'];
|
||||||
$recreatedField = $recreatedRoot->getFields()['singleField'];
|
$recreatedField = $recreatedRoot->getFields()['singleField'];
|
||||||
$this->assertEquals($description, $recreatedField->description);
|
$this->assertEquals($description, $recreatedField->description);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user