Skip to content

Commit 257a38d

Browse files
committed
prettified star wars example code
1 parent a1c2aa8 commit 257a38d

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

examples/starwars/__data.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

33
$humans = [
4-
"1000" => ["type"=>"human", "id"=>"1000", "name"=>"Luke Skywalker", "friends"=>["1002", "1003", "2000", "2001"], "appearsIn"=>["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet"=>"Tatooine"],
5-
"1001" => ["type"=>"human", "id"=>"1001", "name"=>"Darth Vader", "friends"=>["1004"], "appearsIn"=>["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet"=>"Tatooine"],
6-
"1002" => ["type"=>"human", "id"=>"1002", "name"=>"Han Solo", "friends"=>["1000", "1003", "2001"], "appearsIn"=>["NEW_HOPE", "EMPIRE", "JEDI"]],
7-
"1003" => ["type"=>"human", "id"=>"1003", "name"=>"Leia Organa", "friends"=>["1000", "1002", "2000", "2001"], "appearsIn"=>["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet"=>"Alderaan"],
8-
"1004" => ["type"=>"human", "id"=>"1004", "name"=>"Wilhuff Tarkin", "friends"=>["1001"], "appearsIn"=>["NEW_HOPE"]],
4+
"1000" => ["type" => "human", "id" => "1000", "name" => "Luke Skywalker", "friends" => ["1002", "1003", "2000", "2001"], "appearsIn" => ["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet" => "Tatooine"],
5+
"1001" => ["type" => "human", "id" => "1001", "name" => "Darth Vader", "friends" => ["1004"], "appearsIn" => ["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet" => "Tatooine"],
6+
"1002" => ["type" => "human", "id" => "1002", "name" => "Han Solo", "friends" => ["1000", "1003", "2001"], "appearsIn" => ["NEW_HOPE", "EMPIRE", "JEDI"]],
7+
"1003" => ["type" => "human", "id" => "1003", "name" => "Leia Organa", "friends" => ["1000", "1002", "2000", "2001"], "appearsIn" => ["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet" => "Alderaan"],
8+
"1004" => ["type" => "human", "id" => "1004", "name" => "Wilhuff Tarkin", "friends" => ["1001"], "appearsIn" => ["NEW_HOPE"]],
99
];
1010

1111
$droids = [
12-
"2000" => ["type"=>"droid", "id"=>"2000", "name"=>"C-3PO", "friends"=>["1000", "1002", "1003", "2001"], "appearsIn"=>["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet"=>"Protocol"],
13-
"2001" => ["type"=>"droid", "id"=>"2000", "name"=>"R2-D2", "friends"=>["1000", "1002", "1003"], "appearsIn"=>["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet"=>"Astromech"],
12+
"2000" => ["type" => "droid", "id" => "2000", "name" => "C-3PO", "friends" => ["1000", "1002", "1003", "2001"], "appearsIn" => ["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet" => "Protocol"],
13+
"2001" => ["type" => "droid", "id" => "2000", "name" => "R2-D2", "friends" => ["1000", "1002", "1003"], "appearsIn" => ["NEW_HOPE", "EMPIRE", "JEDI"], "homeplanet" => "Astromech"],
1414
];

examples/starwars/__schema.php

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
<?php
22

33
use GraphQL\Types\GraphQLObjectType;
4-
use GraphQL\Types\GraphQLInt;
54
use GraphQL\Types\GraphQLEnum;
65
use GraphQL\Types\GraphQLEnumValue;
76
use GraphQL\Types\GraphQLString;
87
use GraphQL\Types\GraphQLInterface;
98
use GraphQL\Types\GraphQLNonNull;
109
use GraphQL\Types\GraphQLList;
11-
use GraphQL\Types\GraphQLUnion;
1210
use GraphQL\Fields\GraphQLTypeField;
1311
use GraphQL\Arguments\GraphQLFieldArgument;
1412

@@ -26,15 +24,15 @@
2624
/**
2725
* CHARACTER
2826
*/
29-
$Character = new GraphQLInterface("Character", "A character in the Star Wars Trilogy.", function() use(&$Character, &$Episode){
27+
$Character = new GraphQLInterface("Character", "A character in the Star Wars Trilogy.", function () use (&$Character, &$Episode) {
3028
return [
3129
new GraphQLTypeField("id", new GraphQLNonNull(new GraphQLString()), "The id of the character."),
3230
new GraphQLTypeField("name", new GraphQLString(), "The name of the character."),
3331
new GraphQLTypeField("friends", new GraphQLList($Character), "The friends of the character, or an empty list if they have none."),
3432
new GraphQLTypeField("appearsIn", new GraphQLList($Episode), "Which movies they appear in."),
3533
];
36-
}, function ($character){
37-
if($character["type"]==="human"){
34+
}, function ($character) {
35+
if ($character["type"] === "human") {
3836
return "Human";
3937
}
4038
return "Droid";
@@ -43,12 +41,12 @@
4341
/**
4442
* HUMAN
4543
*/
46-
$Human = new GraphQLObjectType("Human", "A humanoid creature in the Star Wars universe.", function() use(&$Character, &$Episode, &$humans, &$droids){
44+
$Human = new GraphQLObjectType("Human", "A humanoid creature in the Star Wars universe.", function () use (&$Character, &$Episode, &$humans, &$droids) {
4745
return [
4846
new GraphQLTypeField("id", new GraphQLNonNull(new GraphQLString()), "The id of the human."),
4947
new GraphQLTypeField("name", new GraphQLString(), "The name of the human."),
50-
new GraphQLTypeField("friends", new GraphQLList($Character), "The friends of the human, or an empty list if they have none.", function ($character) use(&$humans, &$droids){
51-
return array_map(function($friendId) use(&$humans, &$droids){
48+
new GraphQLTypeField("friends", new GraphQLList($Character), "The friends of the human, or an empty list if they have none.", function ($character) use (&$humans, &$droids) {
49+
return array_map(function ($friendId) use (&$humans, &$droids) {
5250
return $humans[$friendId] ?? $droids[$friendId];
5351
}, $character["friends"]);
5452
}),
@@ -62,12 +60,12 @@
6260
/**
6361
* DROID
6462
*/
65-
$Droid = new GraphQLObjectType("Droid", "A mechanical creature in the Star Wars universe.", function() use(&$Character, &$Episode, &$humans, &$droids){
63+
$Droid = new GraphQLObjectType("Droid", "A mechanical creature in the Star Wars universe.", function () use (&$Character, &$Episode, &$humans, &$droids) {
6664
return [
6765
new GraphQLTypeField("id", new GraphQLNonNull(new GraphQLString()), "The id of the droid."),
6866
new GraphQLTypeField("name", new GraphQLString(), "The name of the droid."),
69-
new GraphQLTypeField("friends", new GraphQLList($Character), "The friends of the droid, or an empty list if they have none.", function ($character) use(&$humans, &$droids){
70-
return array_map(function($friendId) use(&$humans, &$droids){
67+
new GraphQLTypeField("friends", new GraphQLList($Character), "The friends of the droid, or an empty list if they have none.", function ($character) use (&$humans, &$droids) {
68+
return array_map(function ($friendId) use (&$humans, &$droids) {
7169
return $humans[$friendId] ?? $droids[$friendId];
7270
}, $character["friends"]);
7371
}),
@@ -82,26 +80,26 @@
8280
/**
8381
* QUERY
8482
*/
85-
$Query = new GraphQLObjectType("Query", "Root Query", function () use (&$Episode, &$Character, &$Human, &$Droid, &$humans, &$droids){
83+
$Query = new GraphQLObjectType("Query", "Root Query", function () use (&$Episode, &$Character, &$Human, &$Droid, &$humans, &$droids) {
8684
return [
87-
new GraphQLTypeField("hero", $Character, "", function($_, $args) use(&$humans, &$droids){
88-
if(($args["episode"] ?? null)==="JEDI"){
89-
return $humans["1000"]; // Luke Skywalker
90-
}
91-
return $droids["2000"]; // R2-D2
92-
}, [
85+
new GraphQLTypeField("hero", $Character, "", function ($_, $args) use (&$humans, &$droids) {
86+
if (($args["episode"] ?? null) === "JEDI") {
87+
return $humans["1000"]; // Luke Skywalker
88+
}
89+
return $droids["2000"]; // R2-D2
90+
}, [
9391
new GraphQLFieldArgument("episode", $Episode, "If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode")
9492
]
9593
),
96-
new GraphQLTypeField("human", $Human, "", function($_, $args) use(&$humans){
97-
return $humans[$args["id"]] ?? null;
98-
}, [
94+
new GraphQLTypeField("human", $Human, "", function ($_, $args) use (&$humans) {
95+
return $humans[$args["id"]] ?? null;
96+
}, [
9997
new GraphQLFieldArgument("id", new GraphQLNonNull(new GraphQLString()), "id of the human")
10098
]
10199
),
102-
new GraphQLTypeField("droid", $Droid, "", function($_, $args) use(&$droids){
103-
return $droids[$args["id"]] ?? null;
104-
}, [
100+
new GraphQLTypeField("droid", $Droid, "", function ($_, $args) use (&$droids) {
101+
return $droids[$args["id"]] ?? null;
102+
}, [
105103
new GraphQLFieldArgument("id", new GraphQLNonNull(new GraphQLString()), "id of the droid")
106104
]
107105
)

0 commit comments

Comments
 (0)