mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-25 14:26:08 +03:00
commit
bfff27ef34
@ -1,8 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Tests;
|
||||
|
||||
use function array_map;
|
||||
|
||||
class StarWarsData
|
||||
{
|
||||
/**
|
||||
* Helper function to get a character by ID.
|
||||
*/
|
||||
public static function getCharacter($id)
|
||||
{
|
||||
$humans = self::humans();
|
||||
$droids = self::droids();
|
||||
if (isset($humans[$id])) {
|
||||
return $humans[$id];
|
||||
}
|
||||
if (isset($droids[$id])) {
|
||||
return $droids[$id];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function humans()
|
||||
{
|
||||
return [
|
||||
'1000' => self::luke(),
|
||||
'1001' => self::vader(),
|
||||
'1002' => self::han(),
|
||||
'1003' => self::leia(),
|
||||
'1004' => self::tarkin(),
|
||||
];
|
||||
}
|
||||
|
||||
private static function luke()
|
||||
{
|
||||
return [
|
||||
@ -56,14 +89,11 @@ class StarWarsData
|
||||
];
|
||||
}
|
||||
|
||||
static function humans()
|
||||
public static function droids()
|
||||
{
|
||||
return [
|
||||
'1000' => self::luke(),
|
||||
'1001' => self::vader(),
|
||||
'1002' => self::han(),
|
||||
'1003' => self::leia(),
|
||||
'1004' => self::tarkin(),
|
||||
'2000' => self::threepio(),
|
||||
'2001' => self::artoo(),
|
||||
];
|
||||
}
|
||||
|
||||
@ -82,7 +112,7 @@ class StarWarsData
|
||||
* We export artoo directly because the schema returns him
|
||||
* from a root field, and hence needs to reference him.
|
||||
*/
|
||||
static function artoo()
|
||||
private static function artoo()
|
||||
{
|
||||
return [
|
||||
|
||||
@ -94,48 +124,25 @@ class StarWarsData
|
||||
];
|
||||
}
|
||||
|
||||
static function droids()
|
||||
{
|
||||
return [
|
||||
'2000' => self::threepio(),
|
||||
'2001' => self::artoo(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get a character by ID.
|
||||
*/
|
||||
static function getCharacter($id)
|
||||
{
|
||||
$humans = self::humans();
|
||||
$droids = self::droids();
|
||||
if (isset($humans[$id])) {
|
||||
return $humans[$id];
|
||||
}
|
||||
if (isset($droids[$id])) {
|
||||
return $droids[$id];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows us to query for a character's friends.
|
||||
*/
|
||||
static function getFriends($character)
|
||||
public static function getFriends($character)
|
||||
{
|
||||
return array_map([__CLASS__, 'getCharacter'], $character['friends']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $episode
|
||||
* @return array
|
||||
* @param int $episode
|
||||
* @return mixed[]
|
||||
*/
|
||||
static function getHero($episode)
|
||||
public static function getHero($episode)
|
||||
{
|
||||
if ($episode === 5) {
|
||||
// Luke is the hero of Episode V.
|
||||
return self::luke();
|
||||
}
|
||||
|
||||
// Artoo is the hero otherwise.
|
||||
return self::artoo();
|
||||
}
|
||||
@ -144,19 +151,21 @@ class StarWarsData
|
||||
* @param $id
|
||||
* @return mixed|null
|
||||
*/
|
||||
static function getHuman($id)
|
||||
public static function getHuman($id)
|
||||
{
|
||||
$humans = self::humans();
|
||||
return isset($humans[$id]) ? $humans[$id] : null;
|
||||
|
||||
return $humans[$id] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @return mixed|null
|
||||
*/
|
||||
static function getDroid($id)
|
||||
public static function getDroid($id)
|
||||
{
|
||||
$droids = self::droids();
|
||||
return isset($droids[$id]) ? $droids[$id] : null;
|
||||
|
||||
return $droids[$id] ?? null;
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Tests;
|
||||
|
||||
use GraphQL\GraphQL;
|
||||
@ -44,12 +47,20 @@ class StarWarsIntrospectionTest extends TestCase
|
||||
['name' => '__EnumValue'],
|
||||
['name' => '__Directive'],
|
||||
['name' => '__DirectiveLocation'],
|
||||
]
|
||||
]
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to test a query and the expected response.
|
||||
*/
|
||||
private function assertValidQuery($query, $expected) : void
|
||||
{
|
||||
$this->assertEquals(['data' => $expected], GraphQL::executeQuery(StarWarsSchema::build(), $query)->toArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* @see it('Allows querying the schema for query type')
|
||||
*/
|
||||
@ -66,10 +77,8 @@ class StarWarsIntrospectionTest extends TestCase
|
||||
';
|
||||
$expected = [
|
||||
'__schema' => [
|
||||
'queryType' => [
|
||||
'name' => 'Query'
|
||||
'queryType' => ['name' => 'Query'],
|
||||
],
|
||||
]
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
@ -87,9 +96,7 @@ class StarWarsIntrospectionTest extends TestCase
|
||||
}
|
||||
';
|
||||
$expected = [
|
||||
'__type' => [
|
||||
'name' => 'Droid'
|
||||
]
|
||||
'__type' => ['name' => 'Droid'],
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
@ -110,8 +117,8 @@ class StarWarsIntrospectionTest extends TestCase
|
||||
$expected = [
|
||||
'__type' => [
|
||||
'name' => 'Droid',
|
||||
'kind' => 'OBJECT'
|
||||
]
|
||||
'kind' => 'OBJECT',
|
||||
],
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
@ -132,8 +139,8 @@ class StarWarsIntrospectionTest extends TestCase
|
||||
$expected = [
|
||||
'__type' => [
|
||||
'name' => 'Character',
|
||||
'kind' => 'INTERFACE'
|
||||
]
|
||||
'kind' => 'INTERFACE',
|
||||
],
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
@ -165,46 +172,46 @@ class StarWarsIntrospectionTest extends TestCase
|
||||
'name' => 'id',
|
||||
'type' => [
|
||||
'name' => null,
|
||||
'kind' => 'NON_NULL'
|
||||
]
|
||||
'kind' => 'NON_NULL',
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'name',
|
||||
'type' => [
|
||||
'name' => 'String',
|
||||
'kind' => 'SCALAR'
|
||||
]
|
||||
'kind' => 'SCALAR',
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'friends',
|
||||
'type' => [
|
||||
'name' => null,
|
||||
'kind' => 'LIST'
|
||||
]
|
||||
'kind' => 'LIST',
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'appearsIn',
|
||||
'type' => [
|
||||
'name' => null,
|
||||
'kind' => 'LIST'
|
||||
]
|
||||
'kind' => 'LIST',
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'secretBackstory',
|
||||
'type' => [
|
||||
'name' => 'String',
|
||||
'kind' => 'SCALAR'
|
||||
]
|
||||
'kind' => 'SCALAR',
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'primaryFunction',
|
||||
'type' => [
|
||||
'name' => 'String',
|
||||
'kind' => 'SCALAR'
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
'kind' => 'SCALAR',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
@ -243,17 +250,17 @@ class StarWarsIntrospectionTest extends TestCase
|
||||
'kind' => 'NON_NULL',
|
||||
'ofType' => [
|
||||
'name' => 'String',
|
||||
'kind' => 'SCALAR'
|
||||
]
|
||||
]
|
||||
'kind' => 'SCALAR',
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'name',
|
||||
'type' => [
|
||||
'name' => 'String',
|
||||
'kind' => 'SCALAR',
|
||||
'ofType' => null
|
||||
]
|
||||
'ofType' => null,
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'friends',
|
||||
@ -262,9 +269,9 @@ class StarWarsIntrospectionTest extends TestCase
|
||||
'kind' => 'LIST',
|
||||
'ofType' => [
|
||||
'name' => 'Character',
|
||||
'kind' => 'INTERFACE'
|
||||
]
|
||||
]
|
||||
'kind' => 'INTERFACE',
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'appearsIn',
|
||||
@ -273,28 +280,28 @@ class StarWarsIntrospectionTest extends TestCase
|
||||
'kind' => 'LIST',
|
||||
'ofType' => [
|
||||
'name' => 'Episode',
|
||||
'kind' => 'ENUM'
|
||||
]
|
||||
]
|
||||
'kind' => 'ENUM',
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'secretBackstory',
|
||||
'type' => [
|
||||
'name' => 'String',
|
||||
'kind' => 'SCALAR',
|
||||
'ofType' => null
|
||||
]
|
||||
'ofType' => null,
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'primaryFunction',
|
||||
'type' => [
|
||||
'name' => 'String',
|
||||
'kind' => 'SCALAR',
|
||||
'ofType' => null
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
'ofType' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
@ -329,7 +336,7 @@ class StarWarsIntrospectionTest extends TestCase
|
||||
}
|
||||
';
|
||||
|
||||
$expected = array(
|
||||
$expected = [
|
||||
'__schema' => [
|
||||
'queryType' => [
|
||||
'fields' => [
|
||||
@ -337,13 +344,13 @@ class StarWarsIntrospectionTest extends TestCase
|
||||
'name' => 'hero',
|
||||
'args' => [
|
||||
[
|
||||
'defaultValue' => NULL,
|
||||
'defaultValue' => null,
|
||||
'description' => 'If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.',
|
||||
'name' => 'episode',
|
||||
'type' => [
|
||||
'kind' => 'ENUM',
|
||||
'name' => 'Episode',
|
||||
'ofType' => NULL,
|
||||
'ofType' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
@ -356,13 +363,13 @@ class StarWarsIntrospectionTest extends TestCase
|
||||
'description' => 'id of the human',
|
||||
'type' => [
|
||||
'kind' => 'NON_NULL',
|
||||
'name' => NULL,
|
||||
'name' => null,
|
||||
'ofType' => [
|
||||
'kind' => 'SCALAR',
|
||||
'name' => 'String',
|
||||
],
|
||||
],
|
||||
'defaultValue' => NULL,
|
||||
'defaultValue' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
@ -374,21 +381,21 @@ class StarWarsIntrospectionTest extends TestCase
|
||||
'description' => 'id of the droid',
|
||||
'type' => [
|
||||
'kind' => 'NON_NULL',
|
||||
'name' => NULL,
|
||||
'name' => null,
|
||||
'ofType' =>
|
||||
[
|
||||
'kind' => 'SCALAR',
|
||||
'name' => 'String',
|
||||
],
|
||||
],
|
||||
'defaultValue' => NULL,
|
||||
'defaultValue' => null,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
);
|
||||
];
|
||||
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
@ -409,17 +416,9 @@ class StarWarsIntrospectionTest extends TestCase
|
||||
$expected = [
|
||||
'__type' => [
|
||||
'name' => 'Droid',
|
||||
'description' => 'A mechanical creature in the Star Wars universe.'
|
||||
]
|
||||
'description' => 'A mechanical creature in the Star Wars universe.',
|
||||
],
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to test a query and the expected response.
|
||||
*/
|
||||
private function assertValidQuery($query, $expected)
|
||||
{
|
||||
$this->assertEquals(['data' => $expected], GraphQL::executeQuery(StarWarsSchema::build(), $query)->toArray());
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +1,14 @@
|
||||
<?php
|
||||
namespace GraphQL\Tests;
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Tests;
|
||||
|
||||
use GraphQL\GraphQL;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class StarWarsQueryTest extends TestCase
|
||||
{
|
||||
// Star Wars Query Tests
|
||||
// Basic Queries
|
||||
|
||||
/**
|
||||
* @see it('Correctly identifies R2-D2 as the hero of the Star Wars Saga')
|
||||
*/
|
||||
@ -23,13 +22,24 @@ class StarWarsQueryTest extends TestCase
|
||||
}
|
||||
';
|
||||
$expected = [
|
||||
'hero' => [
|
||||
'name' => 'R2-D2'
|
||||
]
|
||||
'hero' => ['name' => 'R2-D2'],
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to test a query and the expected response.
|
||||
*/
|
||||
private function assertValidQuery($query, $expected) : void
|
||||
{
|
||||
$this->assertEquals(
|
||||
['data' => $expected],
|
||||
GraphQL::executeQuery(StarWarsSchema::build(), $query)->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
// Describe: Nested Queries
|
||||
|
||||
/**
|
||||
* @see it('Allows us to query for the ID and friends of R2-D2')
|
||||
*/
|
||||
@ -51,22 +61,16 @@ class StarWarsQueryTest extends TestCase
|
||||
'id' => '2001',
|
||||
'name' => 'R2-D2',
|
||||
'friends' => [
|
||||
[
|
||||
'name' => 'Luke Skywalker',
|
||||
['name' => 'Luke Skywalker'],
|
||||
['name' => 'Han Solo'],
|
||||
['name' => 'Leia Organa'],
|
||||
],
|
||||
[
|
||||
'name' => 'Han Solo',
|
||||
],
|
||||
[
|
||||
'name' => 'Leia Organa',
|
||||
],
|
||||
]
|
||||
]
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
|
||||
// Describe: Nested Queries
|
||||
// Describe: Using IDs and query parameters to refetch objects
|
||||
|
||||
/**
|
||||
* @see it('Allows us to query for the friends of friends of R2-D2')
|
||||
@ -93,42 +97,40 @@ class StarWarsQueryTest extends TestCase
|
||||
'friends' => [
|
||||
[
|
||||
'name' => 'Luke Skywalker',
|
||||
'appearsIn' => ['NEWHOPE', 'EMPIRE', 'JEDI',],
|
||||
'appearsIn' => ['NEWHOPE', 'EMPIRE', 'JEDI'],
|
||||
'friends' => [
|
||||
['name' => 'Han Solo',],
|
||||
['name' => 'Leia Organa',],
|
||||
['name' => 'C-3PO',],
|
||||
['name' => 'R2-D2',],
|
||||
['name' => 'Han Solo'],
|
||||
['name' => 'Leia Organa'],
|
||||
['name' => 'C-3PO'],
|
||||
['name' => 'R2-D2'],
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'Han Solo',
|
||||
'appearsIn' => ['NEWHOPE', 'EMPIRE', 'JEDI'],
|
||||
'friends' => [
|
||||
['name' => 'Luke Skywalker',],
|
||||
['name' => 'Luke Skywalker'],
|
||||
['name' => 'Leia Organa'],
|
||||
['name' => 'R2-D2',],
|
||||
]
|
||||
['name' => 'R2-D2'],
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'Leia Organa',
|
||||
'appearsIn' => ['NEWHOPE', 'EMPIRE', 'JEDI'],
|
||||
'friends' =>
|
||||
[
|
||||
['name' => 'Luke Skywalker',],
|
||||
['name' => 'Han Solo',],
|
||||
['name' => 'C-3PO',],
|
||||
['name' => 'R2-D2',],
|
||||
['name' => 'Luke Skywalker'],
|
||||
['name' => 'Han Solo'],
|
||||
['name' => 'C-3PO'],
|
||||
['name' => 'R2-D2'],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
]
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
|
||||
// Describe: Using IDs and query parameters to refetch objects
|
||||
|
||||
/**
|
||||
* @see it('Using IDs and query parameters to refetch objects')
|
||||
*/
|
||||
@ -142,9 +144,7 @@ class StarWarsQueryTest extends TestCase
|
||||
}
|
||||
';
|
||||
$expected = [
|
||||
'human' => [
|
||||
'name' => 'Luke Skywalker'
|
||||
]
|
||||
'human' => ['name' => 'Luke Skywalker'],
|
||||
];
|
||||
|
||||
$this->assertValidQuery($query, $expected);
|
||||
@ -162,18 +162,27 @@ class StarWarsQueryTest extends TestCase
|
||||
}
|
||||
}
|
||||
';
|
||||
$params = [
|
||||
'someId' => '1000'
|
||||
];
|
||||
$params = ['someId' => '1000'];
|
||||
$expected = [
|
||||
'human' => [
|
||||
'name' => 'Luke Skywalker'
|
||||
]
|
||||
'human' => ['name' => 'Luke Skywalker'],
|
||||
];
|
||||
|
||||
$this->assertValidQueryWithParams($query, $params, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to test a query with params and the expected response.
|
||||
*/
|
||||
private function assertValidQueryWithParams($query, $params, $expected)
|
||||
{
|
||||
$this->assertEquals(
|
||||
['data' => $expected],
|
||||
GraphQL::executeQuery(StarWarsSchema::build(), $query, null, null, $params)->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
// Using aliases to change the key in the response
|
||||
|
||||
/**
|
||||
* @see it('Allows us to create a generic query, then use it to fetch Han Solo using his ID')
|
||||
*/
|
||||
@ -186,13 +195,9 @@ class StarWarsQueryTest extends TestCase
|
||||
}
|
||||
}
|
||||
';
|
||||
$params = [
|
||||
'someId' => '1002'
|
||||
];
|
||||
$params = ['someId' => '1002'];
|
||||
$expected = [
|
||||
'human' => [
|
||||
'name' => 'Han Solo'
|
||||
]
|
||||
'human' => ['name' => 'Han Solo'],
|
||||
];
|
||||
$this->assertValidQueryWithParams($query, $params, $expected);
|
||||
}
|
||||
@ -209,21 +214,17 @@ class StarWarsQueryTest extends TestCase
|
||||
}
|
||||
}
|
||||
';
|
||||
$params = [
|
||||
'id' => 'not a valid id'
|
||||
];
|
||||
$expected = [
|
||||
'human' => null
|
||||
];
|
||||
$params = ['id' => 'not a valid id'];
|
||||
$expected = ['human' => null];
|
||||
$this->assertValidQueryWithParams($query, $params, $expected);
|
||||
}
|
||||
|
||||
// Using aliases to change the key in the response
|
||||
// Uses fragments to express more complex queries
|
||||
|
||||
/**
|
||||
* @see it('Allows us to query for Luke, changing his key with an alias')
|
||||
*/
|
||||
function testLukeKeyAlias()
|
||||
public function testLukeKeyAlias() : void
|
||||
{
|
||||
$query = '
|
||||
query FetchLukeAliased {
|
||||
@ -233,9 +234,7 @@ class StarWarsQueryTest extends TestCase
|
||||
}
|
||||
';
|
||||
$expected = [
|
||||
'luke' => [
|
||||
'name' => 'Luke Skywalker'
|
||||
],
|
||||
'luke' => ['name' => 'Luke Skywalker'],
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
@ -243,7 +242,7 @@ class StarWarsQueryTest extends TestCase
|
||||
/**
|
||||
* @see it('Allows us to query for both Luke and Leia, using two root fields and an alias')
|
||||
*/
|
||||
function testTwoRootKeysAsAnAlias()
|
||||
public function testTwoRootKeysAsAnAlias() : void
|
||||
{
|
||||
$query = '
|
||||
query FetchLukeAndLeiaAliased {
|
||||
@ -256,22 +255,16 @@ class StarWarsQueryTest extends TestCase
|
||||
}
|
||||
';
|
||||
$expected = [
|
||||
'luke' => [
|
||||
'name' => 'Luke Skywalker'
|
||||
],
|
||||
'leia' => [
|
||||
'name' => 'Leia Organa'
|
||||
]
|
||||
'luke' => ['name' => 'Luke Skywalker'],
|
||||
'leia' => ['name' => 'Leia Organa'],
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
|
||||
// Uses fragments to express more complex queries
|
||||
|
||||
/**
|
||||
* @see it('Allows us to query using duplicated content')
|
||||
*/
|
||||
function testQueryUsingDuplicatedContent()
|
||||
public function testQueryUsingDuplicatedContent() : void
|
||||
{
|
||||
$query = '
|
||||
query DuplicateFields {
|
||||
@ -288,12 +281,12 @@ class StarWarsQueryTest extends TestCase
|
||||
$expected = [
|
||||
'luke' => [
|
||||
'name' => 'Luke Skywalker',
|
||||
'homePlanet' => 'Tatooine'
|
||||
'homePlanet' => 'Tatooine',
|
||||
],
|
||||
'leia' => [
|
||||
'name' => 'Leia Organa',
|
||||
'homePlanet' => 'Alderaan'
|
||||
]
|
||||
'homePlanet' => 'Alderaan',
|
||||
],
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
@ -301,7 +294,7 @@ class StarWarsQueryTest extends TestCase
|
||||
/**
|
||||
* @see it('Allows us to use a fragment to avoid duplicating content')
|
||||
*/
|
||||
function testUsingFragment()
|
||||
public function testUsingFragment() : void
|
||||
{
|
||||
$query = '
|
||||
query UseFragment {
|
||||
@ -322,12 +315,12 @@ class StarWarsQueryTest extends TestCase
|
||||
$expected = [
|
||||
'luke' => [
|
||||
'name' => 'Luke Skywalker',
|
||||
'homePlanet' => 'Tatooine'
|
||||
'homePlanet' => 'Tatooine',
|
||||
],
|
||||
'leia' => [
|
||||
'name' => 'Leia Organa',
|
||||
'homePlanet' => 'Alderaan'
|
||||
]
|
||||
'homePlanet' => 'Alderaan',
|
||||
],
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
@ -348,7 +341,7 @@ class StarWarsQueryTest extends TestCase
|
||||
$expected = [
|
||||
'hero' => [
|
||||
'__typename' => 'Droid',
|
||||
'name' => 'R2-D2'
|
||||
'name' => 'R2-D2',
|
||||
],
|
||||
];
|
||||
$this->assertValidQuery($query, $expected);
|
||||
@ -371,32 +364,10 @@ class StarWarsQueryTest extends TestCase
|
||||
$expected = [
|
||||
'hero' => [
|
||||
'__typename' => 'Human',
|
||||
'name' => 'Luke Skywalker'
|
||||
'name' => 'Luke Skywalker',
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertValidQuery($query, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to test a query and the expected response.
|
||||
*/
|
||||
private function assertValidQuery($query, $expected)
|
||||
{
|
||||
$this->assertEquals(
|
||||
['data' => $expected],
|
||||
GraphQL::executeQuery(StarWarsSchema::build(), $query)->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to test a query with params and the expected response.
|
||||
*/
|
||||
private function assertValidQueryWithParams($query, $params, $expected)
|
||||
{
|
||||
$this->assertEquals(
|
||||
['data' => $expected],
|
||||
GraphQL::executeQuery(StarWarsSchema::build(), $query, null, null, $params)->toArray()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Tests;
|
||||
|
||||
/**
|
||||
@ -11,13 +14,16 @@ namespace GraphQL\Tests;
|
||||
* NOTE: This may contain spoilers for the original Star
|
||||
* Wars trilogy.
|
||||
*/
|
||||
use GraphQL\Type\Schema;
|
||||
|
||||
use GraphQL\Type\Definition\EnumType;
|
||||
use GraphQL\Type\Definition\InterfaceType;
|
||||
use GraphQL\Type\Definition\NonNull;
|
||||
use GraphQL\Type\Definition\ObjectType;
|
||||
use GraphQL\Type\Definition\ResolveInfo;
|
||||
use GraphQL\Type\Definition\Type;
|
||||
use GraphQL\Type\Schema;
|
||||
use function array_intersect_key;
|
||||
use function array_map;
|
||||
|
||||
/**
|
||||
* Using our shorthand to describe type systems, the type system for our
|
||||
@ -56,10 +62,9 @@ use GraphQL\Type\Definition\Type;
|
||||
*
|
||||
* We begin by setting up our schema.
|
||||
*/
|
||||
|
||||
class StarWarsSchema
|
||||
{
|
||||
public static function build()
|
||||
public static function build() : Schema
|
||||
{
|
||||
/**
|
||||
* The original trilogy consists of three movies.
|
||||
@ -73,17 +78,17 @@ class StarWarsSchema
|
||||
'values' => [
|
||||
'NEWHOPE' => [
|
||||
'value' => 4,
|
||||
'description' => 'Released in 1977.'
|
||||
'description' => 'Released in 1977.',
|
||||
],
|
||||
'EMPIRE' => [
|
||||
'value' => 5,
|
||||
'description' => 'Released in 1980.'
|
||||
'description' => 'Released in 1980.',
|
||||
],
|
||||
'JEDI' => [
|
||||
'value' => 6,
|
||||
'description' => 'Released in 1983.'
|
||||
'description' => 'Released in 1983.',
|
||||
],
|
||||
],
|
||||
]
|
||||
]);
|
||||
|
||||
$humanType = null;
|
||||
@ -103,7 +108,7 @@ class StarWarsSchema
|
||||
$characterInterface = new InterfaceType([
|
||||
'name' => 'Character',
|
||||
'description' => 'A character in the Star Wars Trilogy',
|
||||
'fields' => function() use (&$characterInterface, $episodeEnum) {
|
||||
'fields' => function () use (&$characterInterface, $episodeEnum) {
|
||||
return [
|
||||
'id' => [
|
||||
'type' => Type::nonNull(Type::string()),
|
||||
@ -111,7 +116,7 @@ class StarWarsSchema
|
||||
],
|
||||
'name' => [
|
||||
'type' => Type::string(),
|
||||
'description' => 'The name of the character.'
|
||||
'description' => 'The name of the character.',
|
||||
],
|
||||
'friends' => [
|
||||
'type' => Type::listOf($characterInterface),
|
||||
@ -119,7 +124,7 @@ class StarWarsSchema
|
||||
],
|
||||
'appearsIn' => [
|
||||
'type' => Type::listOf($episodeEnum),
|
||||
'description' => 'Which movies they appear in.'
|
||||
'description' => 'Which movies they appear in.',
|
||||
],
|
||||
'secretBackstory' => [
|
||||
'type' => Type::string(),
|
||||
@ -164,7 +169,7 @@ class StarWarsSchema
|
||||
$fieldSelection['id'] = true;
|
||||
|
||||
$friends = array_map(
|
||||
function($friend) use ($fieldSelection) {
|
||||
function ($friend) use ($fieldSelection) {
|
||||
return array_intersect_key($friend, $fieldSelection);
|
||||
},
|
||||
StarWarsData::getFriends($human)
|
||||
@ -175,22 +180,22 @@ class StarWarsSchema
|
||||
],
|
||||
'appearsIn' => [
|
||||
'type' => Type::listOf($episodeEnum),
|
||||
'description' => 'Which movies they appear in.'
|
||||
'description' => 'Which movies they appear in.',
|
||||
],
|
||||
'homePlanet' => [
|
||||
'type' => Type::string(),
|
||||
'description' => 'The home planet of the human, or null if unknown.'
|
||||
'description' => 'The home planet of the human, or null if unknown.',
|
||||
],
|
||||
'secretBackstory' => [
|
||||
'type' => Type::string(),
|
||||
'description' => 'Where are they from and how they came to be who they are.',
|
||||
'resolve' => function() {
|
||||
'resolve' => function () {
|
||||
// This is to demonstrate error reporting
|
||||
throw new \Exception('secretBackstory is secret.');
|
||||
},
|
||||
],
|
||||
],
|
||||
'interfaces' => [$characterInterface]
|
||||
'interfaces' => [$characterInterface],
|
||||
]);
|
||||
|
||||
/**
|
||||
@ -216,7 +221,7 @@ class StarWarsSchema
|
||||
],
|
||||
'name' => [
|
||||
'type' => Type::string(),
|
||||
'description' => 'The name of the droid.'
|
||||
'description' => 'The name of the droid.',
|
||||
],
|
||||
'friends' => [
|
||||
'type' => Type::listOf($characterInterface),
|
||||
@ -227,22 +232,22 @@ class StarWarsSchema
|
||||
],
|
||||
'appearsIn' => [
|
||||
'type' => Type::listOf($episodeEnum),
|
||||
'description' => 'Which movies they appear in.'
|
||||
'description' => 'Which movies they appear in.',
|
||||
],
|
||||
'secretBackstory' => [
|
||||
'type' => Type::string(),
|
||||
'description' => 'Construction date and the name of the designer.',
|
||||
'resolve' => function() {
|
||||
'resolve' => function () {
|
||||
// This is to demonstrate error reporting
|
||||
throw new \Exception('secretBackstory is secret.');
|
||||
},
|
||||
],
|
||||
'primaryFunction' => [
|
||||
'type' => Type::string(),
|
||||
'description' => 'The primary function of the droid.'
|
||||
]
|
||||
'description' => 'The primary function of the droid.',
|
||||
],
|
||||
'interfaces' => [$characterInterface]
|
||||
],
|
||||
'interfaces' => [$characterInterface],
|
||||
]);
|
||||
|
||||
/**
|
||||
@ -267,11 +272,11 @@ class StarWarsSchema
|
||||
'args' => [
|
||||
'episode' => [
|
||||
'description' => 'If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.',
|
||||
'type' => $episodeEnum
|
||||
]
|
||||
'type' => $episodeEnum,
|
||||
],
|
||||
],
|
||||
'resolve' => function ($root, $args) {
|
||||
return StarWarsData::getHero(isset($args['episode']) ? $args['episode'] : null);
|
||||
return StarWarsData::getHero($args['episode'] ?? null);
|
||||
},
|
||||
],
|
||||
'human' => [
|
||||
@ -280,13 +285,14 @@ class StarWarsSchema
|
||||
'id' => [
|
||||
'name' => 'id',
|
||||
'description' => 'id of the human',
|
||||
'type' => Type::nonNull(Type::string())
|
||||
]
|
||||
'type' => Type::nonNull(Type::string()),
|
||||
],
|
||||
],
|
||||
'resolve' => function ($root, $args) {
|
||||
$humans = StarWarsData::humans();
|
||||
return isset($humans[$args['id']]) ? $humans[$args['id']] : null;
|
||||
}
|
||||
|
||||
return $humans[$args['id']] ?? null;
|
||||
},
|
||||
],
|
||||
'droid' => [
|
||||
'type' => $droidType,
|
||||
@ -294,15 +300,16 @@ class StarWarsSchema
|
||||
'id' => [
|
||||
'name' => 'id',
|
||||
'description' => 'id of the droid',
|
||||
'type' => Type::nonNull(Type::string())
|
||||
]
|
||||
'type' => Type::nonNull(Type::string()),
|
||||
],
|
||||
],
|
||||
'resolve' => function ($root, $args) {
|
||||
$droids = StarWarsData::droids();
|
||||
return isset($droids[$args['id']]) ? $droids[$args['id']] : null;
|
||||
}
|
||||
]
|
||||
]
|
||||
|
||||
return $droids[$args['id']] ?? null;
|
||||
},
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
return new Schema(['query' => $queryType]);
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Tests;
|
||||
|
||||
use GraphQL\Language\Parser;
|
||||
@ -37,6 +40,16 @@ class StarWarsValidationTest extends TestCase
|
||||
$this->assertEquals(true, empty($errors));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to test a query and the expected response.
|
||||
*/
|
||||
private function validationErrors($query)
|
||||
{
|
||||
$ast = Parser::parse($query);
|
||||
|
||||
return DocumentValidator::validate(StarWarsSchema::build(), $ast);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see it('Notes that non-existent fields are invalid')
|
||||
*/
|
||||
@ -142,13 +155,4 @@ class StarWarsValidationTest extends TestCase
|
||||
$errors = $this->validationErrors($query);
|
||||
$this->assertEquals(true, empty($errors));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to test a query and the expected response.
|
||||
*/
|
||||
private function validationErrors($query)
|
||||
{
|
||||
$ast = Parser::parse($query);
|
||||
return DocumentValidator::validate(StarWarsSchema::build(), $ast);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace GraphQL\Tests;
|
||||
|
||||
use GraphQL\Utils\Utils;
|
||||
|
Loading…
Reference in New Issue
Block a user