|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | use GraphQL\Types\GraphQLObjectType; |
4 | | -use GraphQL\Types\GraphQLInt; |
5 | 4 | use GraphQL\Types\GraphQLEnum; |
6 | 5 | use GraphQL\Types\GraphQLEnumValue; |
7 | 6 | use GraphQL\Types\GraphQLString; |
8 | 7 | use GraphQL\Types\GraphQLInterface; |
9 | 8 | use GraphQL\Types\GraphQLNonNull; |
10 | 9 | use GraphQL\Types\GraphQLList; |
11 | | -use GraphQL\Types\GraphQLUnion; |
12 | 10 | use GraphQL\Fields\GraphQLTypeField; |
13 | 11 | use GraphQL\Arguments\GraphQLFieldArgument; |
14 | 12 |
|
|
26 | 24 | /** |
27 | 25 | * CHARACTER |
28 | 26 | */ |
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) { |
30 | 28 | return [ |
31 | 29 | new GraphQLTypeField("id", new GraphQLNonNull(new GraphQLString()), "The id of the character."), |
32 | 30 | new GraphQLTypeField("name", new GraphQLString(), "The name of the character."), |
33 | 31 | new GraphQLTypeField("friends", new GraphQLList($Character), "The friends of the character, or an empty list if they have none."), |
34 | 32 | new GraphQLTypeField("appearsIn", new GraphQLList($Episode), "Which movies they appear in."), |
35 | 33 | ]; |
36 | | -}, function ($character){ |
37 | | - if($character["type"]==="human"){ |
| 34 | +}, function ($character) { |
| 35 | + if ($character["type"] === "human") { |
38 | 36 | return "Human"; |
39 | 37 | } |
40 | 38 | return "Droid"; |
|
43 | 41 | /** |
44 | 42 | * HUMAN |
45 | 43 | */ |
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) { |
47 | 45 | return [ |
48 | 46 | new GraphQLTypeField("id", new GraphQLNonNull(new GraphQLString()), "The id of the human."), |
49 | 47 | 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) { |
52 | 50 | return $humans[$friendId] ?? $droids[$friendId]; |
53 | 51 | }, $character["friends"]); |
54 | 52 | }), |
|
62 | 60 | /** |
63 | 61 | * DROID |
64 | 62 | */ |
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) { |
66 | 64 | return [ |
67 | 65 | new GraphQLTypeField("id", new GraphQLNonNull(new GraphQLString()), "The id of the droid."), |
68 | 66 | 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) { |
71 | 69 | return $humans[$friendId] ?? $droids[$friendId]; |
72 | 70 | }, $character["friends"]); |
73 | 71 | }), |
|
82 | 80 | /** |
83 | 81 | * QUERY |
84 | 82 | */ |
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) { |
86 | 84 | 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 | + }, [ |
93 | 91 | new GraphQLFieldArgument("episode", $Episode, "If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode") |
94 | 92 | ] |
95 | 93 | ), |
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 | + }, [ |
99 | 97 | new GraphQLFieldArgument("id", new GraphQLNonNull(new GraphQLString()), "id of the human") |
100 | 98 | ] |
101 | 99 | ), |
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 | + }, [ |
105 | 103 | new GraphQLFieldArgument("id", new GraphQLNonNull(new GraphQLString()), "id of the droid") |
106 | 104 | ] |
107 | 105 | ) |
|
0 commit comments