Last active
August 21, 2018 22:44
-
-
Save damaya/cd5fcd91a88e18b56e14cdf7e6722c9a to your computer and use it in GitHub Desktop.
Using overblog for graphql with star wars example
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
From Resources/config/graphql/Query.types.yml | |
human: | |
type: "Human" | |
args: | |
id: | |
description: "id of the human" | |
type: "String!" | |
resolve: "@=resolver('character_human', [args])" | |
droid: | |
type: "Droid" | |
args: | |
id: | |
description: "id of the droid" | |
type: "String!" | |
resolve: "@=resolver('character_droid', [args])" | |
In Resources/config/graphql/types/ add the following files: | |
- Character.types.yml | |
- Droid.types.yml | |
- Human.types.yml | |
# src/MyBundle/Resources/config/graphql/Character.types.yml | |
# Characters in the Star Wars trilogy are either humans or droids. | |
# | |
# This implements the following type system shorthand: | |
# interface Character { | |
# id: String! | |
# name: String | |
# friends: [Character] | |
# appearsIn: [Episode] | |
# } | |
Character: | |
type: interface | |
config: | |
description: "A character in the Star Wars Trilogy" | |
fields: | |
id: | |
type: "String!" | |
description: "The id of the character." | |
name: | |
type: "String" | |
description: "The name of the character." | |
friends: | |
type: "[Character]" | |
description: "The friends of the character." | |
# used expression language to defined resolver (tagged services) | |
resolveType: "@=resolver('character_type', [value])" | |
------------------------------------------------------------------------- | |
Droid: | |
type: object | |
config: | |
description: "A mechanical creature in the Star Wars universe." | |
fields: | |
id: | |
type: "String!" | |
description: "The id of the droid." | |
name: | |
type: "String" | |
description: "The name of the droid." | |
friends: | |
type: "[Character]" | |
description: "The friends of the droid, or an empty list if they have none." | |
resolve: "@=resolver('character_friends', [value])" | |
primaryFunction: | |
type: "String" | |
description: "The primary function of the droid." | |
interfaces: [Character] | |
---------------------------------------------------------------------------------------- | |
# src/MyBundle/Resources/config/graphql/Human.types.yml | |
# We define our human type, which implements the character interface. | |
# | |
# This implements the following type system shorthand: | |
# type Human : Character { | |
# id: String! | |
# name: String | |
# friends: [Character] | |
# appearsIn: [Episode] | |
# } | |
Human: | |
type: object | |
config: | |
description: "A humanoid creature in the Star Wars universe." | |
fields: | |
id: | |
type: "String!" | |
description: "The id of the character." | |
# to deprecate a field, only set the deprecation reason | |
#deprecationReason: "A terrible reason" | |
name: | |
type: "String" | |
description: "The name of the character." | |
friends: | |
type: "[Character]" | |
description: "The friends of the character." | |
resolve: "@=resolver('character_friends', [value])" | |
homePlanet: | |
type: "String" | |
description: "The home planet of the human, or null if unknown." | |
interfaces: [Character] | |
----------------------------------------------------------------------------------------------- | |
From services.yml | |
my.graph.resolver.character: | |
class: AppBundle\GraphQL\Resolver\CharacterResolver | |
arguments: | |
- "@overblog_graphql.type_resolver" | |
tags: | |
- { name: overblog_graphql.resolver, alias: "character_type", method: "resolveType" } | |
- { name: overblog_graphql.resolver, alias: "character_friends", method: "resolveFriends" } | |
- { name: overblog_graphql.resolver, alias: "character_hero", method: "resolveHero" } | |
- { name: overblog_graphql.resolver, alias: "character_human", method: "resolveHuman" } | |
- { name: overblog_graphql.resolver, alias: "character_droid", method: "resolveDroid" } | |
-------------------------------------------------------------------------------------------------------- | |
The class with methods is: | |
<?php | |
/** | |
* Created by PhpStorm. | |
* User: diegoamaya | |
*/ | |
// src/MyBundle/GraphQL/Resolver | |
namespace AppBundle\GraphQL\Resolver; | |
require_once __DIR__ . '/../tests/StarWarsData.php'; | |
use GraphQL\Tests\StarWarsData; | |
use Overblog\GraphQLBundle\Resolver\TypeResolver; | |
class CharacterResolver | |
{ | |
/** | |
* @var TypeResolver | |
*/ | |
private $typeResolver; | |
public function __construct(TypeResolver $typeResolver) | |
{ | |
$this->typeResolver = $typeResolver; | |
} | |
public function resolveType($data) | |
{ | |
$humanType = $this->typeResolver->resolve('Human'); | |
$droidType = $this->typeResolver->resolve('Droid'); | |
$humans = StarWarsData::humans(); | |
$droids = StarWarsData::droids(); | |
if (isset($humans[$data['id']])) { | |
return $humanType; | |
} | |
if (isset($droids[$data['id']])) { | |
return $droidType; | |
} | |
return null; | |
} | |
public function resolveFriends($character) | |
{ | |
return StarWarsData::getFriends($character); | |
} | |
public function resolveHero($args) | |
{ | |
return StarWarsData::getHero(isset($args['episode']) ? $args['episode'] : null); | |
} | |
public function resolveHuman($args) | |
{ | |
$humans = StarWarsData::humans(); | |
return isset($humans[$args['id']]) ? $humans[$args['id']] : null; | |
} | |
public function resolveDroid($args) | |
{ | |
$droids = StarWarsData::droids(); | |
return isset($droids[$args['id']]) ? $droids[$args['id']] : null; | |
} | |
public function getCurrentTime() | |
{ | |
return "Current Time " . date("Y-m-d H:i:sa") . " Requested from GraphQL Server" ; | |
} | |
} | |
------------------------------------------------------------------------------------------------------------ | |
You should also create a local file with the starwarsdata info in AppBundle/GraphQL/tests/StarWarsData.php | |
<?php | |
namespace GraphQL\Tests; | |
class StarWarsData | |
{ | |
private static function luke() | |
{ | |
return [ | |
'id' => '1000', | |
'name' => 'Luke Skywalker', | |
'friends' => ['1002', '1003', '2000', '2001'], | |
'appearsIn' => [4, 5, 6], | |
'homePlanet' => 'Tatooine', | |
]; | |
} | |
private static function vader() | |
{ | |
return [ | |
'id' => '1001', | |
'name' => 'Darth Vader', | |
'friends' => ['1004'], | |
'appearsIn' => [4, 5, 6], | |
'homePlanet' => 'Tatooine', | |
]; | |
} | |
private static function han() | |
{ | |
return [ | |
'id' => '1002', | |
'name' => 'Han Solo', | |
'friends' => ['1000', '1003', '2001'], | |
'appearsIn' => [4, 5, 6], | |
]; | |
} | |
private static function leia() | |
{ | |
return [ | |
'id' => '1003', | |
'name' => 'Leia Organa', | |
'friends' => ['1000', '1002', '2000', '2001'], | |
'appearsIn' => [4, 5, 6], | |
'homePlanet' => 'Alderaan', | |
]; | |
} | |
private static function tarkin() | |
{ | |
return [ | |
'id' => '1004', | |
'name' => 'Wilhuff Tarkin', | |
'friends' => ['1001'], | |
'appearsIn' => [4], | |
]; | |
} | |
static function humans() | |
{ | |
return [ | |
'1000' => self::luke(), | |
'1001' => self::vader(), | |
'1002' => self::han(), | |
'1003' => self::leia(), | |
'1004' => self::tarkin(), | |
]; | |
} | |
private static function threepio() | |
{ | |
return [ | |
'id' => '2000', | |
'name' => 'C-3PO', | |
'friends' => ['1000', '1002', '1003', '2001'], | |
'appearsIn' => [4, 5, 6], | |
'primaryFunction' => 'Protocol', | |
]; | |
} | |
/** | |
* We export artoo directly because the schema returns him | |
* from a root field, and hence needs to reference him. | |
*/ | |
static function artoo() | |
{ | |
return [ | |
'id' => '2001', | |
'name' => 'R2-D2', | |
'friends' => ['1000', '1002', '1003'], | |
'appearsIn' => [4, 5, 6], | |
'primaryFunction' => 'Astromech', | |
]; | |
} | |
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) | |
{ | |
return array_map([__CLASS__, 'getCharacter'], $character['friends']); | |
} | |
/** | |
* @param $episode | |
* @return array | |
*/ | |
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(); | |
} | |
/** | |
* @param $id | |
* @return mixed|null | |
*/ | |
static function getHuman($id) | |
{ | |
$humans = self::humans(); | |
return isset($humans[$id]) ? $humans[$id] : null; | |
} | |
/** | |
* @param $id | |
* @return mixed|null | |
*/ | |
static function getDroid($id) | |
{ | |
$droids = self::droids(); | |
return isset($droids[$id]) ? $droids[$id] : null; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment